Pages

22 Apr 2012

Insertion Sort - Sorting an array.

Insertion Sort is one of the ways to sort an array.

void InsertSort(int* arr, int size)
{
int temp = 0;
int j =0;
for(int i = 1; i< size; ++i)
{
temp = arr[i];
j = i-1;
while(j >= 0 && temp < arr[j])
{
arr[j+1] = arr[j];
--j;
}
arr[j+1]=temp;
}
}

No comments:

Post a Comment