快速排序方法
2015-01-26 00:51
267 查看
运行效果:
=================================================
代码部分:
=================================================
/hello_test/src/com/b510/test/QuickStore.java
=================================================
代码部分:
=================================================
/hello_test/src/com/b510/test/QuickStore.java
1 /** 2 * 3 */ 4 package com.b510.test; 5 6 /** 7 * 快速排序 8 * @author <a href="mailto:hongtenzone@foxmail.com">hongten</a> 9 * @date 2013-3-1 10 * 更多信息:<a href="http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html">more>></a> 11 */ 12 public class QuickStore { 13 /** 14 * 创建随机数组 15 * @param min 数组的最小值 16 * @param max 数组的最大值 17 * @param length 数组的长度 18 * @return 随机数组 19 */ 20 public static int[] createArray(int min, int max, int length) { 21 int[] array = new int[length]; 22 for (int i = 0; i < length; i++) { 23 array[i] = min + (int) (Math.random() * (max - min + 1)); 24 System.out.println("随机数 :array[" + i + "] = " + array[i]); 25 } 26 return array; 27 } 28 29 /** 30 * 快速排序 31 * @param array 源数组 32 * @param l 33 * @param r 34 * @return 35 */ 36 public static int[] quickStore(int[] array, int l, int r) { 37 if (l < r) { 38 int i = l, j = r, x = array[l]; 39 while (i < j) { 40 while (i < j && array[j] >= x) 41 // 从右向左找第一个小于x的数 42 j--; 43 if (i < j) 44 array[i++] = array[j]; 45 46 while (i < j && array[i] < x) 47 // 从左向右找第一个大于等于x的数 48 i++; 49 if (i < j) 50 array[j--] = array[i]; 51 } 52 array[i] = x; 53 quickStore(array, l, i - 1); // 递归调用 54 quickStore(array, i + 1, r); 55 } 56 return array; 57 } 58 59 /** 60 * 显示数组信息 61 * @param array 62 */ 63 public static void showArray(int[] array) { 64 System.out.println("排序后...."); 65 for (int i = 0; i < array.length; i++) { 66 System.out.println("array[" + i + "] = " + array[i]); 67 } 68 } 69 70 /** 71 * test 72 * @param args 73 */ 74 public static void main(String[] args) { 75 int[] array = createArray(12, 50, 6); 76 int[] newArray = quickStore(array, 0, array.length - 1); 77 showArray(newArray); 78 } 79 }
相关文章推荐
- 快速排序List的通用方法
- c#中快速排序的方法(转载)
- 快速掌握PHP多维数组排序方法
- 排序都有哪几种方法?请列举。用JAVA实现一个快速排序。
- 快速排序-常见中轴(主元pivot)选择方法及实现代码(末位/随机/三数中值/..)
- 快速排序方法Java实现与分析
- 快速排序 改进快排的方法
- 排序的几种方法(合并,快速,冒泡排序)
- 快速排序的库函数qsort与sort的使用方法
- 快速排序的两种改进方法算法及topK问题求解
- 点击Ehlib列标题的快速排序方法---最新(转帖)
- 快速排序List的通用方法
- 快速排序{不同方法寻找分割元素}
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
- 4、 排序有哪几种方法?请列举。并用JAVA实现一个快速排序.
- 我的C语言快速排序方法
- 最容易理解的快速排序方法和程序
- 算法入门-快速排序-基本快速排序方法
- 经典快速排序,linux纯C实现。注意swap方法,并且这个算法还需再敲写15篇。
- 快速排序 改进快排的方法