#include #include // Sort an array using Insertion Sort void printArray(int a[], int n) { int i; printf("\nIndex i: "); for(i=0; i< n; i++) printf("%2d, ", i); printf("\nElement a[i]: "); for(i=0; i< n; i++) printf("%2d, ", a[i]); printf("\n\n"); } #define N 6 int main() { int a[N]= { 23, 78, 45, 8, 32, 56}; int i; int DEBUG = 1; // 0; // Print the unsorted array printf("===== UNSORTED ARRAY ======"); printArray(a, N); for(i = 1; i < N; i++) { int j = i; int INS = a[i]; while ((j > 0) && (a[j-1] > INS)) { a[j] = a[j-1]; // shift elements to the right j--; } a[j] = INS; // insert the element if(DEBUG) { printf("===== ITERATION %d ======", i); printArray(a, N); } } // Print the sorted array printf("===== SORTED ARRAY ======"); printArray(a, N); system("pause"); }