java 实现的快排

    技术2022-06-10  55

    // zhangs.QuickSort1.java /** *@author zhangs *从数组的1号单元开始计 ,0号单元用作枢轴,不可用,排序默认为int数组 */ package zhangs; public class QuickSort1{ //一趟快排 private static int Partition(int data[], int low, int high){ //设置枢轴 data[0] = data[low]; while(low < high){ while(low < high && data[high] >= data[0]) --high; data[low] = data[high]; while(low < high && data[low] <= data[0]) ++low; data[high] = data[low]; } data[low] = data[0]; //返回枢轴的位置 return low; }//Partition private static void QSort(int data[], int low, int high){ if(low < high){ int pivotloc = Partition(data, low, high); QSort(data, low, pivotloc-1); QSort(data, pivotloc+1, high); } }//QSort public static void main(String[] args){ //注意0号单元仅用于占位 int data[] = {0,3,4,8,9,5,6,1,7,2,0}; QSort(data, 1, data.length-1); for(int i=1;i<data.length;i++){ System.out.print(data[i] + " "); } } }


    最新回复(0)