-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d0aba3
commit 25e071b
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |