-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertionSort.h
44 lines (31 loc) · 971 Bytes
/
insertionSort.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
/****************************************************
REFERENCE
Author: Faizan Parvez, Great Learning
Link: https://www.mygreatlearning.com/blog/insertion-sort-algorithm/
****************************************************/
/*
You may declare additional functions here
*/
/****************************************************
YOU ARE NOT ALLOWED TO MODIFY THE FUNCTION PROTOTYPES
****************************************************/
/*
Sorts the array A using insertion 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
*/
// function to sort the elements of the array
void insertionSort(int A[], int n, double *dCounter) {
int i;
for (i = 1; i < n; i++) {
int tmp = A[i];
int j = i - 1;
while (tmp < A[j] && j >= 0) {
A[j + 1] = A[j];
j--;
++*dCounter;
}
A[j + 1] = tmp;
}
}