-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.c
More file actions
59 lines (50 loc) · 1.08 KB
/
InsertionSort.c
File metadata and controls
59 lines (50 loc) · 1.08 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
#include <stdio.h>
//For the array size input
int arraySize(){
int s;
printf("Input the array size: ");
scanf("%d", &s);
return s;
}
//For the array input
void arrayInput(int s, int x[]){
printf("Input the array: ");
for(int j = 0; j < s; j++){
scanf("%d", &x[j]);
}
}
//The Insertion Sorting algorithm
void InsertionSort(int s, int a[]){
int key = 0, k = 0;
for(int i = 1; i < s; i++){
key = a[i];
for(k = i-1; k >= 0; k--){
if(a[k] > key){
a[k+1] = a[k];
}
else{
break;
}
}
a[k+1] = key;
}
}
//For printing out the array's elements on the screen
void arrayOutput(int s, int a[]){
printf("The new array is: ");
for(int k = 0; k < s; k++){
printf("%d ", a[k]);
}
}
int main()
{
//Taking input
int s = arraySize();
int a[s];
arrayInput(s, a);
//Sorting using bogosort
InsertionSort(s, a);
//Printing the output
arrayOutput(s, a);
return 0;
}