That's not an insertion sort. It mixes
indices and
values, and clobbers data.
Remember, an
index into the array tells you
where some value is.
A
value is what you are sorting relative to other values.
They are different types of things, and cannot be mixed.
You need to read up on how an insertion sort works:
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/#algorithm
The algorithm works by treating an entire array as having two parts:
- stuff in the front is sorted
- stuff in the back is not sorted
That means you only need to modify an existing array -- you do not need to create additional arrays. Hence, your function should look like:
1 2 3 4
|
void Insertion_sort(vector<int> &A)
{
// sort A here
}
|
(No need to
return anything.)
For the actual sorting, you'll need to do two things:
1 - search for the next value in the
unsorted part of the array.
(This is the same thing you did in earlier assignments where you had to
find the smallest value in an array.)
2 - swap the value (just found) to its final location (the beginning of
the unsorted part).
This is illustrated nicely for you in the link:
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/#algorithm
Be careful what you are comparing. Remember the find-minimum type homeworks.
Good luck!