八大排序(有没有想起八大派围战光明顶的视角)中,要说面试中问到最多的排序,非快排莫属,快排的思想这里不再重复多说,作为基础中的基础,具体原理出门左转,见度娘!
具体代码实现:
public class QuictSort {
public void sort(int [] s, int left, int right){
int temp = s[left];
if(left>=right){
return;
}
int tempLeft = left+1;
int tempRight = right;
while(true){
while(s[tempRight]>temp && tempLeft < tempRight){
tempRight--;
}
if(tempLeft >= tempRight){
break;
}
int t = s[tempRight];
s[tempRight] = temp;
temp = t;
while(s[tempLeft] < temp && tempLeft < tempRight){
tempLeft++;
}
if(tempLeft >= tempRight){
break;
}
t = s[tempLeft];
s[tempLeft] = temp;
temp = t;
}
if(s[tempRight] < temp){
int t= s[tempRight];
s[tempRight] = temp;
temp =t;
tempRight--;
}
s[left] = temp;
if(left+1>=right){
return;
}
sort(s,left, tempRight);
sort(s,tempRight+1, right);
}
public static void main(String[] args){
int [] s={5,4,3,2,1};
QuictSort quictSort = new QuictSort();
quictSort.sort(s, 0, 4);
for(int s1:s){
System.out.println(s1+" ");
}
}
}