-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_sort.c
More file actions
48 lines (44 loc) · 836 Bytes
/
Copy pathInsertion_sort.c
File metadata and controls
48 lines (44 loc) · 836 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
#include<stdio.h>
int i, j, k, size;
void insertion_sort();
void main()
{
int ch;
printf("In which order do you wanna sort?\n 1.Ascending 2.Descending\nEnter your choice(number): ");
scanf("%d", &ch);
printf("Enter the size of array: ");
scanf("%d", &size);
int a[size];
printf("Enter the elements of array: ");
for(i=0;i<size;i++)
scanf("%d", &a[i]);
insertion_sort(a);
switch(ch)
{
case 1:
printf(" Sorted array: ");
for(i=0; i<size; i++)
printf("%d ", a[i]);
break;
case 2:
printf(" Sorted array: ");
for(i=size-1; i>=0; i--)
printf("%d ", a[i]);
break;
}
}
void insertion_sort(int arr[])
{
int temp;
for(k=1;k<size;k++)
{
temp = arr[k];
j = k-1;
while(temp<= arr[j] && j>=0)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}