-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortingAlgos.cpp
159 lines (141 loc) · 3.47 KB
/
sortingAlgos.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
#include "headers/sortingAlgos.h"
void selectionSort(std::vector<int> &vect)
{
for (int i = 0; i < vect.size()-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < vect.size(); j++)
{
if (vect[j] < vect[min_idx])
{
min_idx = j;
}
displayArray(vect,j);
displayArray(vect,min_idx-1);
}
displayArray(vect,min_idx-1);
std::swap(vect[min_idx], vect[i]);
displayArray(vect,i-1);
}
}
void insertionSort(std::vector<int> &vect)
{
for (int i = 1; i < vect.size() ; i++)
{
int key = vect[i];
int j = i - 1;
while (vect[j] > key && j >= 0)
{
vect[j + 1] = vect[j];
j--;
displayArray(vect,j);
}
vect[j + 1] = key;
}
}
//We find the smallest element in the unsorted array and we put in the end of the sorted array
void bubbleSort(std::vector<int> &vect)
{
for (int i = 0; i < vect.size()-1; i++)
// Last i elements are already in place
for (int j = 0; j < vect.size()-i-1; j++)
{
if (vect[j] > vect[j+1])
std::swap(vect[j], vect[j+1]);
displayArray(vect,j);
}
};
void CocktailSort(std::vector<int> &vect)
{
bool swapped = true;
int start = 0;
int end = vect.size() - 1;
while (swapped)
{
swapped = false;
//From left to right
for (int i = start; i < end; ++i)
{
if (vect[i] > vect[i + 1])
{
std::swap(vect[i], vect[i + 1]);
swapped = true;
}
displayArray(vect,i);
}
if (!swapped)
break;
swapped = false;
--end;
//From right to left
for (int i = end - 1; i >= start; --i)
{
if (vect[i] > vect[i + 1])
{
std::swap(vect[i], vect[i + 1]);
swapped = true;
}
displayArray(vect,i);
}
++start;
}
}
//--------merge sort----------------------------------
void merge(std::vector<int> &vect, int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
int L[n1], R[n2];
// Copy data to temp arrays L[] and R[]
for (int i = 0; i < n1; i++)
L[i] = vect[l + i];
for (int j = 0; j < n2; j++)
R[j] = vect[m + 1 + j];
// Merge the temp arrays back into vect[l..r]
// Initial index of first subarray
int i = 0;
// Initial index of second subarray
int j = 0;
// Initial index of merged subarray
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
vect[k] = L[i];
i++;
}
else {
vect[k] = R[j];
j++;
}
displayArray(vect,k);
k++;
}
// Copy the remaining elements of
// L[], if there are any
while (i < n1) {
vect[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of
// R[], if there are any
while (j < n2) {
vect[k] = R[j];
j++;
k++;
}
}
// l is for left index and r is
// right index of the sub-array
// of vect to be sorted */
void mergeSort(std::vector<int> &vect,int l,int r){
if(l>=r){
return;//returns recursively
}
int m =l+ (r-l)/2;
mergeSort(vect,l,m);
mergeSort(vect,m+1,r);
merge(vect,l,m,r);
}