Shell sort with an array of gap sequences

I'm trying to implement a shell sort using an array of gap sequences, and I'm having trouble getting the array to work with the shell sort code. It compiles, but the array that I have does not seem to be used for the gap sequences. It seems to only use the size of the array(16), not the elements of the array. If anyone could point me in the right direction as to what I can do to make the contents of the array work with the shell sort, I would very much appreciate it. Thank you.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

template <typename Comparable>
unsigned long shellsort( vector<Comparable> & a )
{
unsigned long counter = 0;
int const size = 16;
int array[size]= {65535, 32767, 16383, 8191, 4095, 2047, 1023, 511,
255, 127, 63, 31, 15, 7, 3, 1};

for( unsigned int gap = size / 2; gap > 0; gap /= 2 )
for( unsigned int i = gap; i < a[size]; i++ )
{
Comparable tmp = a[ i ];
unsigned int j = i;

for( ; j >= gap ; j -= gap )
{
counter++;
if (!(tmp < a[ j - gap ])) break;
a[ j ] = a[ j - gap ];
}
a[ j ] = tmp;
}
return counter;
}
int main ()
{
vector<int> rnumbers;
clock_t start, finish;
double duration;

srand(42);
start = clock();

cout << "Sorting " << N << " numbers." << endl;

for (int i=0; i<N; i++)
rnumbers.push_back (rand ());

finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;

cout << "Initializing vector: " << duration << " seconds." << endl;

start = clock();

unsigned long comp = shellsort (rnumbers);

finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;

cout << "Sorting vector: " << duration << " seconds." << endl;
cout << "Number of comparisons: " << comp << endl;

system("PAUSE");
return 0;
}
You're not using a.size(). Take a look at the Pseudocode:

https://en.wikipedia.org/wiki/Shellsort

Note that n in the pseudo code is actually a.size()
Topic archived. No new replies allowed.