-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.c
More file actions
51 lines (37 loc) · 772 Bytes
/
insertionSort.c
File metadata and controls
51 lines (37 loc) · 772 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 10
void printArray(char* name, int arr[], int size) {
printf("%s = {", name);
for (int i=0; i<size; i++) {
printf("%d", arr[i]);
if (i!=size-1)
printf(", ");
}
printf("}\n");
}
void insertionSort(int arr[], int size) {
int val, pos;
for (int i=1; i<size; i++) {
val = arr[i];
for (pos=i; pos>0; pos--) {
if (val<arr[pos-1])
arr[pos]=arr[pos-1];
else
break;
}
arr[pos] = val;
printArray("arr", arr, size);
}
}
void main() {
int arr[ARRAY_SIZE];
srand(time(NULL));
for (int i=0; i<ARRAY_SIZE; i++) {
arr[i] = rand()%1000;
}
printArray("arr", arr, ARRAY_SIZE);
insertionSort(arr, ARRAY_SIZE);
printArray("arr", arr, ARRAY_SIZE);
}