快速排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速排序相关的知识,希望对你有一定的参考价值。
public class QuickSort { public static int getMidIndex(int[] list, int low, int high) { int temp = list[low]; while(low < high) { while(low<high && list[high] >= temp) { high --; } list[low] = list[high]; while(low<high && list[low] <= temp) { low ++; } list[high] = list[low]; } list[low] = temp; return low; } public static void quickSort(int[] list, int low, int high) { //递归一定要注意递归结束的条件,否则就是死循环 if(low < high) { int middle = getMidIndex(list, low, high); quickSort(list, low, middle-1); quickSort(list, middle+1, high); } } public static void main(String[] args) { int[] list = { 8, 23,6, 6,7,7,3,4,0,9, 17}; quickSort(list, 0, list.length-1); for(int i = 0 ; i < list.length; i++) { System.out.print(list[i] + " "); } } }
以上是关于快速排序的主要内容,如果未能解决你的问题,请参考以下文章