-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion_sort.c
More file actions
42 lines (35 loc) · 1.07 KB
/
Copy pathinsertion_sort.c
File metadata and controls
42 lines (35 loc) · 1.07 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
#include <stdio.h>
void insert(int a[], int number) /* function to sort an array with insertion sort */
{
int i, j, temp;
for (i = 1; i < number; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[], int number) /* function to print the array */
{
for (int i = 0; i < number; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = { 200, 100, 2000, 500 };
int number = sizeof(a) / sizeof(a[0]); // size of array
printf("Before sorting array numbers are - \n");
printArr(a, number);
insert(a, number);
printf("\nAfter sorting array numbers are - \n");
printArr(a, number);
// printf("%d \n",sizeof(number));
// printf("%d \n",sizeof(a[0]));
// printf("%d \n",sizeof(a));
return 0;
}