/**
最好時間複雜度是O(n)
最壞時間複雜度是O(n^2)
平均時間複雜度:O(n^2)
平均空間複雜度:O(1)
*/
- (void)foolSortArray:(NSMutableArray *)array {
for (int i = 0; i < array.count-1; i++) {
for (int j = 0; j < array.count-i-1; j++) {
if (array[j] > array[j+1]) {
id tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
}
複製代碼
/**
最好時間複雜度是O(n^2)
最壞時間複雜度是O(n^2)
平均時間複雜度:O(n^2)
平均空間複雜度:O(1)
*/
- (void)selectSortArray:(NSMutableArray *)array {
for (int i = 0; i < array.count-1; i++) {
for (int j = i+1; j < array.count; j++) {
if (array[i] > array[j]) {
id tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
複製代碼
- (void)insertSortArray:(NSMutableArray *)array {
for (int i = 1; i < [array count]; i++) {
int j = i;
NSInteger temp = [[array objectAtIndex:i] integerValue];
while (j > 0 && temp < [[array objectAtIndex:j - 1] integerValue]) {
[array replaceObjectAtIndex:j withObject:[array objectAtIndex:(j - 1)]];
j--;
}
[array replaceObjectAtIndex:j withObject:[NSNumber numberWithInteger:temp]];
}
}
複製代碼
希爾排序git
希爾排序是把記錄按下標的必定增量分組,對每組使用直接插入排序算法排序;隨着增量逐漸減小,每組包含的關鍵詞愈來愈多,當增量減至1時,整個文件恰被分紅一組,算法便終止。`github
增量: 插入排序只能與相鄰的元素進行比較,而希爾排序則是進行跳躍比較,而增量就是步長。
算法
/**
最優的增量在最壞的狀況下卻爲O(n²⁄³),最壞的狀況下時間複雜度仍爲O(n²)
須要注意的是,增量序列的最後一個增量值必須等於1才行
另外因爲記錄是跳躍式的移動,希爾排序並非一種穩定的排序算法
*/
- (void)shellSortArray:(NSMutableArray *)array {
int count = (int)array.count;
// 初始增量爲數組長度的一半,而後每次除以2取整
for (int increment = count/2; increment > 0; increment/=2) {
// 初始下標設爲第一個增量的位置,而後遞增
for (int i = increment; i<count; i++) {
// 獲取當前位置
int j = i;
// 而後將此位置以前的元素,按照增量進行跳躍式比較
while (j-increment>=0 && [array[j] integerValue]<[array[j-increment] integerValue]) {
[array exchangeObjectAtIndex:j withObjectAtIndex:j-increment];
j-=increment;
}
}
}
}
複製代碼
/**
最理想狀況算法時間複雜度O(nlogn),最壞O(n^2),平均O(nlogn)
平均空間複雜度:O(nlogn) O(nlogn)~O(n^2)
*/
- (void)quickSortArray:(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 quickSortArray:array withLeftIndex:leftIndex andRightIndex:i - 1];
// 排序基準數右邊的
[self quickSortArray:array withLeftIndex:i + 1 andRightIndex:rightIndex];
}
複製代碼
堆排序
堆
shell
堆(英語:heap)是計算機科學中一類特殊的數據結構的統稱
堆老是知足下列性質: 1. 堆中某個節點的值老是不大於或不小於其父節點的值; 2. 堆老是一棵徹底二叉樹
將根節點最大的堆叫作最大堆或大根堆,根節點最小的堆叫作最小堆或小根堆數組
徹底二叉樹
bash
若設二叉樹的深度爲h,除第 h 層外,其它各層 (1~h-1) 的結點數都達到最大個數,第 h 層全部的結點都連續集中在最左邊,這就是徹底二叉樹。數據結構
/**
時間複雜度爲O(nlogn)
*/
- (void)heapSortArray:(NSMutableArray *)heapList len:(NSInteger)len {
// 創建堆,從最底層的父節點開始
for(NSInteger i = (heapList.count/2 -1); i>=0; i--)
[self adjustHeap:heapList location:i len:heapList.count];
for(NSInteger i = heapList.count -1; i >= 0; i--){
NSInteger maxEle = ((NSString *)heapList[0]).integerValue;
heapList[0] = heapList[i];
heapList[i] = @(maxEle).stringValue;
[self adjustHeap:heapList location:0 len:i];
}
}
- (void)adjustHeap:(NSMutableArray *)heapList location:(NSInteger)p len:(NSInteger)len {
NSInteger curParent = ((NSString *)heapList[p]).integerValue;
NSInteger child = 2*p + 1;
while (child < len) {
// left < right
if (child+1 < len && ((NSString *)heapList[child]).integerValue < ((NSString *)heapList[child+1]).integerValue) {
child ++;
}
if (curParent < ((NSString *)heapList[child]).integerValue) {
heapList[p] = heapList[child];
p = child;
child = 2*p + 1;
}
else
break;
}
heapList[p] = @(curParent).stringValue;
}
複製代碼
歸併排序(MERGE-SORT)是創建在歸併操做上的一種有效的排序算法,該算法是採用分治法(Divide and Conquer)的一個很是典型的應用。將已有序的子序列合併,獲得徹底有序的序列;即先使每一個子序列有序,再使子序列段間有序。若將兩個有序表合併成一個有序表,稱爲二路歸併。ide
/**
時間複雜度爲O(nlogn)
(1)「分解」——將序列每次折半劃分
(2)「合併」——將劃分後的序列段兩兩合併後排序
*/
- (NSArray *)mergeSortArray:(NSMutableArray *)array {
// 排序數組
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
// 第一趟排序是的子數組個數爲ascendingArr.count
for (NSNumber *num in array) {
NSMutableArray *subArray = [NSMutableArray array];
[subArray addObject:num];
[tempArray addObject:subArray];
}
/**
分解操做 每一次歸併操做
當數組個數爲偶數時tempArray.count/2; 當數組個數爲奇數時tempArray.count/2+1; 當tempArray.count == 1時,歸併排序完成
*/
while (tempArray.count != 1) {
NSInteger i = 0;
// 當數組個數爲偶數時 進行合併操做, 當數組個數爲奇數時,最後一位輪空
while (i < tempArray.count - 1) {
// 將i 與i+1 進行合併操做 將合併結果放入i位置上 將i+1位置上的元素刪除
tempArray[i] = [self mergeArrayFirstList:tempArray[i] secondList:tempArray[i + 1]];
[tempArray removeObjectAtIndex:i + 1];
// i++ 繼續下一循環的合併操做
i++;
}
}
return tempArray.copy;
}
// 合併
- (NSArray *)mergeArrayFirstList:(NSArray *)array1 secondList:(NSArray *)array2 {
// 合併序列數組
NSMutableArray *resultArray = [NSMutableArray array];
// firstIndex是第一段序列的下標 secondIndex是第二段序列的下標
NSInteger firstIndex = 0, secondIndex = 0;
// 掃描第一段和第二段序列,直到有一個掃描結束
while (firstIndex < array1.count && secondIndex < array2.count) {
// 判斷第一段和第二段取出的數哪一個更小,將其存入合併序列,並繼續向下掃描
if ([array1[firstIndex] floatValue] < [array2[secondIndex] floatValue]) {
[resultArray addObject:array1[firstIndex]];
firstIndex++;
} else {
[resultArray addObject:array2[secondIndex]];
secondIndex++;
}
}
// 若第一段序列還沒掃描完,將其所有複製到合併序列
while (firstIndex < array1.count) {
[resultArray addObject:array1[firstIndex]];
firstIndex++;
}
// 若第二段序列還沒掃描完,將其所有複製到合併序列
while (secondIndex < array2.count) {
[resultArray addObject:array2[secondIndex]];
secondIndex++;
}
// 返回合併序列數組
return resultArray.copy;
}
複製代碼
/**
二分查找法只適用於已經排好序的查找
*/
- (NSInteger)dichotomySearch:(NSArray *)array target:(id)key {
NSInteger left = 0;
NSInteger right = [array count] - 1;
NSInteger middle = [array count] / 2;
while (right >= left) {
middle = (right + left) / 2;
if (array[middle] == key) {
return middle;
}
if (array[middle] > key) {
right = middle - 1;
}else if (array[middle] < key) {
left = middle + 1;
}
}
return -1;
}
複製代碼
- (NSInteger)recursion0:(NSInteger) n {
if (n <= 1) return n;
return [self recursion0:n-1] + [self recursion0:n-2];
}
複製代碼
- (NSInteger)recursion1: (NSInteger)n {
if (n == 0) { //遞歸邊界
return 1;
}
return n*[self recursion1:(n-1)];//遞歸公式
}
複製代碼