开发者

Quicksort algorithm program in Java

I'm trying to implement QuickSort algorithm program in Java, but I'm getting incorrect answer.

public class QuickSort {

    public static void main(String[] args){
        int arr[]={12,34,22,64,34,33,23,64,33};
        int i=0;
        int j=arr.length;
        while(i<j){
            i=quickSort(arr,i,i+1,j-1);

        }   
        for(i=0;i<arr.length;i++)
            System开发者_开发知识库.out.print(arr[i]+" ");
    }

    public static int quickSort(int arr[],int pivot,int i,int j){

        if(i>j) {           
            swap(arr,pivot,j);
            return i;
        }

        while(i<arr.length&&arr[i]<=arr[pivot]) {
            i++;
        }

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

        return quickSort(arr,pivot,i,j);

    }   
    public static void swap(int[] arr,int i,int j) {
        int temp;
        temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }

}

The above program giving me the output as: 12 23 22 33 34 33 64 34 64

Could anyone please tell me how can I get my desire result?


The problem is that this is not really how quicksort works. Quicksort is a recursive algorithm that should only be called once from outside of itself. The idea is that at each iteration, you partition the array into two halves - the left half contains all elements less than the pivot, and the right half contains all elements greater than / equal to the pivot. Then you quicksort the two halves, and finally put the pivot in the middle.

If the side that you are quicksorting is less than 3 elements long, you can just swap the two elements or leave them, and that part of the array is done.

But it doesn't look like your code is doing that at all - you are calling Quicksort 6 times from your client, and within the quicksort function you are making at most one swap. So this is not a case where someone is going to be able to look at your code and debug it by telling you to move a swap or something. You need to revisit your logic.

Check out the Wikipedia diagram for a visual example of what is supposed to happen in a single iteration:

http://en.wikipedia.org/wiki/File:Partition_example.svg


There are open source implementations of quicksort in Apache Harmony and Apache Mahout, probably amongst many others. You can read them.


public static int partition(int[] a, int p, int r){

        int i=p,j=r,pivot=a[r];

        while(i<j){

            while(i<r && a[i] <= pivot){
                i++;
            }

            while(j>p && a[j]>pivot){ 
                j--;
            }

            if(i<j){
                swap(a, i, j);
            }           
        }   
        return j;
    }

    public static void quickSort(int[] a, int p, int r){
        if(p<r){
            int q=partition(a, p, r);

            if(p==q){
                quickSort(a, p+1, r);
            }else if(q==r){
                quickSort(a, p, r-1);
            }else {
                quickSort(a, p, q);
                quickSort(a, q+1, r);
            }

        }
    }

    public static void swap(int[] a, int p1, int p2){
        int temp=a[p1];
        a[p1]=a[p2];
        a[p2]=temp;
    }


here is a quicksort algorithm

package drawFramePackage;
    import java.awt.geom.AffineTransform;
    import java.util.ArrayList;
    import java.util.ListIterator;
    import java.util.Random;
    public class QuicksortAlgorithm {
        ArrayList<AffineTransform> affs;
        ListIterator<AffineTransform> li;
        Integer count, count2;
        /**
         * @param args
         */
        public static void main(String[] args) {
            new QuicksortAlgorithm();
        }
        public QuicksortAlgorithm(){
            count = new Integer(0);
            count2 = new Integer(1);
            affs = new ArrayList<AffineTransform>();
            for (int i = 0; i <= 128; i++){
                affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
            }
            affs = arrangeNumbers(affs);
            printNumbers();
        }
        public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list){
            while (list.size() > 1 && count != list.size() - 1){
                if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()){
                    list.add(count, list.get(count2));
                    list.remove(count2 + 1);
                }
                if (count2 == list.size() - 1){
                    count++;
                    count2 = count + 1;
                }
                else{
                count2++;
                }
            }
            return list;
        }
        public void printNumbers(){
            li = affs.listIterator();
            while (li.hasNext()){
                System.out.println(li.next());
            }
        }
    }

also available with description at nathan's computer knowledge with a description [code] [/code] ``


Your loop is not working properly. Refer the code which is solve your problem about Quick Sort

static void quickSort (int[] numbers, int low, int high)
{
    int i=low;
    int j=high;
    int temp;
    int middle=numbers[(low+high)/2];

    while (i<j) {

        while (numbers[i]<middle) {
            i++;
        }

        while (numbers[j]>middle) {
            j--;
        }

        if (i<=j) {
            temp=numbers[i];
            numbers[i]=numbers[j];
            numbers[j]=temp;
            i++;
            j--;
        }
    }

    if (low<j) {
        quickSort(numbers, low, j);
    }

    if (i<high) {
        quickSort(numbers, i, high);
    }
}

Refer Quick sort.


Please find comprehensive working code for quick sort algorithm implemented in Java here,

http://tech.bragboy.com/2010/01/quick-sort-in-java.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