I have a piece of code like this:
#include <iostream>
using namespace std;
int shellSortAscendent(int arr[], int N)
{
for (int gap = N / 2; gap > 0; gap /= 2) {
for (int i = gap; i < N; i += 1) {
//sort sub lists created by applying gap
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
Here's how I tried to do it :
int shellSortAscendent(int arr[], int N)
{
int gap = N / 2;
do {
gap /= 2;
int i = gap;
do {
int temp = arr[i];
int j;
j = i;
i += 1;
do {
j -= gap;
} while (j >= gap && arr[j - gap] > temp);
arr[j] = arr[j - gap];
arr[j] = temp;
} while (i < N);
} while (gap > 0);
return 0;
}
But the result is incorrectly sorted and some numbers are duplicated. Please help me understand the reason