java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料
保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。
1package Sort;
/*
* 快速<a href="http://www.cfei.net/archives/tag/%e6%8e%92%e5%ba%8f" title="浏览关于“排序”的文章" target="_blank" class="tag_link">排序</a>
*/
public class Kuaisu {
public int num=1;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] list={34,3,53,2,23,7,14,10};
Kuaisu qs=new Kuaisu();
qs.quick(list);
System.out.println();
System.out.println("排序结果:");
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
System.out.println();
}
public int getMiddle(Integer[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
System.out.println("-----排序---");
while (low < high) {
while (low < high && list[high] > tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] < tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
System.out.println();
}
list[low] = tmp; //中轴记录到尾
System.out.println("=====排序"+num+"结束====");
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
System.out.println();
num++;
return low; //返回中轴的位置
}
public void _quickSort(Integer[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
public void quick(Integer[] str) {
if (str.length > 0) { //查看数组是否为空
_quickSort(str, 0, str.length - 1);
}
}
}
运行结果
—–排序—
10 3 53 2 23 7 14 53
10 3 14 2 23 7 14 53
=====排序1结束====
10 3 14 2 23 7 34 53
—–排序—
7 3 14 2 23 14 34 53
7 3 2 2 23 14 34 53
=====排序2结束====
7 3 2 10 23 14 34 53
—–排序—
2 3 2 10 23 14 34 53
=====排序3结束====
2 3 7 10 23 14 34 53
—–排序—
2 3 7 10 23 14 34 53
=====排序4结束====
2 3 7 10 23 14 34 53
—–排序—
2 3 7 10 14 14 34 53
=====排序5结束====
2 3 7 10 14 23 34 53
排序结果:
2 3 7 10 14 23 34 53
java排序
因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。
后续会有更多的精彩的内容分享给大家。
支付宝扫一扫
微信扫一扫
