求一个从小到大快速排序整形数组的完整方法(代码) 求一个从小到大快速排序整形数组的完整方法(代码),是否用到递归? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package com.ibm.lan.test; /** * 快速排序的Java实现 * @author Administrator */ public class QuickSort { public static void main(String[] args) { int[] a = { 5, 3, 7, 9, 1, 8, 2 }; quickSort(a); StringBuilder sb = new StringBuilder(); for(int i : a) { sb.append(i + ", "); } sb.delete(sb.lastIndexOf(","), sb.length()); System.out.println(sb); } /** * 对数组进行分割,随便取出一个数(temp),一般取第一个,进行分割后 * temp的左边将比temp小,右边比temp大 */ public static int partion(int[] a, int low, int high) { //取第一个点当作支点 int temp = a[low]; //一直循环直到low==high while(low < high) { //从右边开始扫描,一直到右边的值比temp(支点)小 while (low < high && temp <= a[high]) { high--; } if(low < high) { a[low++] = a[high]; } //再从左边开始扫描,一直到左边的值比temp(支点)大 while (low < high && temp >= a[low]) { low++; } if(low < high) { a[high--] = a[low]; } } a[low] = temp; return low; } public static void qSort(int[] a, int low, int high) { int p = partion(a, low, high); //对左边进行递归 if (p - 1 > low) { qSort(a, low, p - 1); } //对右边进行递归 if (p + 1 < high) { qSort(a, p + 1, high); } } public static void quickSort(int[] a) { qSort(a, 0, a.length - 1); } } /* 冒泡排序 思路: 1,相邻两个元素进行比较,满足条件,换位。 内循环第一次结束:最值出现在最大脚标位。 */ public static void sort_2(int[] arr) { for(int x=0; x<arr.length; x++) { for(int y = 0; y<arr.length-x-1; y++) { if(arr[y]>arr[y+1]) { int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; } } } } /* 选择排序 思路: 1,选择某一位置,先计算出最值。 内循环第一次结束,最值出现在,0脚标位。 */ public static void sort_1(int[] arr) { for(int x=0; x<arr.length; x++) { for(int y=x+1; y<arr.length; y++) { if(arr[x]>arr[y]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } } 排序的方法太多了,排序的时间效率不一样。就是平均时间效率一样的排序,使用的场合也不一样。楼主指名要快速排序。你的那两个排序时间效率不高,是O(n^2),但比较常见。快速排序的时间效率是O(n*log2n)。 Java JDBC存数据库,字符串数组问题 虚心请教! 又有新问题了!大家帮帮忙吧! jacob 调用ocx问题!!!! 关于图像!生成缩略图! 如何显示只保留两位小数(如2。34234——》2。34),等待? 请教各位大虾,你们用什么来做网站的压力测试?? extends一题???? 在java中如何校验一个EMAIL格式的正确性 请问我该怎么走我的'主线'和'支线'? 关于构造方法的问题! 怎么自学JAVA?
package com.ibm.lan.test;
/**
* 快速排序的Java实现
* @author Administrator
*/
public class QuickSort {
public static void main(String[] args) {
int[] a = { 5, 3, 7, 9, 1, 8, 2 };
quickSort(a);
StringBuilder sb = new StringBuilder();
for(int i : a) {
sb.append(i + ", ");
}
sb.delete(sb.lastIndexOf(","), sb.length());
System.out.println(sb);
}
/**
* 对数组进行分割,随便取出一个数(temp),一般取第一个,进行分割后
* temp的左边将比temp小,右边比temp大
*/
public static int partion(int[] a, int low, int high) {
//取第一个点当作支点
int temp = a[low];
//一直循环直到low==high
while(low < high) {
//从右边开始扫描,一直到右边的值比temp(支点)小
while (low < high && temp <= a[high]) {
high--;
}
if(low < high) {
a[low++] = a[high];
}
//再从左边开始扫描,一直到左边的值比temp(支点)大
while (low < high && temp >= a[low]) {
low++;
}
if(low < high) {
a[high--] = a[low];
}
}
a[low] = temp;
return low;
}
public static void qSort(int[] a, int low, int high) {
int p = partion(a, low, high);
//对左边进行递归
if (p - 1 > low) {
qSort(a, low, p - 1);
}
//对右边进行递归
if (p + 1 < high) {
qSort(a, p + 1, high);
}
}
public static void quickSort(int[] a) {
qSort(a, 0, a.length - 1);
}
}
冒泡排序
思路:
1,相邻两个元素进行比较,满足条件,换位。
内循环第一次结束:最值出现在最大脚标位。
*/ public static void sort_2(int[] arr)
{
for(int x=0; x<arr.length; x++)
{
for(int y = 0; y<arr.length-x-1; y++)
{
if(arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
/*
选择排序
思路:
1,选择某一位置,先计算出最值。
内循环第一次结束,最值出现在,0脚标位。 */
public static void sort_1(int[] arr)
{
for(int x=0; x<arr.length; x++)
{
for(int y=x+1; y<arr.length; y++)
{
if(arr[x]>arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
排序的方法太多了,排序的时间效率不一样。就是平均时间效率一样的排序,使用的场合也不一样。楼主指名要快速排序。你的那两个排序时间效率不高,是O(n^2),但比较常见。快速排序的时间效率是O(n*log2n)。