Skip to content

Commit

Permalink
Create insertionSort.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Saiyed-Gulamali authored Oct 2, 2021
1 parent 4d0aba3 commit 25e071b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions insertionSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<stdio.h>

void printArray(int* A, int n){
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}

void insertionSort(int *A, int n){
int key, j;
// Loop for passes
for (int i = 1; i <= n-1; i++)
{
key = A[i];
j = i-1;
// Loop for each pass
while(j>=0 && A[j] > key){
A[j+1] = A[j];
j--;
}
A[j+1] = key;
}
}

int main() {
int A[] = {12, 54, 65, 7, 23, 9};
int n = 6;
printf("Before Sorting: ");
printArray(A, n);
insertionSort(A, n);
printf("After Sorting: ");
printArray(A, n);
return 0;
}

0 comments on commit 25e071b

Please sign in to comment.