欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

IOS

當(dāng)前位置:主頁 > 軟件編程 > IOS >

iOS常見算法以及應(yīng)用知識(shí)點(diǎn)總結(jié)

來源:本站原創(chuàng)|時(shí)間:2020-01-11|欄目:IOS|點(diǎn)擊: 次

算法比較

關(guān)鍵詞

  • 二分
  • 遞歸
  • 分治
  • 回溯

冒泡排序

思想:兩次循環(huán),外層進(jìn)行循環(huán)次數(shù)的控制,內(nèi)層循環(huán),進(jìn)行數(shù)據(jù)之間的比較,大的數(shù)據(jù)上?。ㄏ鲁粒?/p>

#pragma mark - Objective-C
//冒泡排序
- (void)bubbleSort:(id)array{
  if (!([array isKindOfClass:[NSArray class]] || [array isKindOfClass:[NSMutableArray class]])) {
    NSLog(@"傳入的參數(shù)不是數(shù)組類型");
    return;
  }
  NSMutableArray *tmpArr;
  if ([array isKindOfClass:[NSMutableArray class]]) {
    tmpArr = array;
  }else{
    tmpArr = [array mutableCopy];
  }
  for (int i = 0; i<tmpArr.count; i++) {
    for (int j = 0; j < tmpArr.count -1; j++) {
      if ([tmpArr[j] compare:tmpArr[j+1]] == NSOrderedDescending) {
        [tmpArr exchangeObjectAtIndex:i withObjectAtIndex:j+1];
      }
    }
  }
  NSLog(@"排序完的結(jié)果為:%@/n",tmpArr);
}

#pragma mark - C
//冒泡排序
void bubble_sort(int arr[], const int size){
  for (int i = 0; i < size; i++) {
    for (int j = 0; j<size -1 ; j++) {
      if (arr[j] > arr[j+1]) {
        swap(arr[j], arr[j+1]);
      }
    }
  }
}

void swap(int i,int j){
  i = i + j;
  j = i - j;
  i = i - j;
}

快速排序

思想:(快速排序是基于一種叫做“二分”的思想)從數(shù)列中,挑選出一個(gè)元素作為基準(zhǔn),重新排序數(shù)列,所有元素比基準(zhǔn)值小的擺放在基準(zhǔn)前面,所有元素比基準(zhǔn)值大的擺在基準(zhǔn)的后面(相同的數(shù)可以放在任一邊,在這個(gè)分區(qū)退出之后,該基準(zhǔn)就處于數(shù)列的中間位置,遞歸的把小于基準(zhǔn)值元素的子數(shù)列和大于基準(zhǔn)值元素的子數(shù)列排序。

/**
 快速排序
 @param array 任意類型
 @param low 需要排序的數(shù)組的開始位置
 @param high 需要排序的數(shù)組的結(jié)束位置
 */
- (void)quickSort:(NSMutableArray*)array low:(int)low high:(int)high{
  
  if (array == nil || array.count == 0) {
    return;
  }
  if (low >= high) {
    return;
  }
  //取中值
  int middle = low + (high - low)/2;
  NSNumber *prmt = array[middle];
  int i = low;
  int j = high;
  
   //開始排序,使得left<prmt 同時(shí)right>prmt
  while (i <= j) {
//    while ([array[i] compare:prmt] == NSOrderedAscending) {
//      i++;
//    }
    while ([array[i] intValue] < [prmt intValue]) {
      i++;
    }
//    while ([array[j] compare:prmt] == NSOrderedDescending)
    while ([array[j] intValue] > [prmt intValue]) {
      j--;
    }
    
    if(i <= j){
      [array exchangeObjectAtIndex:i withObjectAtIndex:j];
      i++;
      j--;
    }
  }
  
  if (low < j) {
    [self quickSort:array low:low high:j];
  }
  if (high > i) {
    [self quickSort:array low:i high:high];
  }
}

//快速排序
int a[101],n;//定義全局變量,這兩個(gè)變量需要在子函數(shù)中使用
void quicksort(int left,int right)
{
  int i,j,t,temp;
  if(left>right)
    return;
  
  temp=a[left]; //temp中存的就是基準(zhǔn)數(shù)
  i=left;
  j=right;
  while(i!=j){
    //順序很重要,要先從右邊開始找
    while(a[j]>=temp && i<j)
      j--;
    //再找右邊的
    while(a[i]<=temp && i<j)
      i++;
    //交換兩個(gè)數(shù)在數(shù)組中的位置
    if(i<j){
      t=a[i];
      a[i]=a[j];
      a[j]=t;
    }
  }
  //最終將基準(zhǔn)數(shù)歸位
  a[left]=a[i];
  a[i]=temp;
  
  quicksort(left,i-1);//繼續(xù)處理左邊的,這里是一個(gè)遞歸的過程
  quicksort(i+1,right);//繼續(xù)處理右邊的 ,這里是一個(gè)遞歸的過程
}

選擇排序

思想:首先在未排序序列中找到最小元素,存放到排序序列的起始位置,然后,再從剩余未排序元素中繼續(xù)尋找最小元素,然后放到排序序列末尾,以此類推,直到所有元素均排序完畢。

大專欄 iOS常見算法以及應(yīng)用s="line">6

- (void)selectSort:(NSMutableArray *)array
{
  if(array == nil || array.count == 0){
    return;
  }
  
  int min_index;
  for (int i = 0; i < array.count; i++) {
    min_index = i;
    for (int j = i + 1; j<array.count; j++) {
      if ([array[j] compare:array[min_index]] == NSOrderedAscending) {
        [array exchangeObjectAtIndex:j withObjectAtIndex:min_index];
      }
    }
  }
}

插入排序

思想:從第一個(gè)元素開始,該元素可以認(rèn)為已經(jīng)被排序,取出下一個(gè)元素,在已經(jīng)排序的元素序列中從后向前掃描,如果該元素(已排序)大于新元素,將該元素移到下一位置,重復(fù)以上步驟,直到找到已經(jīng)排序的元素小于或者等于新元素的位置,將新元素插入到該位置中

- (void)inserSort:(NSMutableArray *)array
{
  if(array == nil || array.count == 0){
    return;
  }
  
  for (int i = 0; i < array.count; i++) {
    NSNumber *temp = array[i];
    int j = i-1;
    
    while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {
      [array replaceObjectAtIndex:j+1 withObject:array[j]];
      j--;
    }
    
    [array replaceObjectAtIndex:j+1 withObject:temp];
  }
}

希爾(Shell)排序

思想:先將整個(gè)待排記錄序列分割成為若干子序列分別進(jìn)行直接插入排序,待整個(gè)序列中的記錄“基本有序”時(shí),在對(duì)全體進(jìn)行一次直接插入排序。

優(yōu)化:希爾排序是基于插入排序的以下兩點(diǎn)性質(zhì)而提出的改進(jìn)方法的:
(1)插入排序在對(duì)幾乎已經(jīng)排好序的數(shù)據(jù)操作時(shí),效率高,既可以達(dá)到線性排序的效率。
(2)但插入排序一般來說是低效的,因?yàn)椴迦肱判蛎看沃荒軐?shù)據(jù)移動(dòng)一位

