C examples for Data Structure:Sort
Sort a Given List of Numbers Using an Insertion Sort
#include <stdio.h> int myArray[20];/*ww w .j a v a 2s . c o m*/ int count = 10; void insertionSort() { int intInsert; int intVacancy; int i; for(i = 1; i < count; i++) { /* for loop begins */ intInsert = myArray[i]; intVacancy = i; while (intVacancy > 0 && myArray[intVacancy-1] > intInsert) { myArray[intVacancy] = myArray[intVacancy-1]; intVacancy--; } if(intVacancy != i) { /* if statement begins */ myArray[intVacancy] = intInsert; } /* if statement ends */ } /* for loop ends */ } void main() { int i; printf("Enter the 10 integers for insertion sort separated by white spaces: \n"); for (i=0; i < count; i++) scanf("%d", &myArray[i]); fflush(stdin); insertionSort(); printf("Sorted List: "); for(i = 0; i < count; i++) printf("%d ",myArray[i]); printf("\nThank you.\n"); }