Quicksort给了我两组不同的数字

时间:2016-10-10 13:56:16

标签: c arrays function sorting quicksort

您好我已经编写了一个测试快速排序的快速程序,看看我是否完全了解它,但它似乎在排序不同的数组。我假设我需要指针类型?它看起来像排序数组随机化了0到0。代码有问题吗?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 150

void quickSort(int array[], int low, int high); // sorting algorithm
int split(int array[], int low, int high); // spit the array

int main(void)

{
    int array[MAX];
    int i;
    srand((unsigned)time(NULL));

    for (i=0;i<MAX;i++)
    {
        array[i] = rand()%MAX;
        printf("#%d:[%d]\n", i+1, array[i]);
        quickSort(array, 0, MAX-1);
    }
    printf("The sorted order:\n");
    for(i=0;i<MAX;i++)
    {
        printf("#%d:[%d]\n", i+1,array[i]);
    }


    return 0;
}
// function

void quickSort(int array[], int low, int high) // sorting algorithm
{
    int middle;
    if (low >= high)
    {
        return;
    }
    middle = split(array, low, high);
    quickSort(array, low, middle-1);
    quickSort(array, middle+1, high);
}
int split(int array[], int low, int high)
{
    int partElement = array[low];

    for(;;)
    {
        while (low < high && partElement <= array[high])
        {
            high--;

        }
        if (low>= high)
        {
            break;
        }
        array[low++]=array[high];

        while (low < high && array[low] <= partElement)
        {
            low++;
        }
        if (low >= high)
        {
            break;
        }
        array[high--] = array[low];
    }
    array[high] = partElement;
    return high;
}

0 个答案:

没有答案