-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.h
More file actions
152 lines (125 loc) · 4.83 KB
/
Sort.h
File metadata and controls
152 lines (125 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//I Ayne Delgado have not used any code other than my own (or that in the textbook) for this project.
//I also have not used any function or data-structure from the Standard-Template Library.
//I understand that any violation of this disclaimer will result in a 0 for the project.
#pragma once
#include <iostream>
#include <fstream>
#include <random>
#include <cassert>
using namespace std;
//The sort class
class Sort
{
private:
//------------Private Data Members---------------
int size;
int* array;
//----------------Private Methods------------------
//merge function
//Purpose: merges two sorted subarrays together from the merge_sort function
//Parameters: an int array, an int size of sub array 1, an int size of sub array 2
//Returns: none
//pre-conditions: size>1
//post-conditions: two sub arrays are sorted and merged together into one array
void merge(int[], int, int);
//merge_sort_helper fucntion
//Purpose: recursively sorts an array by sorting two sub arrays then merges them
//Parameters: an int array, an int: the array size
//Returns: none
//pre-condition: array size>1
//post-condtion: the array is sorted
void merge_sort_helper(int[], int);
//swap function
//Purpose: swap two integers in an array in order to sort the array;
//---part of the function to selection_sort function
//Parameters: two integers to swap
//Returns: none
//pre-condition: non-empty array
//post-condtion: numbers in array are swaped, array will eventually be sorted
void swap(int&, int&);
//partition function
//Purpose: sort the array using a pivot_index, part of the quick_sort function
//Parameters: an int array, in int size of the array, an int the pivot_index
//Returns: none
//Pre-conditions: non-empty array
//Post-conditons: numbers in the array are swaped based on the pivot, until the array is sorted
void partition(int[], int, int&);
//quick_sort_helper function
//Purpose: recursive helper method of the quick_sort function
//Parameters: an int array, an int size of the array
//Returns: none
//pre-conditions: non-empty array
//post-conditions: the array is sorted
void quick_sort_helper(int[], int);
public:
//Parameterized constructor
//Purpose: create an array of size passed
//Parameters: an int, size of the array
//Returns: none
//Pre-conditions: none
//Post-condtions: a Sort object is created, with a dynamically allocated array in it
Sort(int);
//Destructor
//Purpose: return dynamically allocated memory to the heap
//Parameters: none
//Returns: none
//Pre-conditions: none
//Post-condtions: dynamically allocated array is deleted
~Sort();
//insertion_sort function
//Purpose: sort an arrray using insertion algorithm
//Parameters: none
//Returns: none
//Pre-conditions: non empty array
//Post-conditions: the array is sorted
void insertion_sort();
//selection_sort function
//Purpose: sort an arrray using selection algorithm
//Parameters: none
//Returns: none
//Pre-conditions: non empty array
//Post-conditions: the array is sorted
void selection_sort();
//merge_sort function
//Purpose: sort an arrray by sorting each half of an array and merging both halves in order
//Parameters: none
//Returns: none
//Pre-conditions: non empty array
//Post-conditions: the array is sorted
void merge_sort();
//quick_sort function
//Purpose: sort an arrray using a pivot point (quick sort alogorithm)
//Parameters: none
//Returns: none
//Pre-conditions: non empty array
//Post-conditions: the array is sorted
void quick_sort();
//init_array fuction
//Purpose: add random elements into an array/re-randomize an array
//Parameters: none
//Returns: none
//pre-condition: size>0
//post-condition: the array is filled with random, unsorted numbers
void init_array();
//get_size function
//Purpose: returns the size of the array
//Parameters: none
//Returns: int, size of the array
//Pre-conditions: none
//Post-conditions: size of the array is known
int get_size() const;
//at function
//purpose: get a value at a specific index of the array, used in the << overload
//parameters: an unsigned int, index of the array
//returns: an int, value at the index
//pre-conditions: index<size
//post-conditions: value at the index is known
int at(unsigned) const;
};
//operator << overload
//purpose: use << to print out the array in a Sort object
//parameters: an ostream object, a sort object
//returns: an ostream object
//pre-conditions: a non-empty array
//post-conditions: values in the array are printed
ostream& operator<<(ostream&, const Sort&);