-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting.cpp
122 lines (104 loc) Β· 2.61 KB
/
Sorting.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
void bubble()
{
int count, temp, i, j, number[30];
printf("How many numbers are u going to enter?: ");
scanf("%d", &count);
printf("Enter %d numbers: ", count);
for (i = 0; i < count; i++)
scanf("%d", &number[i]);
for (i = count - 2; i >= 0; i--)
{
for (j = 0; j <= i; j++)
{
if (number[j] > number[j + 1])
{
temp = number[j];
number[j] = number[j + 1];
number[j + 1] = temp;
}
}
}
printf("Sorted elements: ");
for (i = 0; i < count; i++)
printf(" %d", number[i]);
}
void selection_sort()
{
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d", &count);
printf("Enter %d elements: ", count);
// Loop to get the elements stored in array
for (i = 0; i < count; i++)
scanf("%d", &number[i]);
// Logic of selection sort algorithm
for (i = 0; i < count; i++)
{
for (j = i + 1; j < count; j++)
{
if (number[i] > number[j])
{
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
printf("Sorted elements: ");
for (i = 0; i < count; i++)
printf(" %d", number[i]);
}
void insertation()
{
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d", &count);
printf("Enter %d elements: ", count);
// This loop would store the input numbers in array
for (i = 0; i < count; i++)
scanf("%d", &number[i]);
// Implementation of insertion sort algorithm
for (i = 1; i < count; i++)
{
temp = number[i];
j = i - 1;
while ((temp < number[j]) && (j >= 0))
{
number[j + 1] = number[j];
j = j - 1;
}
number[j + 1] = temp;
}
printf("Order of Sorted elements: ");
for (i = 0; i < count; i++)
printf(" %d", number[i]);
}
int main()
{
int h;
do
{
printf("1.for bubble sorting\n2.for selection sorting\n3.for insertation\n4.exit\n");
printf("enter your choice :");
scanf("%d", &h);
switch (h)
{
case 1:
bubble();
break;
case 2:
selection_sort();
break;
case 3:
insertation();
break;
case 4:
break;
default:
printf("\n ener a valid choice");
break;
}
} while (h >= 4);
return 0;
}