-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.c
More file actions
35 lines (31 loc) · 773 Bytes
/
Copy pathinsertion_sort.c
File metadata and controls
35 lines (31 loc) · 773 Bytes
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
void insertionSort(int ar_size, int * ar) {
for(int i = 1; i < ar_size; i++){
for(int j = i - 1, d = i; d > 0; j--, d--){ // >=
if(ar[d] < ar[j]){
// swap
int temp = ar[d];
ar[d] = ar[j];
ar[j] = temp;
}
}
for(int p = 0; p < ar_size; p++){
printf("%d ", ar[p]);
}
printf("\n");
}
}
int main(void) {
int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}
insertionSort(_ar_size, _ar);
return 0;
}