快速排序
快速排序(Quicksort)是对冒泡排序的一种改进
基本思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
这里还是分别用C、OC和swift写一下,欢迎交流。
C语言版
//函数声明
void quick_sort(int a[], int left, int right);
int array[11] = {23, 8, 90, 12, 7, -9, 54, 3, 36, 99, -17};
//调用
quick_sort(array, 0, 10);
printf("sort result:");
for (int p = 0; p < 11; p++) {
printf(" %d", array[p]);
}
printf("\n");
//sort result: -17 -9 3 7 8 12 23 36 54 90 99
//实现
void quick_sort(int a[], int left, int right) {
int i = 0, j = 0, t = 0, temp = 0;
if(left > right) {//每次二分,左下标一定小于右下标
return;
}
temp = a[left]; //基准数
i = left;
j = right;
while(i != j)
{
//顺序很重要,要先从右往左找 如果大于temp 那就继续看下一个是否大于temp
while(a[j] >= temp && i < j){
j--;
}
//从左往右找 如果小于temp 那就继续看下一个是否小于temp
while(a[i] <= temp && i < j){
i++;
}
//从上面两个while中分别从右侧找到了一个小于temp的值,左侧找到了一个大于temp的值 这里交换两个值
if(i < j)//当i和j没有相遇时
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
//确定基准数的位置之后,开始分左右两部分 分别开始新一轮的基准数位置的确定
quick_sort(a, left, i-1);//继续处理左边的,这里是一个递归的过程
quick_sort(a, i+1, right);//继续处理右边的,这里是一个递归的过程
}
void quick_sort(int a[], int left, int right);
int array[11] = {23, 8, 90, 12, 7, -9, 54, 3, 36, 99, -17};
//调用
quick_sort(array, 0, 10);
printf("sort result:");
for (int p = 0; p < 11; p++) {
printf(" %d", array[p]);
}
printf("\n");
//sort result: -17 -9 3 7 8 12 23 36 54 90 99
//实现
void quick_sort(int a[], int left, int right) {
int i = 0, j = 0, t = 0, temp = 0;
if(left > right) {//每次二分,左下标一定小于右下标
return;
}
temp = a[left]; //基准数
i = left;
j = right;
while(i != j)
{
//顺序很重要,要先从右往左找 如果大于temp 那就继续看下一个是否大于temp
while(a[j] >= temp && i < j){
j--;
}
//从左往右找 如果小于temp 那就继续看下一个是否小于temp
while(a[i] <= temp && i < j){
i++;
}
//从上面两个while中分别从右侧找到了一个小于temp的值,左侧找到了一个大于temp的值 这里交换两个值
if(i < j)//当i和j没有相遇时
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
//确定基准数的位置之后,开始分左右两部分 分别开始新一轮的基准数位置的确定
quick_sort(a, left, i-1);//继续处理左边的,这里是一个递归的过程
quick_sort(a, i+1, right);//继续处理右边的,这里是一个递归的过程
}
OC版
_dataArray = [NSMutableArray arrayWithObjects:@21, @3, @34, @(-28), @10, @(-33), @54, @9, @0, @(-2), nil];
//调用
[self quickSortWithLeft:0 right:self.dataArray.count-1];
NSString *string = [self.dataArray componentsJoinedByString:@" "];
NSLog(@"sort result : %@", string);
//sort result : -33 -28 -2 0 3 9 10 21 34 54
- (void)quickSortWithLeft:(NSInteger)leftPointer right:(NSInteger)rightPointer {
int temp = 0;
NSInteger i = 0, j = 0;
if (leftPointer > rightPointer) {
return;
}
temp = [self.dataArray[leftPointer] intValue];
i = leftPointer;
j = rightPointer;
while (i != j) {
while ([self.dataArray[j] intValue] >= temp && i < j) {
j--;
}
while ([self.dataArray[i] intValue] <= temp && i < j) {
i++;
}
if (i < j) {
[self.dataArray exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
[self.dataArray exchangeObjectAtIndex:i withObjectAtIndex:leftPointer];
[self quickSortWithLeft:leftPointer right:i-1];
[self quickSortWithLeft:i+1 right:rightPointer];
}
//调用
[self quickSortWithLeft:0 right:self.dataArray.count-1];
NSString *string = [self.dataArray componentsJoinedByString:@" "];
NSLog(@"sort result : %@", string);
//sort result : -33 -28 -2 0 3 9 10 21 34 54
- (void)quickSortWithLeft:(NSInteger)leftPointer right:(NSInteger)rightPointer {
int temp = 0;
NSInteger i = 0, j = 0;
if (leftPointer > rightPointer) {
return;
}
temp = [self.dataArray[leftPointer] intValue];
i = leftPointer;
j = rightPointer;
while (i != j) {
while ([self.dataArray[j] intValue] >= temp && i < j) {
j--;
}
while ([self.dataArray[i] intValue] <= temp && i < j) {
i++;
}
if (i < j) {
[self.dataArray exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
[self.dataArray exchangeObjectAtIndex:i withObjectAtIndex:leftPointer];
[self quickSortWithLeft:leftPointer right:i-1];
[self quickSortWithLeft:i+1 right:rightPointer];
}
swift版
var dataArray:NSMutableArray = [76, 1, 19, -4, 2, 4, 6, 8, 0, -19];
//调用
self.quickSort(0, rightPointer: dataArray.count-1);
let string:NSString = dataArray.componentsJoinedByString(" ");
NSLog("sort result: %@", string);
//sort result: -19 -4 0 1 2 4 6 8 19 76
func quickSort(leftPointer:NSInteger, rightPointer:NSInteger) {
var temp:Int = 0;
var i:NSInteger = 0;
var j:NSInteger = 0;
if (leftPointer > rightPointer) {
return;
}
temp = dataArray.objectAtIndex(leftPointer).integerValue;
i = leftPointer;
j = rightPointer;
while i != j {
while dataArray.objectAtIndex(j).integerValue >= temp && i < j {
j -= 1;
}
while dataArray.objectAtIndex(i).integerValue <= temp && i < j {
i += 1;
}
if i < j {
dataArray.exchangeObjectAtIndex(i, withObjectAtIndex: j);
}
}
dataArray.exchangeObjectAtIndex(i, withObjectAtIndex: leftPointer);
self.quickSort(leftPointer, rightPointer: i-1);
self.quickSort(i+1, rightPointer: rightPointer);
}
//调用
self.quickSort(0, rightPointer: dataArray.count-1);
let string:NSString = dataArray.componentsJoinedByString(" ");
NSLog("sort result: %@", string);
//sort result: -19 -4 0 1 2 4 6 8 19 76
func quickSort(leftPointer:NSInteger, rightPointer:NSInteger) {
var temp:Int = 0;
var i:NSInteger = 0;
var j:NSInteger = 0;
if (leftPointer > rightPointer) {
return;
}
temp = dataArray.objectAtIndex(leftPointer).integerValue;
i = leftPointer;
j = rightPointer;
while i != j {
while dataArray.objectAtIndex(j).integerValue >= temp && i < j {
j -= 1;
}
while dataArray.objectAtIndex(i).integerValue <= temp && i < j {
i += 1;
}
if i < j {
dataArray.exchangeObjectAtIndex(i, withObjectAtIndex: j);
}
}
dataArray.exchangeObjectAtIndex(i, withObjectAtIndex: leftPointer);
self.quickSort(leftPointer, rightPointer: i-1);
self.quickSort(i+1, rightPointer: rightPointer);
}