-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.c
More file actions
102 lines (90 loc) · 2.29 KB
/
Copy pathSorting.c
File metadata and controls
102 lines (90 loc) · 2.29 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void selectionSort(int arr[], int n) {
int min, temp;
for (int i = 0; i < n-1; i++) {
min = i;
for (int j = i+1; j < n; j++) {
if (arr[min] > arr[j]) {
min = j;
}
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
void insertionSort(int arr[], int n) {
int key, j;
for (int i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void bubbleSort(int arr[], int n) {
int temp;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n, choice;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: \n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: \n");
printArray(arr, n);
printf("Choose a sorting method:\n");
printf("1. Selection Sort\n");
printf("2. Insertion Sort\n");
printf("3. Bubble Sort\n");
printf("Enter your choice: ");
scanf("%d", &choice);
clock_t start, end;
double cpu_time_used;
start = clock();
switch (choice) {
case 1:
selectionSort(arr, n);
printf("Array sorted using Selection Sort: \n");
break;
case 2:
insertionSort(arr, n);
printf("Array sorted using Insertion Sort: \n");
break;
case 3:
bubbleSort(arr, n);
printf("Array sorted using Bubble Sort: \n");
break;
default:
printf("Invalid choice\n");
return 1;
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printArray(arr, n);
printf("Time taken to sort the array: %f seconds\n", cpu_time_used);
return 0;
}