演算法筆記整理

想想還是算了發表於2018-08-14

10塊錢買5瓶酒,2個瓶蓋換一瓶,4個酒瓶換一瓶。問10塊錢能買多少瓶酒?遞迴演算法解決

#import "ViewController.h"
@interface ViewController ()
 
@property (nonatomic ,assign) int i;
@property (nonatomic ,assign) int number;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.i = 1;
    self.number = 5;
    [self calculateBeerWithBeer:5 bottleCaps:0 bottle:0];
}
 
/**
 *  @author djr
 *  
 *  計算酒瓶數
 *  @param beer 啤酒數
 *  @param bottleCaps 瓶蓋數
 *  @param bottle 瓶子數
 */
- (void)calculateBeerWithBeer:(int)beer bottleCaps:(int)bottleCaps bottle:(int)bottle{
    NSLog(@"第%d次的啤酒數:%d,瓶蓋數:%d,瓶子數:%d",self.i,beer,bottleCaps,bottle);
    /** 不管什麼,反正瓶蓋和瓶子等於啤酒(可能有換不完的) */
    bottle += beer;
    bottleCaps += beer;
    /** 換完瓶蓋和瓶子之後的啤酒數,瓶蓋數,瓶子數 */
    beer = bottleCaps / 2 + bottle / 4;
    bottleCaps = bottleCaps % 2;
    bottle = bottle % 4;
    NSLog(@"最後一共喝了:%d瓶啤酒",self.number += beer);
    if (beer == 0) return ;
    self.i ++;
    [self calculateBeerWithBeer:beer bottleCaps:bottleCaps bottle:bottle];
    
}
@end
複製程式碼

為了排序A[1..n],我們遞迴的排序A[1..n-1],然後把A[n]插入到已排序的陣列A[1..n-1]。為插入排序的這個遞迴版本的最壞執行情況執行時間寫一個遞迴式:


歸併排序演算法

歸併排序演算法詳情

NSMutableArray * array = [NSMutableArray arrayWithObjects:@8,@7,@6,@5,@4,@3,@2,@1, nil];
//呼叫排序
[self mergeSortArray:array];
複製程式碼
- (void)mergeSortArray:(NSMutableArray *)array {
  //建立一個副本陣列
  NSMutableArray * auxiliaryArray = [[NSMutableArray alloc]initWithCapacity:array.count];

  //對陣列進行第一次二分,初始範圍為0到array.count-1
  [self mergeSort:array auxiliary:auxiliaryArray low:0 high:array.count-1];
}
- (void)mergeSort:(NSMutableArray *)array auxiliary:(NSMutableArray *)auxiliaryArray low:(int)low high:(int)high {
  //遞迴跳出判斷
  if (low>=high) {
    return;
  }
  //對分組進行二分
  int middle = (high - low)/2 + low;

  //對左側的分組進行遞迴二分 low為第一個元素索引,middle為最後一個元素索引
  [self mergeSort:array auxiliary:auxiliaryArray low:low high:middle];

  //對右側的分組進行遞迴二分 middle+1為第一個元素的索引,high為最後一個元素的索引
  [self mergeSort:array auxiliary:auxiliaryArray low:middle + 1 high:high];

  //對每個有序陣列進行迴歸合併
  [self merge:array auxiliary:auxiliaryArray low:low middel:middle high:high];
}
- (void)merge:(NSMutableArray *)array auxiliary:(NSMutableArray *)auxiliaryArray low:(int)low middel:(int)middle high:(int)high {
  //將陣列元素複製到副本
  for (int i=low; i<=high; i++) {
    auxiliaryArray[i] = array[i];
  }
  //左側陣列標記
  int leftIndex = low;
  //右側陣列標記
  int rightIndex = middle + 1;

  //比較完成後比較小的元素要放的位置標記
  int currentIndex = low;

  while (leftIndex <= middle && rightIndex <= high) {
    //此處是使用NSNumber進行的比較,你也可以轉成NSInteger再比較
    if ([auxiliaryArray[leftIndex] compare:auxiliaryArray[rightIndex]]!=NSOrderedDescending) {
        //左側標記的元素小於等於右側標記的元素
        array[currentIndex] = auxiliaryArray[leftIndex];
        currentIndex++;
        leftIndex++;
    }else{
        //右側標記的元素小於左側標記的元素
        array[currentIndex] = auxiliaryArray[rightIndex];
        currentIndex++;
        rightIndex++;
    }
  }
  //如果完成後左側陣列有剩餘
  if (leftIndex <= middle) {
     for (int i = 0; i<=middle - leftIndex; i++) {
        array[currentIndex +i] = auxiliaryArray[leftIndex +i ];
    }
  }
 }
複製程式碼

此處只寫思路,程式碼細節不細扣

相關文章