-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.c
More file actions
52 lines (46 loc) · 970 Bytes
/
InsertionSort.c
File metadata and controls
52 lines (46 loc) · 970 Bytes
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
#include<stdio.h>
/**
Worst Case O(n^2) performance, Works good with partially sorted lists.
*/
void insertionSort(int *list, int n)
{
int currCard, pos;
for(int i=1;i<n;i++)
{
currCard = list[i];
pos = i-1;
while(pos>=0)
{
if(list[pos]>currCard)
{
// printf("Shift %d over %d\n",list[pos],list[pos+1] );
list[pos+1]=list[pos];
pos--;
}
else
break;
}
list[pos+1]=currCard;
}
return;
}
/**
If getting compilation errors set -std=c99 in compiler options.
*/
/**
Uncomment commented printf for better understanding
*/
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",(a+i));
/**
Invoke Sorting function here.
*/
insertionSort(a,n);
for(int i=0;i<n;i++)
printf("%d",*(a+i));
}