-
Notifications
You must be signed in to change notification settings - Fork 1
/
1_5_Sort.c
107 lines (94 loc) · 1.78 KB
/
1_5_Sort.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
int a[10], n;
void insertion()
{
int i;
printf("Enter no of elements (up to 10):\n");
scanf("%d", &n);
if (n <= 10) {
printf("Enter elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
} else {
printf("Array size exceeded. Maximum allowed size is 10.\n");
}
}
void disp()
{
int i;
if (n > 0)
{
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
else
{
printf("empty array");
}
}
void sort()
{
int i, j;
int temp;
int swapped;
if (n > 0)
{
for (i = 0; i < n - 1; i++)
{
swapped = 0;
for (j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
else
{
printf("empty array");
}
}
int menu() // menu function
{
int ch;
printf("\n=======MENU========\nINSERT-1\nDISPLAY-2\nSORT-3\nnEXIT-4\nEnter A Choice:");
scanf("%d", &ch);
return ch;
}
void process()
{
int ch;
for (ch = menu(); ch != 4; ch = menu())
{
switch (ch)
{
case 1:
insertion();
break;
case 2:
disp();
break;
case 3:
sort();
break;
default:
printf("Invalid Choice\n");
}
}
}
int main()
{
process();
return 0;
}