-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion-Sort.c
45 lines (39 loc) · 1012 Bytes
/
Insertion-Sort.c
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
#include <stdio.h>
// Function for Insertion Sort
void insertionSort(int array[], int n)
{
int i, j, key;
// loop through array
for(i = 1; i < n; i++)
{
// Create a variable for the element and set it equal to the
// previous elements index
key = array[i];
j = i - 1;
// loop backward while index ie greater than or equal to zero ann
// the current element is greater than the temp variable
while (j >= 0 && key < array[j])
{
// set next element equal to current element
array[j+1] = array[j];
j = j - 1;
}
// set next element equal to key element
array[j+1] = key;
}
}
// Function to print array
void printArray(int array[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ",array[i]);
}
// drivers code
int main()
{
int a[] = {5, 3, 9, 2, 8};
int size = 5;
insertionSort(a, size);
printArray(a, size);
}