forked from sanjaysunil34/hacktober2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-Sorting.c
More file actions
78 lines (67 loc) · 1.45 KB
/
4-Sorting.c
File metadata and controls
78 lines (67 loc) · 1.45 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
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int temp;
for(int j = 0; j < n - 1; j++)
{
for(int i = 0; i < n - j - 1; i++)
{
if(arr[i] > arr[i+1]){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
void insertionSort(int arr[], int n) {
int j, key;
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--;
}
arr[j+1] = key;
}
}
void selectionSort(int arr[], int n) {
int temp,min_index;
for(int i = 0; i < n-1; i++)
{
min_index = i;
for(int j = i+1; j < n; j++)
{
if(arr[j] < arr[min_index])
min_index = j;
}
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
int main() {
int q, n, t;
int arr[5000];
scanf("%d", &q);
while (q--) {
scanf("%d%d", &t, &n);
int i;
for(i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
if (t == 1) {
bubbleSort(arr, n);
} else if (t == 2) {
insertionSort(arr, n);
} else {
selectionSort(arr, n);
}
for(i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
}
}