Невозможно правильно распечатать матрицу быстрой сортировки - PullRequest
2 голосов
/ 05 ноября 2019

Я не могу правильно распечатать шаги, заданные алгоритмом быстрой сортировки. Эта функция работает, когда N = 1, что означает, что значения в матрице повторяются только один раз, но при N> 1 она не печатается должным образом.

Это для программы, которую я пишу, для представления различныхАлгоритмы сортировки выполняются с помощью консоли.

void quicksort(int *target, int left, int right) {

   char charac[MAX*N][MAX]; // this is the array I am going to print on screen

    if(left >= right)
        return;

    int i = left, j = right;     
    int tmp, pivot = target[i];
    for(;;) {

        while(target[i] < pivot)
            i++;
        while(pivot < target[j])
            j--;

        if(i >= j)
            break;

        tmp = target[i]; 
        target[i] = target[j]; 
        target[j] = tmp;

        i++; j--; //normal quicksort until here

        for(int k=0;k<MAX*N;k++){ /*Here begin two loops that put the cursor on a certain point on the screen and then gives values to char[][]*/

            GotoXY(k+30,5);
            for(int l=0;l<MAX;l++){
                If(l<target[k]) // if the value of the given vector is lower than l writes a space
                    charac[k][l]=32;
                 else
                     charac [k][l]=219; // else a block
                 printf("%c ",charac[k][l]); // print the block or space
                 GotoXY(k+35,5+l); // go to the next point of the column
            }
            printf("\n");
        }
        system("cls"); // clear the screen for the next graph
    }
    quicksort(target, left, i-1);
    quicksort(target, j+1, right);
}

Я рассчитываю правильно отсортировать значения, даже если они повторяются, но результат даже не близок к этому.

Вот для N = 1 в начале:

alt text

В конце:

alt text

При N = 2 оно генерируется правильно:

alt text

Но это результат:

alt text

Это даже близко не к чему-то хорошему.

Спасибо за ваше время.

...