快速排序在Java中未按预期工作。

huangapple go评论63阅读模式
英文:

Quick Sort not working as expected in java

问题

以下是翻译好的部分:

我学习了关于快速排序的理论。我尝试在Java中实现这个算法,但我发现并没有所有的数字都被排序。我无法指出我在哪里犯了错误。

QuickSort.java

public class QuickSort {
    
    public void quicksort(int[] arr, int lb, int ub) {
        // up=upper bound
        // lb=lower bound
        
        if (lb < ub) {
            int loc = partition(arr, lb, ub);
            quicksort(arr, lb, loc - 1);
            quicksort(arr, loc + 1, ub);
        }
        
    }
    
    public int partition(int[] arr, int lb, int ub) {
        int pivot = arr[lb];
        int start = lb;
        int end = ub - 1;
        
        while (start < end) {
            while (arr[start] <= pivot && start < arr.length - 1) {
                start++;
            }
            
            while (arr[end] > pivot) {
                end--;
            }
            
            if (start < end) {
                swap(start, end, arr);
            }
        }
        
        swap(lb, end, arr); // 用末尾元素交换中心值(pivot)
        return end;
    }
    
    public void swap(int start, int end, int[] arr) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }

}

Main class:

import java.util.Arrays;

public class Program {
    
    public static void main(String[] args) {
        int[] arr = {7, 6, 10, 5, 9, 2, 1, 15, 7};
        QuickSort q = new QuickSort();
        q.quicksort(arr, 0, arr.length);
        System.out.println(Arrays.toString(arr));
    }

}

我的输出结果是:

[2, 5, 6, 7, 1, 7, 9, 10, 15]
我重新检查了我的代码并进行了一些调试,但在第一次通过时它运行得很好。我不知道,在哪个点上出错了?
英文:

I learned the theory about QuickSort. I tried to implement this algorithm in java but I am not seeing all the numbers are being sorted.I am not able to point out what mistake I am making.

QuickSort.java

public class QuickSort {

	public void quicksort(int[] arr, int lb, int ub) {
		//up=upper bound
		//lb=lower bound
		
		if(lb&lt;ub) {
		  int loc=partition(arr,lb,ub);
		  quicksort(arr,lb,loc-1);
		  quicksort(arr,loc+1,ub);
		}
		
	}
	
	public int partition(int[] arr,int lb,int ub) {
		int pivot=arr[lb];
		int start=lb;
		int end=ub-1;
		
		while(start&lt;end) {
			while(arr[start]&lt;=pivot &amp;&amp; start&lt;arr.length-1) {
				start++;
				}
		
		while(arr[end]&gt;pivot) {
			end--;
		}
		
		if(start&lt;end) {
			swap(start,end,arr);
		}
		}
		
		swap(lb,end,arr); //swapping pivot value with end
		return end;
	}
	
	  public void swap(int start,int end,int[] arr) {
		 // System.out.println(end+&quot; &quot;+pivot);
		  int temp=arr[start];
		  arr[start]=arr[end];
		  arr[end]=temp;
	  }

}

Main class:

import java.util.Arrays;

public class Program {
	
	public static void main(String[] args) {
		int[] arr= {7,6,10,5,9,2,1,15,7};
		QuickSort q=new QuickSort();
	  q.quicksort(arr,0,arr.length);
	  System.out.println(Arrays.toString(arr));
	}

}

My output coming is:

[2, 5, 6, 7, 1, 7, 9, 10, 15]

I rechecked my code and done some debugging but for first pass it is working fine.I dont know,at what point it is being wrong?

答案1

得分: 1

这位仁兄恰好做了你要寻找的内容 快速排序在Java中未按预期工作。 在这里查看他的 partition 函数:

https://stackoverflow.com/questions/13543843/random-pivot-quicksort-in-java

以下是你可以为辅助函数做的事情:

public static void sort(int arr[], int lb, int ub) {
    if (lb < ub) {

        int part = partition(arr, lb, ub);

        // 对分区进行排序
        sort(arr, lb, part - 1);
        sort(arr, part + 1, ub);
    }
}

public static void displayArray(int[] arr) {
    System.out.print("[ ");
    for (int x :
            arr) {
        System.out.print(x + " ");
    }
    System.out.print("]");
}

public static void main(String[] args) {

    int[] arr = {7, 6, 10, 5, 9, 2, 1, 15, 7};
    int len = arr.length;

    displayArray(arr);
    sort(arr, 0, len-1);
    System.out.println();
    displayArray(arr);

}
英文:

This dude made exactly what you are looking for 快速排序在Java中未按预期工作。 take a look at his
partition function here:

https://stackoverflow.com/questions/13543843/random-pivot-quicksort-in-java

Here is what you can do for the helper functions:

public static void sort(int arr[], int lb, int ub) {
    if (lb &lt; ub) {

        int part = partition(arr, lb, ub);

        // Sorts the partitions
        sort(arr, lb, part - 1);
        sort(arr, part + 1, ub);
    }
}

public static void displayArray(int[] arr) {
    System.out.print(&quot;[ &quot;);
    for (int x :
            arr) {
        System.out.print(x + &quot; &quot;);
    }
    System.out.print(&quot;]&quot;);
}

public static void main(String[] args) {

    int[] arr = {7, 6, 10, 5, 9, 2, 1, 15, 7};
    int len = arr.length;

    displayArray(arr);
    sort(arr, 0, len-1);
    System.out.println();
    displayArray(arr);

}

huangapple
  • 本文由 发表于 2020年9月13日 22:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63872013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定