为什么我的快速排序算法不能用于重复元素?

时间:2016-02-04 16:52:09

标签: c sorting quicksort

我在C中实现了一个快速排序算法来排序数组的元素。它适用于所有情况,除非数组有两个或更多相等的元素。我一直在尝试修复它并且一直在调试它但是当有重复的元素时我似乎无法让它工作。

我很感激有关如何更改代码以便为重复元素工作的任何帮助。

#include <stdio.h>
#include <stdlib.h>

//Random Array Length
#define L 10
#define MAX 100

void smarter_sort(int[],int,int);
void swap(int[],int,int);
int choose_piv(int[],int,int);

int main(){

    int i, a[L];

    //Generate an array of random numbers
    for(i=0; i<L; i++)
        a[i]= rand() % (MAX+1);

    //Unsorted Array
    printf("\nUnsorted array: ");
    for(i=0; i<L; i++)
            printf("%d    ", a[i]);

    //Sorted Array
    smarter_sort(a,0,L-1);
    printf("\nSorted array:   ");
            for(i=0; i<L; i++)
                printf("%d    ", a[i]);
    return 0;
}

//Recursively defined quicksort (Pseudo-code listing 1.9)
void smarter_sort(int a[], int l, int r){
    if(r > l){
        int piv = choose_piv(a, l, r);
        smarter_sort(a, l, piv-1);
        smarter_sort(a, piv+1, r);
    }
}

//Swap Elements
void swap(int a[], int i, int j){
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
}

//Choosing the pivot (pseudo-code listing 1.10)
int choose_piv(int a[], int l, int r){
    //defining pointers and pivot
    int pL = l, pR = r;
    int piv = l;

    while (pL < pR){
        //finding the first left element greater than piv
        while(a[pL] < a[piv])
            pL++;

        //finding the first right element greater than piv
        while(a[pR] > a[piv])
            pR--;

        //swapping if the pointers do not overlap
        if(pL < pR)
            swap(a, pL, pR);

        if(a[pL]==a[piv]||a[pR]==a[piv]){
            pL++;
            pR--;
        }
    }
    //swapping and returning the rightmost pointer as the pivot
    swap(a, piv, pR);
    return pR;
}

1 个答案:

答案 0 :(得分:1)

这是您的代码修改,以便即使包含相同元素的数组也能正常工作:

 #include <stdio.h>
 #include <stdlib.h>

 //Random Array Length
 #define L 10
 #define MAX 100

 void smarter_sort(int[],int,int);
 void swap(int[],int,int);


 int main(){

    int i, a[L];

    //Generate an array of random numbers
    for(i=0; i<L; i++)
       a[i]= rand() % (MAX+1);

    //Unsorted Array
    printf("\nUnsorted array: ");
    for(i=0; i<L; i++)
        printf("%d    ", a[i]);

    //Sorted Array
    smarter_sort(a,0,L-1);
    printf("\nSorted array:   ");
        for(i=0; i<L; i++)
            printf("%d    ", a[i]);
    return 0;
 }

 //Recursively defined quicksort (Pseudo-code listing 1.9)
 void smarter_sort(int arr[], int left, int right) {
      int i = left, j = right;
      int pivot = arr[(left + right) / 2];


      while (i <= j) {
           while (arr[i] <pivot)
                i++;
           while (arr[j]>pivot)
                j--;
           if (i <= j) {
                swap(arr,i,j);
                i++;
                j--;
           }
     };


    if (left < j)
          smarter_sort(arr, left, j);
    if (i < right)
          smarter_sort(arr, i, right);
 }


//Swap Elements
void swap(int a[], int i, int j){
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
}