// 冒泡排序
- (void)bubbleSort {
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"98",@"75",@"89",@"53",@"67",@"92"]];
for (int i = 0; i < array.count - 1; i++) {
for (int j = 0; j < array.count-1-i; j++) {
//原理:从第1个数开始起,与后面的数字相互比较,满足条件的向后位移(值交换),若不满足条件,拿到大一点的数值继续向后比较
if ([array[j] intValue] > [array[j+1] intValue]) {
// 开始交换数据
NSString *temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
NSLog(@"%@",array);
}
}
// 选择排序
- (void)selectSort {
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"98",@"75",@"89",@"53",@"67",@"92"]];
for (int i = 0; i < array.count-1; i++) {
// 原理:从i后面第i+1个数起,跟array[i]相互比较,满足条件即交换值
for (int j = i+1; j < array.count; j++) {
// if里面的 '>' '<' 条件决定了排序的 升降
if ([array[i] intValue] > [array[j] intValue]) {
NSString *temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
NSLog(@"%@",array);
}
}
// 二分法查找
- (void)binarySearch {
// OC方法实现二分法:indexOfObject:inSortedRange:options:usingComparator:
/*
NSArray *sortedArray = ... // must be sorted
id searchObject = ...
NSRange searchRange = NSMakeRange(0, [sortedArray count]);
NSUInteger findIndex = [sortedArray indexOfObject:searchObject
inSortedRange:searchRange
options:NSBinarySearchingFirstEqual
usingComparator:^(id obj1, id obj2)
{
return [obj1 compare:obj2];
}];
*/
NSArray *numberArray = @[@1, @3, @27, @36, @42, @70, @82];
NSNumber *searchNum = @70;
NSUInteger mid;
NSUInteger min = 0;
NSUInteger max = [numberArray count] - 1;
BOOL found = NO;
while (min <= max) {
mid = (min + max)/2;
if ([searchNum intValue] == [numberArray[mid] intValue]) {
NSLog(@"We found the number! It is at index %lu", mid);
found = YES;
break;
} else if ([searchNum intValue] < [numberArray[mid] intValue]) {
max = mid - 1;
} else if ([searchNum intValue] > [numberArray[mid] intValue]) {
min = mid + 1;
}
}
if (!found) {
NSLog(@"The number was not found.");
}
}
// 快排和希尔排序
// Created by 葛朋 on 2018/5/26.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@(6), @(1),@(2),@(5),@(9),@(4),@(3),@(7),@(100),@(80),@(88),@(30),@(35),@(20),@(14),@(34),@(88),@(83),nil];
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
// [self quickSortArray:arr withLeftIndex:0 andRightIndex:arr.count - 1];
[self xierAry:arr];
NSDate* dat2 = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a2 =[dat2 timeIntervalSince1970];
NSLog(@"%@--时间:%f",arr,(a2 - a));
}
- (void)xierAry:(NSMutableArray *)ary{
NSInteger n = ary.count;
for (NSInteger gap = n/2 ; gap > 0; gap /= 2) {
for (NSInteger i = gap; i < n; i++) {
for (NSInteger j = i - gap; j>= 0 && ary[j] > ary[j+ gap]; j-= gap) {
[ary exchangeObjectAtIndex:j withObjectAtIndex:(j+gap)];
}
}
}
}
- (void)quickSortArray:(NSMutableArray *)array withLeftIndex:(NSInteger)leftIndex andRightIndex:(NSInteger)rightIndex
{
if (leftIndex >= rightIndex) {//如果数组长度为0或1时返回
return ;
}
NSInteger i = leftIndex;
NSInteger j = rightIndex;
//记录比较基准数
NSInteger key = [array[i] integerValue];
while (i < j) {
/**** 首先从右边j开始查找比基准数小的值 ***/
while (i < j && [array[j] integerValue] >= key) {//如果比基准数大,继续查找
j--;
}
//如果比基准数小,则将查找到的小值调换到i的位置
array[i] = array[j];
/**** 当在右边查找到一个比基准数小的值时,就从i开始往后找比基准数大的值 ***/
while (i < j && [array[i] integerValue] <= key) {//如果比基准数小,继续查找
i++;
}
//如果比基准数大,则将查找到的大值调换到j的位置
array[j] = array[i];
}
//将基准数放到正确位置
array[i] = @(key);
/**** 递归排序 ***/
//排序基准数左边的
[self quickSortArray:array withLeftIndex:leftIndex andRightIndex:i - 1];
//排序基准数右边的
[self quickSortArray:array withLeftIndex:i + 1 andRightIndex:rightIndex];
}
特别感谢 葛朋 快排、希尔排序
Created by 葛朋 on 2018/5/26.
网友评论