iOS 【排序】

#pragma mark - 冒泡排序
+ (void)bubbleSort:(NSMutableArray *)mutableArray {
  if(mutableArray == nil || mutableArray.count == 0)
      return;
  NSLog(@"冒泡排序之前: %@", mutableArray);
  for (NSInteger i = 0; i < mutableArray.count; i++) {
      for (NSInteger j = 0; j < mutableArray.count - i - 1; j++) {
          if ([mutableArray[j + 1] floatValue] < [mutableArray[j] floatValue]) {
              CGFloat tempFloat = [mutableArray[j] floatValue];
              mutableArray[j] = mutableArray[j + 1];
              mutableArray[j + 1] = [NSNumber numberWithFloat:tempFloat];
          }
      }
  }
  NSLog(@"冒泡排序之后: %@", mutableArray);
}
#pragma mark - 直接插入排序
+ (void)insertSort:(NSMutableArray *)mutableArray {
    if(mutableArray == nil || mutableArray.count == 0)
        return;
    NSLog(@"直接插入排序之前: %@", mutableArray);
    for (NSInteger i = 1; i < mutableArray.count; i++) {
        CGFloat tempFloat = [mutableArray[i] floatValue];
        for (NSInteger j = i - 1; j >= 0 && tempFloat < [mutableArray[j] floatValue]; j--) {
            mutableArray[j + 1] = mutableArray[j];
            mutableArray[j] = [NSNumber numberWithFloat:tempFloat];
        }
    }
    NSLog(@"直接插入排序之后: %@", mutableArray);
}
#pragma mark - 選擇排序
+ (void)chooseSort:(NSMutableArray *)mutableArray {
    if(mutableArray == nil || mutableArray.count == 0)
        return;
    NSLog(@"選擇排序之前: %@", mutableArray);
    for (NSInteger i = 0; i < mutableArray.count; i++) {
        for (NSInteger j = i + 1; j < mutableArray.count; j++) {
            if ([mutableArray[i] floatValue] > [mutableArray[j] floatValue]) {
                CGFloat tempFloat = [mutableArray[i] floatValue];
                mutableArray[i] = mutableArray[j];
                mutableArray[j] = [NSNumber numberWithFloat:tempFloat];
            }
        }
    }
    NSLog(@"選擇排序之后: %@", mutableArray);
}
#pragma mark - 折半插入排序
+ (void)binaryInsertSort:(NSMutableArray *)mutableArray {
    if(mutableArray == nil || mutableArray.count == 0)
        return;
    NSLog(@"折半插入排序之前: %@", mutableArray);
    for(NSInteger i = 1 ; i < mutableArray.count ; i++) {
        CGFloat tempFloat = [[mutableArray objectAtIndex:i] floatValue];
        NSInteger left = 0;
        NSInteger right = i - 1;
        
        while (left <= right) {
            CGFloat middle = (left + right) / 2;
            
            if(tempFloat < [[mutableArray objectAtIndex:middle] floatValue]){
                right = middle - 1;
            } else {
                left = middle + 1;
            }
        }
        for(NSInteger j = i ; j > left; j--) {
            [mutableArray replaceObjectAtIndex:j withObject:[mutableArray objectAtIndex:j - 1]];
        }
        [mutableArray replaceObjectAtIndex:left withObject:[NSNumber numberWithFloat:tempFloat]];
    }
    NSLog(@"折半插入排序之后: %@", mutableArray);
}
#pragma mark - 希爾排序
+ (void)shellSort:(NSMutableArray *)mutableArray {
    if(mutableArray == nil || mutableArray.count == 0)
        return;
    NSLog(@"希爾排序之前: %@", mutableArray);
    NSInteger shellValue = mutableArray.count / 2;
    while (shellValue >= 1) {
        for(NSInteger i = shellValue; i < mutableArray.count; i++) {
            CGFloat tempFloat = [[mutableArray objectAtIndex:i] floatValue];
            NSInteger j = i;
            while (j >= shellValue && tempFloat < [[mutableArray objectAtIndex:(j - shellValue)] floatValue]) {
                [mutableArray replaceObjectAtIndex:j withObject:[mutableArray objectAtIndex:j - shellValue]];
                j -= shellValue;
            }
            [mutableArray replaceObjectAtIndex:j withObject:[NSNumber numberWithFloat:tempFloat]];
        }
        shellValue = shellValue / 2;
    }
    NSLog(@"希爾排序之后: %@", mutableArray);
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • ??冒泡排序 ??選擇排序 在iOS開發過程中,或者面試過程中,避免不了需要一些算法去解決問題,尤其是iOS面試中...
    樂編閱讀 1,019評論 0 7
  • iOS 按時間的近期優先 NSSortDescriptor *sorter = [[NSSortDescripto...
    jack0087閱讀 593評論 0 0
  • 1.選擇排序 由于選擇排序過于簡單,看一下代碼應該就能懂 2.冒泡排序 3.快速排序 步驟:1 ).設置兩個變量i...
    Mr_fei閱讀 521評論 0 2
  • (插入排序、選擇排序、交換排序、歸并排序、基數排序) 排序有內部排序和外部排序,內部排序是數據記錄在內存中進行排序...
    文中客閱讀 6,995評論 5 31
  • I:WHAT該片段講了什么內容?該片段主要講了,底線是可以適當浮動的,頑固不是哈佛的代名詞。 WHY:為何會說底線...
    零風xyxn閱讀 217評論 0 0