之前的博客中其实已经写过有关于快速排序的算法了,之所以再写一遍,是因为快速排序的应用场景还算是多的,而且在Java面试过程中有不小的几率会考察到快速排序的算法,这里我们只提供算法,至于详细的讲解,请看我之前的博客:
《Java实现快速排序算法》链接:https://blog.youkuaiyun.com/IBLiplus/article/details/81056945
package paixu;
public class FastSortSelf {
public static void sort(int []x ,int head,int foot) {
int left = head;
int right = foot;
while(left<right) {
int key = x[left];
while(left<right&&x[right]>key) {
right--;
}
if(x[right]<=key) {
int temp = x[right];
x[right] = x[left];
x[left] = temp;
}
while(left<right&&x[left]<key) {
left++;
}
if(x[left]<=key) {
int temp = x[right];
x[right] = x[left];
x[left] = temp;
}
}
if(left>head) {sort(x,head,left-1);}
if(right<foot) {sort(x,right+1,foot);}
}
public static void main(String[] args) {
int [] x = {12,20,5,16,15,10,30,45};
sort(x,0,x.length-1);
for(int i = 0;i<x.length;i++) {
System.out.print(x[i]+",");
}
}
}

本文深入探讨了快速排序算法在Java中的应用,提供了详细的排序代码实现,并通过一个实例展示了如何使用快速排序对整数数组进行排序。文章强调了快速排序在实际场景中的重要性和在Java面试中的常见性。
2752

被折叠的 条评论
为什么被折叠?



