博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【代码积累】quick sort bia direction
阅读量:4099 次
发布时间:2019-05-25

本文共 1835 字,大约阅读时间需要 6 分钟。

public class Main {	public static void main(String[] args) {		// TODO Auto-generated method stub		//int[] test = {5,4,3,2,1};		int[] test={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};		quickSortBia(test,0,test.length-1,false);				showArray(test);	}	public static void showArray(int[] a) {		for(int i=0;i
= high ) { return; } //calculate pivot index int pivotIndex = (low+high)/2; int storeIndex = partition(a,low,high,pivotIndex,isAscend); quickSortBia(a,low,storeIndex-1,isAscend); quickSortBia(a,storeIndex+1,high,isAscend); } public static int partition(int[] a,int low,int high,int pivotIndex,boolean isAscend) { int pivotValue = a[pivotIndex]; int storeIndex = low; //This is the key step int tmp = 0; //swap a[pivotIndex] and a[high] a[pivotIndex] = a[high]; a[high] = pivotValue; /*traverse from a[0] to a[high-1] in a loop,the storeIndex start from 0,and each time a[i] is smaller/larger than pivotValue * swap a[i] and a[storeIndex],increase storeIndex by 1,after the loop,swap a[high] and a[storeIndex],return storeIndex.*/ for(int i=low;i<=high-1;i++) { if( true == isAscend ) { if(a[i] < pivotValue) { tmp = a[i]; a[i] = a[storeIndex]; a[storeIndex] = tmp; storeIndex++; //increase after swap a[i] and a[storeIndex] } } else { if(a[i] > pivotValue) { tmp = a[i]; a[i] = a[storeIndex]; a[storeIndex] = tmp; storeIndex++; } } } tmp = a[storeIndex]; a[storeIndex] = pivotValue; a[high] = tmp; showArray(a); return storeIndex; }}/*综述: * 快速排序的几个关键点: * 1、分治法,参照点的选取方式 * 2、partition函数的作用是对根据pivot对数列进行趋势排列,然后根据pivot的最终index,将数列划分为2个子数列,再对子数列进行排序,递归调用 * partition的storeIndex的值取数列的首坐标,不能初始化为0 * partition中有3次swap * 只有在发生了a[i] 与 a[storeIndex]的交换后,才需要对storeIndex进行递增操作 * */

转载地址:http://jthii.baihongyu.com/

你可能感兴趣的文章
Markdown编辑器快捷键说明
查看>>
python字符串操作
查看>>
python实现学生信息管理系统
查看>>
MATLAB程序:BPSK/QPSK的调制与解调
查看>>
MATLAB程序:IEEE802.16d路径损耗模型
查看>>
MATLAB程序:S-V信道模型
查看>>
OFDM在实际系统中的过采样
查看>>
关于时间复杂度
查看>>
进程及其实现
查看>>
MySQL常用语句
查看>>
HTML速查列表
查看>>
HTML语法之表单控件
查看>>
CSS-background属性速查
查看>>
kotlin-实战迁移项目(一)
查看>>
Android 11(R)适配_(8月更新)
查看>>
Android 面试题(答案还没写完)
查看>>
OOP面向对象的理解
查看>>
Handler源码分析
查看>>
使用Retrofit+Rxjava实现轮播
查看>>
Viewpager自动循环播放
查看>>