ios oc實現排序演算法

2021-09-02 17:18:15 字數 3691 閱讀 6686

下面是我用oc實現的快速排序,氣泡排序,直接插入排序和折半插入排序,希爾排序,堆排序,直接選擇排序

/*******************************快速排序 start**********************************/

//隨即取 當前取第乙個,首先找到第乙個的位置,然後分成left和right兩組子集 ,分別對left和right繼續執行分割(同上操作)

-(void)quicksort:(nsmutablearray *)list startindex:(nsinteger)startindex endindex:(nsinteger)endindex

}[list exchangeobjectatindex:tempindex withobjectatindex:startindex];

[self quicksort:list startindex:startindex endindex:tempindex-1];

[self quicksort:list startindex:tempindex+1 endindex:endindex];

}/*******************************快速排序 end**********************************/

/*******************************氣泡排序 start**********************************/

//取第乙個 與其鄰接的對比,若大則交換

-(void)bubblesort:(nsmutablearray *)list}}

}/*******************************氣泡排序 end**********************************/

/*******************************直接插入排序 start**********************************/

//從無序表中取出第乙個元素,插入到有序表的合適位置,使有序表仍然有序

-(void)insertsort:(nsmutablearray *)list

[list replaceobjectatindex:j withobject:[nsnumber numberwithint:temp]];

//list[j] = temp;}}

/*******************************直接插入排序 end**********************************/

/*******************************折半插入排序 start**********************************/

//從無序表中取出第乙個元素,利用折半查詢插入到有序表的合適位置,使有序表仍然有序

-(void)binaryinsertsort:(nsmutablearray *)listelse

}//binary search end

//移動3,5,6,[4] 4是當前目標物件 利用binarysearch 找到4應該在有續集的位置,然後向後移動即-->

for(int j = i ; j > left; j--)

[list replaceobjectatindex:left withobject:[nsnumber numberwithint:temp]];}}

/*******************************折半插入排序 end**********************************/

/*******************************希爾排序 start**********************************/

//對直接插入排序優化,創造乙個gap 來對錶進行分割,對分割後的每個子集進行直接插入排序 知道gap==1結束

-(void)shellsort:(nsmutablearray *)list

[list replaceobjectatindex:j withobject:[nsnumber numberwithint:temp]];

}gap = gap / 2;}}

/*******************************希爾排序 end**********************************/

/*******************************堆排序 start**********************************/

//建立最大堆heap 最大/最小優先順序佇列

-(void)createbiggestheap:(nsmutablearray *)list count:(nsinteger)countelse if(leftchildnode > rightchildnode && leftchildnode > parentnode)

}else

}//更新父結點和下乙個子結點

parentindex = currentchildindex;

currentchildindex = 2*currentchildindex + 1;}}

}//每次執行最大堆(索引要前移動 即排除已經排好的最大堆頭元算 交換到list尾部的這個元素)

-(void)heapsort:(nsmutablearray *)list

}/*******************************堆排序 end**********************************/

/*******************************直接選擇排序 start**********************************/

//在物件集中選出最小的 若不是第乙個 則與第乙個交換 在剩餘的物件集中選出最小的 執行前面的步驟

-(void)selectsort:(nsmutablearray *)list

//獲取左子結點為當前子結點

int currentchildindex = 2*i + 1;

// while (currentchildindex <= count - 1) else if(leftchildnode > rightchildnode && leftchildnode > parentnode)

}else

}//更新父結點和下乙個子結點

parentindex = currentchildindex;

currentchildindex = 2*currentchildindex + 1;}}

}//每次執行最大堆(索引要前移動 即排除已經排好的最大堆頭元算 交換到list尾部的這個元素)

-(void)heapsort:(nsmutablearray *)list

}/*******************************堆排序 end**********************************/

/*******************************直接選擇排序 start**********************************/

//在物件集中選出最小的 若不是第乙個 則與第乙個交換 在剩餘的物件集中選出最小的 執行前面的步驟

-(void)selectsort:(nsmutablearray *)list

}if(k != i)}}

/*******************************直接選擇排序 end**********************************/

iOS OC語言 Block底層實現原理

先來簡單介紹一下block block是什麼?蘋果推薦的型別,效率高,在執行中儲存 用來封裝和儲存 有點像函式,block可以在任何時候執行。block和函式的相似性 1 可以儲存 2 有返回值 3 有形參 4 呼叫方式一樣。定義乙個簡單的block 我們再給a賦值為20,此時列印出來a 的值還是1...

排序演算法 選擇排序演算法實現

1 時間複雜度 o n 2 2 選擇排序主要操作是交換和比較 交換次數在0 n 1 總比較次數 n n 1 n 2 n 3 1 n n 1 2 因為交換需要的cpu時間 比較需要的cpu時間 當n比較少時,選擇比冒泡快,減少了不必要的交換,每次交換僅僅是最大值或者最小值與序列起始位置進行狡猾。3 演...

排序演算法實現

自己實現的排序演算法,用來複習 include using namespace std void swap int a,int b void headp adjust int a,int s,int m else break a s temp void bubble sort int a,int l...