iOS快速排序

来源:互联网 发布:php字符串截取添加 编辑:程序博客网 时间:2024/06/06 00:23
#import <Foundation/Foundation.h>@interface QuickSort : NSObject/// 快速排序+ (void)quickSortWithArray:(NSMutableArray *)array withLeft:(NSInteger)left andRight:(NSInteger)right;@end
#import "QuickSort.h"@implementation QuickSort+ (void)quickSortWithArray:(NSMutableArray *)array withLeft:(NSInteger)left andRight:(NSInteger)right{    if (left >= right) return;        NSInteger i = left;    NSInteger j = right;    NSInteger key = [array[left] integerValue];    while (i<j) {        while (i<j && key <= [array[j]integerValue]) {            j--;        }        array[i] = array[j];        while (i<j && key >= [array[i]integerValue]) {            i++;        }    }    array[j] = array[i];    [[self class]quickSortWithArray:array withLeft:left andRight:i -1];    [[self class]quickSortWithArray:array withLeft:i+1 andRight:right];}@end


0 0