forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertionSort.c
35 lines (30 loc) · 718 Bytes
/
insertionSort.c
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
/***Insertion Sort Algorithm***/
//@Author : SoumyadipGhosh23 (GitHub: https://github.com/SoumyadipGhosh23)
#include<stdio.h>
#include<stdlib.h>
void printArr(int* a, int n){
for(int i=0; i<n; i++){
printf("%d ",a[i]);
}
printf("\n");
}
void insertionSort(int* a, int n){
int temp,j;
for(int i =1; i<n; i++){
printf("working on the pass no: %d\n",i);
temp = a[i];
j= i-1;
while(j>=0 && a[j]>temp){
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
int main(){
//int arr[] = {2,36,1,31,10,27,11,78,80,45};
int arr[] = {1,2,3,4,5,6,7,8,9,10};
printArr(arr,10);
insertionSort(arr,10);
printArr(arr,10);
}