Here's a good link from Narue on many sorts, including the Insertion Sort. Go about 1/3 of the way down to find the Insertion Sort segment. It's an efficient sort on data that's mostly sorted already, but not so great for random lists. Narue explains on the link. An array of length 5 is pretty small, so any sort will work well on it.
http://eternallyconfuzzled.com/tuts/...t_sorting.aspx
This code:
insertionSort(array BM[])
for i = 1 to length[BM]-1 do
{
value = BM[i]
j = i-1
while (j >= 0 && BM[j] > value)
{
BM[j + 1] = BM[j]
j = j-1
}
BM[j+1] = value
}
isn't valid C++ code. You need brackets surrounding the function implementation, you need parentheses in your for-loop declaration, and you have a do and a while, but it isn't in the form of a do-while loop. Also, what is
length ? I'm not sure what language this is.
Reputation Points: 2614
Solved Threads: 687
Posting Expert
Online 5,372 posts
since Jan 2008