-(IBAction)zheBanChaZhao:(id)sender{
NSMutableArray * allObj=[[NSMutableArray alloc] init];
int base =0;
int findY=arc4random()%1000;
for (int i=1; i<100; i++) {
int tmpX=arc4random() % 10;
base+=tmpX;
[allObj addObject:[NSNumber numberWithInt:base]];
}
NSMutableString *mulStr=[[NSMutableString alloc] init];
NSInteger count=[allObj count];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"当前的数组 %@ 要查找的值 %d",mulStr,findY);
NSInteger index=-1;
NSInteger low =0;
NSInteger high=allObj.count-1;
while (low < high) {
NSInteger mid=(low+high)/2;
NSNumber *tmpNum=[allObj objectAtIndex:mid];
int tmpInt=[tmpNum intValue];
if (findY < tmpInt) {
high = mid-1;
} else if(findY > tmpInt) {
low = mid+1;
}else{
index = mid;
break;
}
}
if (index != -1) {
NSLog(@"找到了 是第%ld个数字",index);
}else{
NSLog(@"没有找到\n");
}
}
-(IBAction)xuanZhePaiXu:(id)sender{
NSMutableArray * allObj=[[NSMutableArray alloc] init];
for (int i=1; i<30; i++) {
int tmpX=arc4random() % 100;
[allObj addObject:[NSNumber numberWithInt:tmpX]];
}
NSMutableString *mulStr=[[NSMutableString alloc] init];
NSInteger count=[allObj count];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"当前的数组 %@",mulStr);
for (int i=0; i<count; i++) {
for (int j=i; j<count; j++) {
NSNumber *iNum=[allObj objectAtIndex:i];
NSNumber *jNum=[allObj objectAtIndex:j];
// if ([iNum integerValue] < [jNum integerValue]) {
// [allObj exchangeObjectAtIndex:j withObjectAtIndex:i];
// }
if ([iNum integerValue] > [jNum integerValue]) {
[allObj exchangeObjectAtIndex:j withObjectAtIndex:i];
}
}
}
mulStr=[[NSMutableString alloc] init];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"排序后的数组 %@",mulStr);
}
-(IBAction)maoPao:(id)sender{
NSMutableArray * allObj=[[NSMutableArray alloc] init];
for (int i=1; i<30; i++) {
int tmpX=arc4random() % 100;
[allObj addObject:[NSNumber numberWithInt:tmpX]];
}
NSMutableString *mulStr=[[NSMutableString alloc] init];
NSInteger count=[allObj count];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"当前的数组 %@",mulStr);
for (int i=1; i<count; i++) {
for (int j=0; j<count-i; j++) {
NSNumber *jNum=[allObj objectAtIndex:j];
NSNumber *jNum1=[allObj objectAtIndex:j+1];
if ([jNum1 integerValue] > [jNum integerValue]) {
[allObj exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
mulStr=[[NSMutableString alloc] init];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"排序后的数组 %@",mulStr);
}
-(IBAction)kuaiSu:(id)sender{
NSMutableArray * allObj=[[NSMutableArray alloc] init];
for (int i=1; i<10; i++) {
int tmpX=arc4random() % 100;
[allObj addObject:[NSNumber numberWithInt:tmpX]];
}
NSMutableString *mulStr=[[NSMutableString alloc] init];
NSInteger count=[allObj count];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"当前的数组 %@",mulStr);
[self kuaiSu:allObj withLeftIndex:0 andRightIndex:allObj.count - 1];
mulStr=[[NSMutableString alloc] init];
for (NSInteger i=0; i<count; i++) {
NSNumber *num = [allObj objectAtIndex:i];
[mulStr appendString:[NSString stringWithFormat:@"%ld,",[num integerValue]]];
}
NSLog(@"当前的数组 %@",mulStr);
}
- (void)kuaiSu:(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 kuaiSu:array withLeftIndex:leftIndex andRightIndex:i - 1];
//排序基准数右边的
[self kuaiSu:array withLeftIndex:i + 1 andRightIndex:rightIndex];
}
对应源码