-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.cpp
More file actions
211 lines (175 loc) · 4.13 KB
/
Sort.cpp
File metadata and controls
211 lines (175 loc) · 4.13 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Sort.cpp
// sort
//
// Created by Ayne Delgado on 2/15/15.
//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.
#include "Sort.h"
Sort::Sort(int aSize)
{
size=aSize;
array=new int[size];
}
Sort::~Sort()
{
delete[] array;
array=nullptr;
}
void Sort::init_array()
{
assert(size>0);
for(int i=0; i<size; i++)
{
array[i] = rand()%size;
}
}
void Sort::merge(int data[], int n1, int n2)
{
int* temp = new int [n1+n2];
int copied =0;
int copied1 = 0;
int copied2 = 0;
int i;
while((copied1<n1)&&(copied2<n2))
{
if(data[copied1]<(data+n1)[copied2])
temp[copied++] = data[copied1++];
else
temp[copied++] = (data+n1)[copied2++];
}
while(copied1<n1)
temp[copied++]=data[copied1++];
while(copied2<n2)
temp[copied++]=(data+n1)[copied2++];
for(i=0; i<n1+n2;++i)
data[i]=temp[i];
delete [] temp;
}
void Sort::merge_sort_helper(int data[], int n)
{
int n1 = n/2;
int n2 = n-n1;
if(n>1)
{
merge_sort_helper(data, n1);
merge_sort_helper((data+n1), n2);
merge(data, n1, n2);
}
merge(data, n1, n2);
}
void Sort::merge_sort()
{
assert(size>0);
merge_sort_helper(array, size);
}
void Sort::insertion_sort()
{
assert(size>0);
for(int i=0; i<size-1; i++)
{
if(array[i+1]<array[i])
{
int temp = array[i+1];
int insert = 0;
while(temp>array[insert])
insert++;
for(int j=i+1; j>insert; j--)
array[j]=array[j-1];
array[insert]=temp;
}
}
}
void Sort::swap(int& a, int& b)
{
int temp = a;
a=b;
b=temp;
}
void Sort::selection_sort()
{
assert(size>0);
int i, j, index_of_largest;
int largest;
if(size==0)
return;
for(i=size-1; i>0; --i)
{
largest=array[0];
index_of_largest = 0;
for(j=1; j<=i; ++j)
{
if(array[j]>largest)
{
largest=array[j];
index_of_largest=j;
}
}
swap(array[i], array[index_of_largest]);
}
}
void Sort::partition(int data[], int n, int& pivot_index)
{
int pivot=data[0];
int left_index = 1;
int right_index = n-1;
while(left_index<=right_index)//while left hasnt crossed right
{
while(left_index<n && data[left_index]<=pivot)//find next largest
left_index++;
while(data[right_index]>pivot)//find next smallest
right_index--;
if(left_index<right_index)//if indecies haven't crossed, swap them
{
int temp = data[left_index];
data[left_index]=data[right_index];
data[right_index]=temp;
}
}
pivot_index=right_index; //set pivot index
//swap right_index and pivot
data[0] = data[pivot_index];
data[pivot_index]=pivot;
}
void Sort::quick_sort_helper(int data[], int n)
{
int pivot_index; //array index for the pivot element
int n1;//number of elements before the pivot element
int n2;//number of elements after the pivot element
if(n>1)
{
partition(data, n, pivot_index);
n1=pivot_index;
n2= n-n1-1;
quick_sort_helper(data, n1);
quick_sort_helper((data + pivot_index + 1), n2);
}
}
void Sort::quick_sort()
{
assert(size>0);
quick_sort_helper(array, size);
}
int Sort::get_size()const
{
return size;
}
int Sort::at(unsigned index) const
{
assert(index<size);
return array[index];
}
ostream& operator<<(ostream& out, const Sort& s)
{
if(s.get_size()<0)
{
out<<"Empty array"<<endl;
return out;
}
for(int i=0; i<s.get_size(); i++)
{
out<<s.at(i)<<endl;
}
return out;
}