-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertionSort.c
More file actions
36 lines (30 loc) · 785 Bytes
/
insertionSort.c
File metadata and controls
36 lines (30 loc) · 785 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
#include <stdio.h>
void insertionSort(int array[], int size){
for (int step = 1; step<size; step++){
int right = array[step];
int left = step - 1;
while (right < array[left]&&left>=0){
array[left+1]=array[left];
--left;
}
array[left+1]=right;
}
}
void printArray(int array[], int size){
printf("\nSorted array in ascending order: ");
for (int i=0;i<size;i++){
printf("%d ",array[i]);
}
}
int main(){
int arraySize;
printf("Enter the array size: ");
scanf("%d",&arraySize);
int data[arraySize];
printf("Enter the array elements: ");
for(int i=0;i<arraySize;i++){
scanf("%d",&data[i]);
}
insertionSort(data, arraySize);
printArray(data, arraySize);
}