-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_techniques
99 lines (88 loc) · 2.15 KB
/
sorting_techniques
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
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int swapped;
for (int i = 0; i < n - 1; i++) {
swapped = 0;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}
if (swapped == 0) {
break;
}
}
}
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
void printArray(int arr[], int n) {
printf("[");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("]");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
While(1){
int choice;
printf("Menu:\n");
printf("1. Bubble Sort\n");
printf("2. Insertion Sort\n");
printf("3. Selection Sort\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
bubbleSort(arr, n);
printf("Bubble Sorted Array: ");
printArray(arr, n);
break;
case 2:
insertionSort(arr, n);
printf("Insertion Sorted Array: ");
printArray(arr, n);
break;
case 3:
selectionSort(arr, n);
printf("Selection Sorted Array: ");
printArray(arr, n);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
}