forked from mithlesh4257/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInsertion_Sort.c
More file actions
43 lines (35 loc) · 719 Bytes
/
Insertion_Sort.c
File metadata and controls
43 lines (35 loc) · 719 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
#include <stdio.h>
// Function for insertion sort
void Insertion_Sort(int array[], int size)
{
int temp, j, i;
for(i = 1; i < size; i++)
{
temp = array[i];
j = i - 1;
// Do swapping
while(j >= 0 && array[j] > temp)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
// Function to print elements of array
void Print_Array(int array[], int size)
{
for(int i = 0; i < size; i++)
printf("%d\t",array[i]);
printf("\n");
}
// Driver Function
int main()
{
int array[] = {2, 4, 3, 1, 6, 8, 4};
Insertion_Sort(array, 7);
Print_Array(array, 7);
return 0;
}
// Output
// 1 2 3 4 4 6 8