-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort6.h
59 lines (38 loc) · 1.2 KB
/
sort6.h
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
/****************************************************
REFERENCE
Author: GeeksforGeeks, improved by Sam007 and splevel62
Link: https://www.geeksforgeeks.org/gnome-sort-a-stupid-one/
****************************************************/
/*
You may declare additional functions here
*/
/****************************************************
YOU ARE NOT ALLOWED TO MODIFY THE FUNCTION PROTOTYPES
****************************************************/
/*
Sorts the array A using gnome (stupid) sorting algorithm.
@param int A[] array to be sorted
@param int n size of the array to be sorted
@param double *dCounter counter variable for critical parts of the code
*/
void sort6(int A[], int n, double *dCounter) {
// your code here
//documentation unfinished
int pos = 0;
int temp;
// might change != to <
while(pos != n){
if(pos == 0 || A[pos - 1] <= A[pos]){
//If at index 0 or when no swaps are made, move forward by 1 index
pos++;
}else if(A[pos - 1] > A[pos]){
//swap
temp = A[pos - 1];
A[pos - 1] = A[pos];
A[pos] = temp;
++*dCounter;
//If a swap is made, compare preceding pair by going 1 index back
pos--;
}
}
}