-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting.cpp
165 lines (141 loc) · 2.87 KB
/
Sorting.cpp
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
#include "Sorting.h"
//BEGIN FUNCTION DEFS
//SELECTION SORT
void SelectionSort(aType A[], int nElements, int& c, int& s)
{
int iSmallest;
aType tmp;
for (int i = 0; i < nElements; i++)
{
iSmallest = IndexOfSmallest(A, i, nElements - 1, c);
// swap
if (iSmallest > i)
{
tmp = A[i];
A[i] = A[iSmallest];
A[iSmallest] = tmp;
s++;
}
}
}
int IndexOfSmallest(aType A[], int iStart, int iEnd, int& c)
{
int index = -1;
aType aMin = A[iStart];
for (int i = iStart; i <= iEnd; i++)
{
c++;
if (A[i] < aMin)
{
aMin = A[i];
index = i;
}
}
return index;
}
//QUICK SORT
void Quicksort(aType a[], int first, int last, int& c, int& s)
{
int pivot;
if (first < last) {
pivot = Pivot(a, first, last, c, s);
Quicksort(a, first, pivot - 1, c, s);
Quicksort(a, pivot + 1, last, c, s);
}
}
int Pivot(aType a[], int first, int last, int& c, int& s)
{
int p = first;
aType pivot = a[first];
for (int i = first + 1; i <= last; i++) {
c++;
if (a[i] <= pivot) {
p++;
Swap(a[i], a[p], s);
}
}
Swap(a[p], a[first], s);
return p;
}
//MERGE SORT
void Mergesort(aType a[], int first, int last, int& c, int& s)
{
int middle;
c++;
if (first < last) {
middle = (first + last) / 2;
Mergesort(a, first, middle, c, s);
Mergesort(a, middle + 1, last, c, s);
Merge(a, first, middle, middle + 1, last, c, s);
}
}
/* Merge: Merge two segments of an array together.
*/
void Merge(aType a[],
int firstLeft, int lastLeft,
int firstRight, int lastRight, int& c, int& s)
{
aType tempArray[MAX_ARRAY];
int index = firstLeft;
int firstSave = firstLeft;
// Merge segments (array subsets)
while ((firstLeft <= lastLeft) && (firstRight <= lastRight))
{
c++;
c++;//two comparisons from loop
c++;//one comparison from if
if (a[firstLeft] < a[firstRight])
{
tempArray[index] = a[firstLeft];
firstLeft++;
s++;
}
else
{
tempArray[index] = a[firstRight];
firstRight++;
s++;
}
index++;
}
// Copy remainder of left array into tempArray
while (firstLeft <= lastLeft)
{
c++;
tempArray[index] = a[firstLeft];
firstLeft++;
index++;
}
// Copy remainder of right array into tempArray
while (firstRight <= lastRight)
{
c++;
tempArray[index] = a[firstRight];
firstRight++;
index++;
}
// Copy back into original array
for (index = firstSave; index <= lastRight; index++)
a[index] = tempArray[index];
}
/* Swap: Swap two items (by reference).
*/
void Swap(aType& v1, aType& v2, int& s)
{
aType tmpVal;
tmpVal = v1;
v1 = v2;
v2 = tmpVal;
s++;
}
void PrintArray(aType A[], int nElements)
{
cout << "[ ";
for (int i = 0; i < nElements; i++)
{
cout << A[i];
if (i < nElements - 1)
cout << ", ";
}
cout << " ] " << endl;
}