【Algorithm】算法设计与分析(第二版)- 王红梅 - JAVA实现:分治法-快速排序

    科技2026-03-15  4

    package fourthChapter; public class QucikSort { public int Partition(int r[], int first, int end) { int i = first, j = end; while(i < j) { while (i < j && r[i ] <= r[j]) j--; if ( i < j) { int temp = r[i]; r[i] = r[j]; r[j] = temp; i++; } while (i < j && r[i] <= r[j]) i++; if ( i < j) { int temp = r[i]; r[i] = r[j]; r[j] = temp; j--; } } return i; } public void quickSort(int r[], int first, int end) { int pivot; if (first < end) { pivot = Partition(r, first, end); quickSort(r, first, pivot-1); quickSort(r, pivot+1, end); } } public static void main(String[] args) { // TODO Auto-generated method stub int[] r = {23, 13, 35, 6, 19, 50, 28}; QucikSort qSort = new QucikSort(); qSort.quickSort(r, 0, 6); for (int i = 0; i < 7; i++) { System.out.println(r[i]); } } }

     

    Processed: 0.019, SQL: 9