C Program To Implement Insertion Sort Algorithm To Print N Numbers In Ascending Order
♣ Try it Out ::
#include<stdio.h> #include<conio.h> void InsertionSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); InsertionSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void InsertionSort(int Arr[], int Size) { int Key, i, j; for(i=0; i<Size; i++) { Key = Arr[i]; j = i - 1; while(j>=0 && Arr[j]>Key) { Arr[j+1] = Arr[j]; j--; } j++; Arr[j] = Key; } }
♣ Output ::
How many numbers you wish to enter: 5 Enter 5 integers: 4 3 0 8 7 Sorted list in ascending order: 0 3 4 7 8
♣ Downloads ::
Comments
Post a Comment