JAVA笔试又是快排!!!写在博客滥记于心!!!!!!!
来源:互联网 发布:安装完ubuntu无法启动 编辑:程序博客网 时间:2024/04/30 06:56
面试一家搞医疗软件的公司,笔试题又来一道排速排序,说是简单,现在可能也只是大学本科的无经验写的快排,先blog一下,以后应该会有优化.........
public void quickSort(int[] array){ //快排是不稳定的,如: 5、6.、6。、4 、1、2、3 ----选4做轴-----》 3、2、1、4、6。、6.、5 //明显两个6的位置换了
if(array==null || array.length<2)
return;
_quickSort(array,0,array.length-1);
}
private void _quickSort(int[] array,int low,int high){
if(low>=high) return ;
int tmpLow = low , tmpHigh = high , tmpNum = array[(high+low)/2];
while(tmpLow<tmpHigh){ //while 1
while(array[tmpLow]<tmpNum&&tmpLow<tmpHigh)
tmpLow++;
while(array[tmpHigh]>tmpNum&&tmpLow<tmpHigh)
tmpHigh--;
if(tmpLow<tmpHigh){ //用位与,相对加减,不会有溢出问题
array[tmpLow] = array[tmpLow]^array[tmpHigh];
array[tmpHigh] = array[tmpLow]^array[tmpHigh];
array[tmpLow] = array[tmpLow]^array[tmpHigh];
}
tmpLow++;
tmpHigh--;
}//while 1 走完一趟快排
_quickSort(array , low,(high+low)/2-1);//注意这两个段必然是全部的
_quickSort(array, (high+low)/2, high);//不能把这里的(high+low)/2改为(high+low)/2+1,除非上面是(high+low)/2,否则这样会漏掉中间那个,而中间那个也不一定就是在这个位置
}
- JAVA笔试又是快排!!!写在博客滥记于心!!!!!!!
- 写在Google笔试之前
- 写在博客之前的话——java程序员规划
- 写在前面-本博客是与java相关的
- 【记】又一轮的笔试结束
- 又一年,写在新年里
- 写在博客的开头
- 写在博客开始前
- 写在博客开篇之际
- 写在博客之前
- 写在博客的开头
- 写在博客启用之际
- 写在博客的序言
- 写在前面-开始博客
- 写在博客开篇
- 写在博客的开篇
- 写在博客前面的话
- 写在博客开始
- pb webfrom右键菜单的编程
- FusionCharts参数的详细说明和功能特性
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- android拍照
- GetModuleFileName
- JAVA笔试又是快排!!!写在博客滥记于心!!!!!!!
- VC++ MFC文件的移动复制删除更名遍历操作
- yii导入资源文件
- 什么是BIOS bootloader ubuut
- Lucene3.6小例子
- uC/OS-II 学习笔记之:任务的概念
- Hibernate将文件二进制存入mysql
- 置顶菜单可用性研究,可使网站导航快22%
- eclipse subclipse unable load default svn client的解决办法