博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速排序*^____^*
阅读量:4473 次
发布时间:2019-06-08

本文共 1323 字,大约阅读时间需要 4 分钟。

package Test01;public class QuickSprt {    public static void main(String[] args) {        int[] arr = { 1, 3, 2, 9, 8, 7, 1, 0,0,2,3,3,1,0,101,100,1,1,001,100 }; // 要排序的数组            QuickSort(arr);        for (int i : arr) {            System.out.print(i+"  ");        }    }    public static void QuickSort(int[] arr) {        if (arr == null || arr.length < 2) {            return;        } else {            quickSort(arr, 0, arr.length - 1);        }    }    public static void quickSort(int[] arr, int L, int R) {        if (L < R) {            int[] p = partition(arr, L, R);            quickSort(arr, L, p[0] );            quickSort(arr, p[1] , R);                }    }    public static int[] partition(int[] arr, int L, int R) {        int num = arr[(int) (Math .random()*(R-L+1)+L)];  //随机快排        int less = L - 1;        int more = R + 1;        while (L < more) {            if (arr[L] < num) {                swap(arr, L++, ++less);            } else if (arr[L] > num) {                swap(arr, L, --more);            } else {                L++;            }        }        return new int[] { less, more };    }        public static void swap(int arr[], int a, int b) {        int temp = arr[a];        arr[a] = arr[b];        arr[b] = temp;    }}

转载于:https://www.cnblogs.com/superfly123/p/10530120.html

你可能感兴趣的文章
jsp中${}是EL表达式的常规表示方式
查看>>
GoldenGate常见问题及处理
查看>>
Android JNI学习(五)——Demo演示
查看>>
SSRS 呈现Barcode Free
查看>>
java快速排序引起的StackOverflowError异常
查看>>
泛函编程(35)-泛函Stream IO:IO处理过程-IO Process
查看>>
-XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
查看>>
mac 安装php redis扩展
查看>>
css3中Animation
查看>>
JS 判断是否是手机端并跳转操作
查看>>
最短路径问题(dijkstra-模板)
查看>>
c# 导出表格 api
查看>>
使用Android NDK以及JNI编写应用
查看>>
学习笔记之-php数组数据结构
查看>>
初学者--bootstrap(六)组件中的下拉菜单----在路上(10)
查看>>
QMetaObject::connectSlotsByName 总结
查看>>
app图标
查看>>
Android 微信支付步骤
查看>>
js操作table
查看>>
JQuery学习系列篇(一)
查看>>