OC代碼實(shí)現(xiàn):

//希爾排序,初始的dk值為array.count/2
- (void)ShellSort:(NSMutableArray *)array dk:(int)dk
{
  
  if(array == nil || array.count == 0||dk>=array.count){
    return;
  }
  
  for (int i = 0; i < array.count; i ++) {
    NSNumber *temp = array[i];
    int j = i - dk;
    
      //若第i個(gè)元素大于i-1元素,直接插入。小于的話,移動(dòng)有序表后插入
      while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) {
        [array replaceObjectAtIndex:j+dk withObject:array[j]];
        j-=dk;
      }
      [array replaceObjectAtIndex:j+dk withObject:temp];
    
  }
  
  while (dk>=1) {
    dk = dk/2;
    [self ShellSort:array dk:dk];
  }
}

實(shí)際應(yīng)用

壓縮圖片

+(NSData *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength
{
  // Compress by quality
  CGFloat compression = 1;
  NSData *data = UIImageJPEGRepresentation(image, compression);
  if (data.length < maxLength) return data;
  //采用二分法提高性能
  CGFloat max = 1;
  CGFloat min = 0;
  for (int i = 0; i < 6; ++i) {
    compression = (max + min) / 2;
    data = UIImageJPEGRepresentation(image, compression);
    if (data.length < maxLength * 0.9) {
      min = compression;
    } else if (data.length > maxLength) {
      max = compression;
    } else {
      break;
    }
  }
  UIImage *resultImage = [UIImage imageWithData:data];
  if (data.length < maxLength) return data;
  
  // Compress by size
  NSUInteger lastDataLength = 0;
  while (data.length > maxLength && data.length != lastDataLength) {
    lastDataLength = data.length;
    CGFloat ratio = (CGFloat)maxLength / data.length;
    CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
    UIGraphicsBeginImageContext(size);
    [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    data = UIImageJPEGRepresentation(resultImage, compression);
  }
  
  return data;
}

+(NSData *)compressImage:(UIImage *)image
{
  NSData *data=UIImageJPEGRepresentation(image, 1.0);
  if (data.length>300*1024) {
    
    if (data.length>1024*1024) {//1M以及以上
      
      data=UIImageJPEGRepresentation(image, 0.5);
      
    }else if (data.length>300*1024) {//0.5M-1M
      
      data=UIImageJPEGRepresentation(image, 0.8);
      
    }
  }
  return data;
}

以上就是本次介紹的全部知識(shí)點(diǎn)內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)我們的支持。

上一篇:詳解iOS 輕松獲取當(dāng)前控制器的正確方式

欄    目:IOS

下一篇:iOS仿AirPods彈出動(dòng)畫

本文標(biāo)題:iOS常見算法以及應(yīng)用知識(shí)點(diǎn)總結(jié)

本文地址:http://mengdiqiu.com.cn/a1/IOS/11858.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有