-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSortingAlgoC.c
More file actions
84 lines (76 loc) · 2.36 KB
/
SortingAlgoC.c
File metadata and controls
84 lines (76 loc) · 2.36 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
int main() {
int a[10], n, i,j, temp, ch, key;
printf("\nMenu:\n1.Bubble sort\n2.Insertion\n3.Selection");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the size of array:");
scanf("%d", &n);
printf("\nEnter all the elements in array \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nApplying the logic of bubble sort:\n");
for(i=0; i<n-1; i++)
{
for(int j=0; j<n-1-i; j++)
{
if(a[j]>a[j+1]);
{
temp= a[j];
a[j]= a[j+1];
a[j+1]= temp;
}
}
}
printf("\nSorted array is:");
for(i=0; i<n; i++)
printf("%d\n", a[i]);
break;
case 2: printf("\nEnter the size of array:");
scanf("%d", &n);
printf("\nEnter all the elements in array \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nApplying the logic of insertion sort:\n");
for (i = 1; i < n; i++) {
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
printf("\nSorted array is:");
for(i=0; i<n; i++)
printf("%d\n", a[i]);
break;
case 3: printf("\nEnter the size of array:");
scanf("%d", &n);
printf("\nEnter all the elements in array \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nApplying the logic of selection sort:\n");
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\nSorted array is:");
for(i=0; i<n; i++)
printf("%d\n", a[i]);
break;
}
default :
{
printf("Invalid choice!!!!");
break;
}
}
return 0;
}