C Program To Implement Insertion Sort Algorithm To Print N Numbers In Descending 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: 5 0 4 9 1 Sorted list in descending order: 9 5 4 1 0
♣ Downloads ::
Comments
Post a Comment