快速排序
快速排序是对冒泡排序的改进,它使用分治法的思想,每次循环根据指定的基准数,将其他元素分别放置其左右(升序排序,大的放右小的放左),第二次循环,以基准数为中心,分为左右两部分,每部分再通过新的基准数排序…(下边来个小例子解释)。基准数:一般指定第一个元素为基准数(任意元素都可以作为基准数)。
- 来个小例子:
对一个int型数组升序排序(第一个位置为基准数),两个指针分别指向数组头尾:
int[] arr={6,1,2,7,9,3,4,5,10,8};
int frist=0;
int last=9;
- 第一次循环,frist指向arr[0],last指向arr[9]。首先从右开始寻找小于基准数6的元素,last指向arr[7]=5,frist从左寻找大于基准数6的元素,指向arr[3]=7,交换两个位置的值
int[] arr={6,1,2,5,9,3,4,7,10,8};
int frist=3;
int last=7;
last继续向左移动,指向arr[6]=4,frist向右移动,指向arr[4]=9,交换值
int[] arr={6,1,2,5,4,3,9,7,10,8};
int frist=4;
int last=6;
接下来,last继续向左移动,指向arr[5]=3,frist向右移动,指向arr[5]=3,frist=last,交换基准数与arr[5]=3位置,至此第一次循环结束(左边全部小于基准数6,右边全部大于6)。
int[] arr={3,1,2,5,4,6,9,7,10,8};
int frist=5;
int last=5;
再将数组以基准数6为中心,分为两部分,分别作为新的数组进行排序:
int[] arr1={3,1,2,5,4};
int[] arr2={9,7,10,8};
2.第二次循环
第一部分,新的基准数为3,last,frist分别寻找小于,大于基准数3的元素,交换它们的位置,直至frist=last
//start
int[] arr={3,1,2,5,4,6,9,7,10,8};
int frist=0;
int last=4;
//第一次交换,frist=last基准数与arr[2]=2交换
int[] arr={2,1,3,5,4,6,9,7,10,8};
int frist=2;
int last=2;
再将数组以基准数3为中心,分为两部分,分别作为新的数组进行排序:
int[] arr1={2,1};
int[] arr2={5,4};
第二部分,新的基准数为9,last,frist分别寻找小于,大于基准数9的元素,交换它们的位置,直至frist=last
//start
int[] arr={3,1,2,5,4,6,9,7,10,8};
int frist=6;
int last=9;
//第一次交换,frist=8,last=9 arr[9]=8与arr[8]=10交换
int[] arr={2,1,3,5,4,6,9,7,8,10};
int frist=8;
int last=9;
//第二次交换,frist=last=8 基准数9与arr[8]=8交换
int[] arr={2,1,3,5,4,6,8,7,9,10};
int frist=8;
int last=9;
再将数组以基准数9为中心,分为两部分,分别作为新的数组进行排序:
int[] arr1={8,7};
int[] arr2={10};
3.第三次循环
第一部分,新的基准数为2,last,frist分别寻找小于,大于基准数3的元素,交换它们的位置,直至frist=last
//start
int[] arr={2,1,3,5,4,6,9,7,10,8};
int frist=0;
int last=1;
//第一次交换,frist=last基准数与arr[2]=2交换
int[] arr={1,2,3,5,4,6,9,7,10,8};
int frist=2;
int last=2;
往下同理即可。。。
3.递归代码实现
public class QuickSort {
public void fristBaseValue(int[] arr, int frist, int last) {
if (frist > last) return;//递归出口
int i = frist;
int j = last;
int temp = arr[frist];
while (i != j) {
while (arr[j] >= temp && j > i) {
j--;
}
while (arr[i] <= temp && i < j) {
i++;
}
if (i < j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
if (i == j) {
arr[frist] = arr[i];
arr[i] = temp;
}
fristBaseValue(arr, frist, i - 1);
fristBaseValue(arr, i + 1, last);
}
}
递归排序过程
[6, 1, 2, 7, 9, 3, 4, 5, 10, 8];frist=0;last=9
[3, 1, 2, 5, 4, 6, 9, 7, 10, 8];frist=0;last=4
[2, 1, 3, 5, 4, 6, 9, 7, 10, 8];frist=0;last=1
[1, 2, 3, 5, 4, 6, 9, 7, 10, 8];frist=0;last=0
[1, 2, 3, 5, 4, 6, 9, 7, 10, 8];frist=0;last=-1
[1, 2, 3, 5, 4, 6, 9, 7, 10, 8];frist=1;last=0
[1, 2, 3, 5, 4, 6, 9, 7, 10, 8];frist=2;last=1
[1, 2, 3, 5, 4, 6, 9, 7, 10, 8];frist=3;last=4
[1, 2, 3, 4, 5, 6, 9, 7, 10, 8];frist=3;last=3
[1, 2, 3, 4, 5, 6, 9, 7, 10, 8];frist=3;last=2
[1, 2, 3, 4, 5, 6, 9, 7, 10, 8];frist=4;last=3
[1, 2, 3, 4, 5, 6, 9, 7, 10, 8];frist=5;last=4
[1, 2, 3, 4, 5, 6, 9, 7, 10, 8];frist=6;last=9
[1, 2, 3, 4, 5, 6, 8, 7, 9, 10];frist=6;last=7
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=6;last=6
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=6;last=5
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=7;last=6
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=8;last=7
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=9;last=9
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=9;last=8
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];frist=10;last=9
测试代码
public void fristBaseValue() {
int[] arr={6,1,2,7,9,3,4,5,10,8};
QuickSort sort=new QuickSort();
sort.fristBaseValue(arr,0,arr.length-1);
}