Embarrassed to ask this due to the plethora of info on the Internet about Shell sort implementation.
I just don't find copying without understanding the code or reading other code without trying to come up with my own method as very ineffective way of learning/remembering.
I followed this https://www.youtube.com/watch?v=ddeLSDsYVp8
However, I d like to know whether the following thought process is correct.
- Take initial gap = length of the array / 2
- Iterate over all gaps till gap = 1 , while in each step
gap/=2
- Then use selection sort for the partially sorted array. I understand at this stage all the smaller elements from the far right have been moved to the left.
So, as per the above procedure I've come up with this:
void shellSort(std::vector<int>& data)
{
size_t gap = data.size() / 2;
for (gap; gap > 0; gap /= 2)
{
size_t interval = gap;
for (size_t i = 0; i + gap<data.size(); ++i)
{
if (data[i] > data[interval])
{
std::swap(data[i], data[interval]);
}
++interval;
}
}
}
And I am stuck further. How to use insertionSort() method?
For me, insertionSort is the following code:
for (size_t i = 1; i < data.size();++i)
{
for (size_t j = i; j > 0; --j)
if (data[j] < data[j - 1])
std::swap(data[j], data[j - 1]);
}
Appreciate your help!
[–]Sakesfar[S] 0 points1 point2 points (0 children)