Java 快速排序算法

编程入门 行业动态 更新时间:2024-10-18 23:25:18
本文介绍了Java 快速排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试学习快速排序算法,这是我目前的代码:

I am trying to learn Quick Sort algorithm and this is my code so far:

import java.util.Arrays; public class JavaFiddle { static int[] myArray = new int[]{35, 12, 25, 1, 5, 33, 56}; public static void QuickSort(int[] array) { QuickSort(array, 0, array.length - 1); } public static void QuickSort(int[] array, int left, int right) { if (left < right) { int pivot = left + ((right - left) / 2); int index = partition(array, left, right, pivot); QuickSort(array, left, index - 1); QuickSort(array, index + 1, right); } } public static int partition(int[] array, int left, int right, int pivot) { while (left < right) { while (array[left] < pivot) { left++; } while (array[right] > pivot) { right--; } if (left < right) { swap(array, left, right); left++; right--; } } return left; } public static void swap(int[] array, int left, int right) { int temp = array[left]; array[left] = array[right]; array[right] = temp; } public static void main(String[] args) { System.out.println(Arrays.toString(myArray)); QuickSort(myArray); System.out.println(Arrays.toString(myArray)); } }

然而,这段代码给了我一个错误的结果:

However, this code gives me an incorrect result:

[35, 12, 25, 1, 5, 33, 56] - before sort [1, 12, 25, 35, 5, 33, 56] - after sort

我这里有什么问题?我找不到逻辑上的缺陷.

What do I have wrong here? I cannot find the flaw in the logic.

推荐答案

此处出现多个错误,您在 main 方法中定义了枢轴,但快速排序算法会将枢轴从右侧元素交换到中间元素.您可以在 while 循环中编辑 while 循环中的左右值,这会导致左右值比枢轴小/高,并跳过一些交换.这是没有您的 while { while { ... } } 和正确的枢轴(从右到中间)

Multiple errors here, You define pivot in main method, but quick sort algorithm will swap pivot from right element to the middle. You edit left and right values in your while loop in a while loop, which result right and left to be smaller/taller than your pivot and skipping some swaps. Here's the correct implementation without your while { while { ... } } and a correct pivot (from right to middle)

import java.util.Arrays; public class Main { static int[] myArray = new int[] {35, 12, 25, 1, 5, 56, 33}; public static void QuickSort(int[] array) { QuickSort(array, 0, array.length - 1); } public static void QuickSort(int[] array, int left, int right){ if(left < right) { int index = partition(array, left, right); QuickSort(array, left, index - 1); QuickSort(array, index + 1, right); } } public static int partition(int[] array, int left, int right){ int pivot = array[right]; int first = left - 1; for (int j = left; j <= right - 1; j++) { if(array[j] < pivot) { first ++; swap(array, first, j); } } swap(array, first + 1, right); return first + 1; } public static void swap(int[] array, int left, int right) { int temp = array[left]; array[left] = array[right]; array[right] = temp; } public static void main(String[] args) { System.out.println(Arrays.toString(myArray)); QuickSort(myArray); System.out.println(Arrays.toString(myArray)); } }

你还比较了 pivot 和 array[...] 的索引,array[...] 是一个值

Also you compare pivot which is an index with array[...] which is a value

更多推荐

Java 快速排序算法

本文发布于:2023-11-30 19:52:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   快速   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!