Separate growth elements at the same locations and elements at odd locations with+++
-
Separate growth features and elements at odd locations. I can't figure out how to distort the elements in the wrong places. Please help me.
void Sort(int* parr, const int size)
{
for (int i = 1; i < size; i++) { if ((i % 2) == 0) { int tmp = parr[i]; int j = 0; while (tmp > parr[j]) j++; for (int k = i - 1; k >= j; k--) parr[k + 1] = parr[k]; parr[j] = tmp; } }
}
-
Look. We'll take a simple bubble class:
void swap(int&a, int&b) { int t = a; a = b; b = t; }
void BubbleSort(int * a, int n)
{
for(int b = 0, e = n-1; b != e; ++b)
for(int k = e, j = k--; j != b; j = k--)
if (a[j] < a[k]) swap(a[j],a[k]);
}
Now, let's go with step 2 only on the first, third and further elements (indices 0, 2, ...):
void oddBubbleSort(int * a, int n)
{
for(int b = 0, e = ((n-1)/2)*2; b != e; b+=2)
for(int k = e-2, j = e; j != b; j = k, k -= 2)
if (a[j] < a[k]) swap(a[j],a[k]);
}
That's it. The point is to correctly calculate the last element:
I hope you write it for the honors now?
Example of work is here: https://ideone.com/13CCDo