<span style="font-size:18px;">//快速排序
public class QuickSort {

public static void main(String args[]){
int[] a = {4,2,1,6,3,6,0,-5,1,1};
qsort(a,0,a.length-1);
for(int i:a){
System.out.print(i);
}

}
//排序递归方法
public static void qsort(int[] source,int left,int right){
if(left>=right){
return;
}
int a = left;
int b = right;
int temp = source[left];//设置基准数

while(a<b){
//先从右开始向左寻找比数小的
while(source[b]>=temp && a<b){
b--;
}
//再从左开始向右寻找数大的
while(source[a]<=temp && a<b){
a++;
}
//交换两个数
if(a<b){
int t = source[a];
source[a] = source[b];
source[b] = t;
}
}


//寻找完一次开始交换基准数
source[left] = source[a];
source[a] = temp;
//开始递归
qsort(source,left,a-1);
qsort(source,a+1,right);
}
}</span>
<span style="font-size:18px;">结果输出:-5011123466</span>

更多相关文章

  1. 如何在Java中递归解压缩文件?
  2. 二分法查找递归方式()
  3. Java递归实现算24

随机推荐

  1. Android GridView 使用示例
  2. 从零开始--系统深入学习android(实践-让我
  3. android检测当前网络是否可用
  4. Android笔记_Linearlayout(线性布局)
  5. Android全屏显示的两种方式
  6. Android进程 与 消息模型
  7. android应用程序签名问题
  8. Android杂谈---Android几种预定义样式
  9. 开机关机动画工作流程
  10. Android Fastboot[wiki百科]