快速排序
来源:互联网 发布:linux查看mysql连接数 编辑:程序博客网 时间:2024/06/05 05:51
package quicksort;public class QuickSort { public static void main(String[] args) { int [] myint=new int[]{4,1,9,2,8,7,3,13,113,12}; show (myint); //输出排序前结果 Qsort(myint,0,myint.length-1); show(myint); //排序后输出排序好的结果 } public static void show(int [] myint){ for(int num:myint){ System.out.print(num+" "); } System.out.println(); } public static void Qsort(int [] arr,int low,int high){ if(arr==null || arr.length<=1){ //一个元素或者为空没有必要 return; } if(low<high){ //左边必须小于右边 int mid=getmid(arr,low,high); //传递数据 先找到中间值,中间值左边排序,右边排序 Qsort(arr,low,mid-1); //分割,low为0 Qsort(arr,mid+1,high); //分割,high为length-1 } } public static int getmid(int [] arr,int low,int high){ int tmp=arr[low]; //保存第一个元素的值 while(low<high){ while(low<high && arr[high]>=tmp){ high--; //找到最左边一个大于第一个元素的数据 } arr[low]=arr[high]; //保存最右边一个大于第一个元素的数据 此时high:7 ,3 while(low<high && arr[low]<tmp){ low++; //找到最右边一个小于第一个元素的数据 } arr[high]=arr[low]; //保存右边一个小于第一个元素的数据,此时low:2 ,3 } arr[low]=tmp; //移动第一个元素到中间 return low; } }精髓:先找到第一个数:把小于他的放在左边,大于他的放在右边。两段,再这样递归进行
详解:取出第一个值赋值给temp,循环开始:从最右边遍历条件low小于high并且arr[high]>tmp,high--找到比temp小的值arr[high]赋值给arr[low],low此时为0;
从最左边遍历找到比temp大的值arr[low]赋值给给arr[high],跳出循环后把temp赋值给arr[low],并返回low
// 4 1 9 2 8 7 5 3 13 // 4| 1 3 2 |8 7 5 9 13 // 1 3 2 | 4| 8 7 5 9 13
快速排序的基本思想:首先选取一个记录作为枢纽(不失一般性,可选第一个记录),依他的关键字为基准重排其余记录,将所有关键字比他大的记录都安置在它之后,而降所有关键字比他小的记录都安置在它之前,由此完成一趟快速排序;之后,分别对由一趟排序分割成的两个子序列进行快速排序。
本人调试笔记:
// 精髓:先找到第一个数:把小于他的放在左边,大于他的放在右边。两段,再这样递归进行// 详解:取出第一个值赋值给temp,循环开始:从最右边遍历条件low小于high并且arr[high]>tmp,high--找到比temp小的值arr[high]赋值给arr[low],low此时为0;// 从最左边遍历找到比temp大的值arr[low]赋值给给arr[high],跳出循环后把temp赋值给arr[low],并返回low // 4 1 9 2 8 7 5 3 13 // 4| 1 3 2 |8 7 5 9 13 // 1 3 2 | 4| 8 7 5 9 13 public static void show( int [] myint) { for(int num:myint) { System.out.print(num+" "); }// System.out.println("aa"); System.out.println("\n"); } public static int getmid(int []arr ,int low,int high) { int temp=arr[low];//保存第一个元素的值 // int temp=arr[low];//保存最后一个元素的值// System.out.println(temp); System.out.println(arr[low]); System.out.println(low+" "+high +""); // 0 10 while(low<high) { while(low<high && arr[high]>temp) { high--;//找到最左边一个大于第一个元素的数据 } arr[low]=arr[high];//保存最右边一个大于第一个元素的数据 此时high:7 ,3 System.out.println(high+"6777"); while(low<high && arr[low] <=temp) { low++;//找到最右边一个小于第一个元素的数据 } System.out.println(low+"777"); arr[high]=arr[low];//保存右边一个小于第一个元素的数据,此时low:2 ,3 show( arr); } arr[low]=temp;//移动第一个元素到中间 show( arr); return low; } public static void Qsort(int []arr ,int low,int high) { if(arr==null || arr.length<=1)//一个元素或者为空没有必要 { return; } if(low<high)//左边必须小于右边 { int mid=getmid(arr,low,high);//传递数据 先找到中间值,中间值左边排序,右边排序 System.out.println("hhhhh"+mid+arr[mid]); System.out.println("hhhha"+high+low); Qsort(arr ,low,mid-1);//分割,low为0 Qsort(arr ,mid+1,high);//分割 10 } } public static void main(String[] args) { int [] myint=new int[]{4,1,9,2,8,7,5,3,13,113,12}; show( myint); //输出排序前结果// System.out.println("cc"); Qsort(myint ,0,myint.length-1);// System.out.println(myint.length);// System.out.println("bb");// show( myint);//排序后输出排序好的结果 }
取最后一个核心算法:
public static int getmid(int []arr ,int low,int high) {// int temp=arr[low];//保存第一个元素的值 int temp=arr[high];//保存最后一个元素的值// System.out.println(temp); System.out.println(arr[low]); System.out.println(arr[high]); System.out.println(low+" "+high +""); // 0 10 while(low<high) { while(low<high && arr[low] <=temp) { low++;//找到最右边一个大于第一个元素的数据 } System.out.println(low+"777"); arr[high]=arr[low];//保存右边一个小于第一个元素的数据,此时low:2 ,3 while(low<high && arr[high]>temp) { high--;//找到最右边第一个小于第一个元素的数据 } arr[low]=arr[high];//保存最左边一个大于第一个元素的数据 此时high:7 ,3 System.out.println(high+"6777"); show( arr); } arr[high]=temp;//移动第一个元素到中间 show( arr); return high; }
快速排序(从小到大排序)取左边第一个与取右边最后一个为temp的区别:
取左边第一个为temp,while循环时,先找最右边第一个小于temp的数据,然后再找最左边第一个大于temp的数据,两者数据进行位置交换,把temp给arr[low](移动第一个元素到中间),返回low。
取右边最后一个为temp,while循环时,先找到最左边第一个大于temp的数据,然后找到最右边第一个小于temp的数据,两者数据进行位置交换,把temp给arr[high](移动最后一个元素到中间),返回high。
注:getmid()方法中两个while循环><互掉,能实现从小到大排序。
ps:对字符串的处理--对应数的哈希值
快速排序对字符串排序的处理:
public static int getmid(String [] ints,int low,int high) { String temp=ints[low]; while(low<high) { //从右往左, while(low<high && ints[high].hashCode()>temp.hashCode() )////最左边大于第一个数 { high--; } //从左往右, ints [low]=ints[high];//保存最左边大于第一个数 while(low<high && ints[low].hashCode()<temp.hashCode())//最右边小于第一个数 { low++; } ints [high]=ints[low];//保存最左边大于第一个数 } ints[low]=temp;//保存 return low; }
冒泡对字符串排序的处理:
public static void bubblesort(String[] strs) { System.out.println("排序开始"); for(int i=1;i<strs.length;i++) { for(int j=0;j<strs.length-i;j++) { if(strs[j].hashCode()>strs[j+1].hashCode())//hashcode,字符串 10010100 { String temp=strs[j]; strs[j]=strs[j+1]; strs[j+1]=temp; } } } System.out.println("排序完成"); }
0 0
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- Node.js与Sails之Model数据模型
- 【USACO题库】5.4.4 Betsy's Tour漫游小镇
- BeanUtils的介绍
- 使用LoadRunner之参数化取值
- 第15周 oj Problem H: 判断字符串是否为回文
- 快速排序
- 456. 132 Pattern
- 字典的使用
- Activity的生命周期
- 学习C语言需要了解的计算机的基本原理知识
- AdapterView
- 网络OSI和TCP/IP参考模型详解
- python中@property装饰器的用法
- 自定义Android资源覆盖rro方案