- (void)algorithm {     [self bubbleSort];     [self quickSort];     [self selectSort];     [self heapSelectSort];     [self insertSort]; }
 
 
  - (void)bubbleSort {   NSMutableArray *array = [NSMutableArray arrayWithArray:@[@23, @13, @42, @59, @10, @12, @18, @29, @2, @5, @30, @50]];   for (int i = 0; i < array.count - 1; i++) {       
        for (int j = 0; j < array.count - 1 - i; j++) {           NSLog(@"%d  %d", i, j);           if ([array[j] integerValue] > [array[j+1] integerValue]) {               [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];           }       }
    }   NSLog(@"%@", array);   }
 
 
  - (void)quickSort {   NSMutableArray *array = [NSMutableArray arrayWithArray:@[@23, @13, @42, @59, @10, @12, @18, @29, @2, @5, @30, @50]];   [self quickSortArray:array leftIndex:0 rightIndex:array.count - 1];   }
  - (void)quickSortArray:(NSMutableArray *)array leftIndex:(NSInteger)left rightIndex:(NSInteger)right {   if (left > right) {       return;   }      NSInteger temp = [array[left] integerValue];   NSInteger i = left;   NSInteger j = right;
    while (i < j)   {              while ([array[j] integerValue] >= temp && i < j)       {           j--;       }              while([array[i] integerValue] <= temp && i < j)       {           i++;       }
        if (i < j)       {           [array exchangeObjectAtIndex:j withObjectAtIndex:i];       }
    }      [array exchangeObjectAtIndex:i withObjectAtIndex:left];
          [self quickSortArray:array leftIndex:left rightIndex:i - 1];      [self quickSortArray:array leftIndex:i+1 rightIndex:right];   NSLog(@"%@", array);   }   
  - (void)selectSort {   NSMutableArray *array = [NSMutableArray arrayWithArray:@[@23, @13, @42, @59, @10, @12, @18, @29, @2, @5, @30, @50, @42]];   for (int i = 0; i < array.count - 1; i++) {       for (int j = i + 1; j < array.count; j++) {           if ([array[i] integerValue] > [array[j] integerValue]) {               [array exchangeObjectAtIndex:i withObjectAtIndex:j];           }       }   }   NSLog(@"%@", array);   }
 
 
  - (void)heapSelectSort {   NSMutableArray *array = [NSMutableArray arrayWithArray:@[@23, @13, @42, @59, @10, @12, @18, @29, @2, @5, @30, @50, @42]];      for (NSInteger i = array.count * 0.5; i >= 0; i--) {       [self heapAdjustWithArray:array parentIndex:i length:array.count];   }      for (NSInteger j = array.count - 1; j > 0; j--) {              [array exchangeObjectAtIndex:j withObjectAtIndex:0];              [self heapAdjustWithArray:array parentIndex:0 length:j];   }   NSLog(@"堆排序升序结果是--->%@",array);   }
  - (void)heapAdjustWithArray:(NSMutableArray *)array       parentIndex:(NSInteger)parentIndex            length:(NSInteger)length {   NSInteger temp = [array[parentIndex] integerValue];    NSInteger child = 2 * parentIndex + 1; 
    while (child < length) {              if (child + 1 < length && [array[child] integerValue] < [array[child + 1] integerValue]) {           child++;       }       
               if (temp >= [array[child] integerValue]) {           break;       }                     array[parentIndex] = array[child];                     parentIndex = child;       child = 2 * child + 1;
    }   array[parentIndex] = @(temp);   }   
  - (void)insertSort {   NSMutableArray *array = [NSMutableArray arrayWithArray:@[@23, @13, @42, @59, @10, @12, @18, @29, @2, @5, @30, @50, @42]];   NSInteger j;   for (NSInteger i = 1; i < array.count; i++) {      NSInteger temp = [array[i] integerValue];   for (j = i - 1; j >= 0 && temp < [array[j] integerValue]; j--) {                      array[j + 1] = array[j];           array[j] = @(temp);       }   }   NSLog(@"插入排序结果是--->%@",array);   }
  |