lundi 25 septembre 2017

Shell Sort of Array Filled with Random Integers, c++

having a lot of trouble trying to implement a shell sort on an array filled with random numbers (using c_time). The size of the array is given by user input but is never less than ten, and there is no limit to how large the numbers are.

I am fairly certain that the issue is the swapping part of the algorithm because even though I have chosen a proper gap, my method of doing so has left me fairly confused as to how to reposition the intended values. Here is my code:

void sort(T a[], size_t n, U cmp) {                                             //init gap, counter, and pointer variables
int gap, j;
int z = a[n-1];
int b = a[n - 3];
int c = a[n / 2];
int d = a[1];
static int gap_sequence[] = { z,b,c,d };

for (int i = 1; gap = gap_sequence[i]; i++)
    std::cout << gap_sequence[i] << "  ";
     {                                  //gap is various elements in array
    for (int i = 0; i < n; i++) {   
        for (j = 0; gap < a[j]; j++) {  //loop means the swap only happens if j is bigger than the selected gap
            if (j == 0) {
                std::swap(a[j], a[n-1]);
            }
            else
            std::swap(a[j], a[j-1]);
        }
    }
}

}

I am also thinking that because the gaps are in an unsorted order the sort isn't really sorting in relation to anything. But if so, how do I chose my gap values?

Like I said, I'm not sure how to go about finding the index of the gap again. Maybe this method is completely wrong and I'm an idiot, I don't know. I am very much still in the learning process and am all alone here. If anyone could help me out I'd very much appreciate it. If you want my testing algorithm then please let me know.




Aucun commentaire:

Enregistrer un commentaire