快速排序Quicksort Array in Java
2015-08-25 10:40
465 查看
Quicksort Array in Java
public class QuickSort { public static void main(String[] args) { int[] x = { 9, 2, 4, 7, 3, 7, 10 }; System.out.println(Arrays.toString(x)); int low = 0; int high = x.length - 1; quickSort(x, low, high); System.out.println(Arrays.toString(x)); } public static void quickSort(int[] arr, int low, int high) { if (arr == null || arr.length == 0) return; if (low >= high) return; // pick the pivot int middle = low + (high - low) / 2; int pivot = arr[middle]; // make left < pivot and right > pivot int i = low, j = high; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } // recursively sort two sub parts if (low < j) quickSort(arr, low, j); if (high > i) quickSort(arr, i, high); } }
相关文章推荐
- 如何设置UISwitch的大小
- win10 10525怎么升级?win10 Build 10525升级安装方法
- 关于orcad生成netlist时出现Conflicting values:错误
- 使用 GCD 自定义 UIImageView 增加异步下载图片的类别
- UITableviewController的创建以及关于多余分割线问题
- iOS UITextField的全解
- POJ 1503 && HDU 1047 Integer Inquiry(高精度)
- UGUI(九)UI粒子特效
- UGUI(八)UI节点制作
- 【php】include、include_once、require、require_once的区别
- UGUI(七)界面拖动和焦点界面
- QtGui.QComboBox控件常用函数
- Android Studio failed to find build tools revision 19.1.0 问题
- leetcode: (232) Implement Queue using Stacks
- UGUI(六)资源下载之Icon
- UGUI(五)动画系统
- UGUI(四)事件系统的封装
- 解决 ffmpeg yasm not found, use --disable-yasm for a crippled build
- Request(对象)
- UGUI(三)无限循环列表