#include <stdio.h>
#define MAX 10
void insertionSort(int data[MAX])
{
for (int index = 1; index < MAX; index++)
{
for (int previousIndex = index - 1, currentIndex = index; previousIndex >= 0; previousIndex--, currentIndex--)
{
if (data[previousIndex] < data[currentIndex])
break;
int temporary = data[previousIndex];
data[previousIndex] = data[currentIndex];
data[currentIndex] = temporary;
}
}
}
void main()
{
int data[] = {3, 10, 4, 6, 8, 9, 7, 2, 1, 5};
printf("Before: ");
for (int i = 0; i < MAX; i++)
printf("%d ", data[i]);
insertionSort(data);
printf("\nAfter: ");
for (int i = 0; i < MAX; i++)
printf("%d ", data[i]);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: