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

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

Quick Sort not working as expected in java

问题

以下是翻译好的部分:

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

QuickSort.java

  1. public class QuickSort {
  2. public void quicksort(int[] arr, int lb, int ub) {
  3. // up=upper bound
  4. // lb=lower bound
  5. if (lb < ub) {
  6. int loc = partition(arr, lb, ub);
  7. quicksort(arr, lb, loc - 1);
  8. quicksort(arr, loc + 1, ub);
  9. }
  10. }
  11. public int partition(int[] arr, int lb, int ub) {
  12. int pivot = arr[lb];
  13. int start = lb;
  14. int end = ub - 1;
  15. while (start < end) {
  16. while (arr[start] <= pivot && start < arr.length - 1) {
  17. start++;
  18. }
  19. while (arr[end] > pivot) {
  20. end--;
  21. }
  22. if (start < end) {
  23. swap(start, end, arr);
  24. }
  25. }
  26. swap(lb, end, arr); // 用末尾元素交换中心值(pivot)
  27. return end;
  28. }
  29. public void swap(int start, int end, int[] arr) {
  30. int temp = arr[start];
  31. arr[start] = arr[end];
  32. arr[end] = temp;
  33. }
  34. }

Main class:

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

我的输出结果是:

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

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

  1. public class QuickSort {
  2. public void quicksort(int[] arr, int lb, int ub) {
  3. //up=upper bound
  4. //lb=lower bound
  5. if(lb&lt;ub) {
  6. int loc=partition(arr,lb,ub);
  7. quicksort(arr,lb,loc-1);
  8. quicksort(arr,loc+1,ub);
  9. }
  10. }
  11. public int partition(int[] arr,int lb,int ub) {
  12. int pivot=arr[lb];
  13. int start=lb;
  14. int end=ub-1;
  15. while(start&lt;end) {
  16. while(arr[start]&lt;=pivot &amp;&amp; start&lt;arr.length-1) {
  17. start++;
  18. }
  19. while(arr[end]&gt;pivot) {
  20. end--;
  21. }
  22. if(start&lt;end) {
  23. swap(start,end,arr);
  24. }
  25. }
  26. swap(lb,end,arr); //swapping pivot value with end
  27. return end;
  28. }
  29. public void swap(int start,int end,int[] arr) {
  30. // System.out.println(end+&quot; &quot;+pivot);
  31. int temp=arr[start];
  32. arr[start]=arr[end];
  33. arr[end]=temp;
  34. }
  35. }

Main class:

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

My output coming is:

  1. [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

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

  1. public static void sort(int arr[], int lb, int ub) {
  2. if (lb < ub) {
  3. int part = partition(arr, lb, ub);
  4. // 对分区进行排序
  5. sort(arr, lb, part - 1);
  6. sort(arr, part + 1, ub);
  7. }
  8. }
  9. public static void displayArray(int[] arr) {
  10. System.out.print("[ ");
  11. for (int x :
  12. arr) {
  13. System.out.print(x + " ");
  14. }
  15. System.out.print("]");
  16. }
  17. public static void main(String[] args) {
  18. int[] arr = {7, 6, 10, 5, 9, 2, 1, 15, 7};
  19. int len = arr.length;
  20. displayArray(arr);
  21. sort(arr, 0, len-1);
  22. System.out.println();
  23. displayArray(arr);
  24. }
英文:

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:

  1. public static void sort(int arr[], int lb, int ub) {
  2. if (lb &lt; ub) {
  3. int part = partition(arr, lb, ub);
  4. // Sorts the partitions
  5. sort(arr, lb, part - 1);
  6. sort(arr, part + 1, ub);
  7. }
  8. }
  9. public static void displayArray(int[] arr) {
  10. System.out.print(&quot;[ &quot;);
  11. for (int x :
  12. arr) {
  13. System.out.print(x + &quot; &quot;);
  14. }
  15. System.out.print(&quot;]&quot;);
  16. }
  17. public static void main(String[] args) {
  18. int[] arr = {7, 6, 10, 5, 9, 2, 1, 15, 7};
  19. int len = arr.length;
  20. displayArray(arr);
  21. sort(arr, 0, len-1);
  22. System.out.println();
  23. displayArray(arr);
  24. }

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:

确定