forked from Shwetabhdixit/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionsort.c
57 lines (52 loc) · 1 KB
/
insertionsort.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
46
47
48
49
50
51
52
53
54
55
56
57
// insertion sort implementation
// each time a data from unsorted array will be put in it's correct position in sorted array.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,j,n,arr[20],ele,temp;
char res;
printf("\nIMPLEMENTATION OF SELECTION SORT\n\n");
do
{
printf("\nEnter number of data: ");
scanf("%d",&n);
if(n<2)
{
printf("\nYou can't sort a array if it has less than 1 element..\n");
}
else
{
printf("\nEnter the elements: \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//sorting
for(i=1;i<n;i++)
{
ele = arr[i];
for(j=0;j<i;j++)
{
if(ele<arr[j])
{
temp = arr[j];
arr[j] = ele;
ele = temp;
}
}
arr[i]=ele;
}
// sorted now print
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
}
printf("\n\nDo you want to sort more dataset?(Y or N)");
res = getch();
}
while(res=='y' || res=='Y');
return 0;
}