//*
*创建by Sheldon on 2019/9/18。
*项目名称: al study。
* Package Name: PACKAGE_NAME。
*/
公共类快速排序{
私有静态输入计数;
//*
*测试
*
* @param args
*/
publicstaticvoidmain (字符串[ ] args ) {
int [ ] num={ 6,2,7,3,8,9 };
system.out.println (阵列字符串) ) num,“未排序”);
快速排序(num,0,num.length - 1 );
system.out.println (array tostring (num,'排序') );
System.out.println ('数组个数: ' num.length );
System.out.println;
}
//*
*快速排序
*
* @param num排序数组
* @param left序列的前针
* @param right序列后针
*/
私有状态语音quicksort (int [ ] num,int left,int right ) {
如果left等于right,即数组中只有一个元素,则直接返回
if(left=right ) {
返回;
}
//以最左边的要素为基准值
int key=num[left];
//数组中小于key的为左,大于key的为右,key值的下标为I
int i=left;
int j=right;
wile(Ij ) {
//j向左移动直到遇到小于key的值
while(num[j]=keyIj ) {
j----;
}
//i向右移动直到遇到大于key的值
while(num[I]=keyIj ) {
I;
}
//i和j所指元素的交换
if(Ij ) {
int temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
num[left]=num[i];
num[i]=key;
出局;
system.out.println (阵列字符串(num,'排序次数' count ) );
快速排序(num,left,i - 1 );
快速排序(num,i 1,right );
}
//*
将int类型数组转换为字符串
*
* @param arr
* @param flag
* @return
*/
私有状态字符串(int [ ] arr,字符串标志) {
String str='数组由' flag ' ):';
for(inta:ARR ) {
str =a 't ';
}
返回str;
}
}