iOS 開發常見排序算法及其優化

一 冒泡排序

冒泡排序:原理:冒泡顧名思義,就像氣泡從水底冒出一樣,它的排序方式是:研究了一下,它給人的感覺是像近視眼一樣,它只能看見自己和緊挨著自己的下一個數字,所以它的排序方式也就是將比較元素和緊挨著自己的元素比較看是否交換位置,然后持續這個過程,比較的一直都是緊挨著的兩個元素。下面看代碼吧,再代碼里面再詳細解釋。

冒泡排序(Bubble Sort),是一種計算機科學領域的較簡單的排序算法

它重復地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重復地進行直到沒有再需要交換,也就是說該數列已經排序完成。

這個算法的名字由來是因為越小的元素會經由交換慢慢“浮”到數列的頂端,故名。

由于冒泡排序簡潔的特點,它通常被用來對于計算機程序設計入門的學生介紹算法的概念。

冒泡排序的復雜程度:n2 ? 算法非常穩定

-(void)buddding{

NSMutableArray * titles =[NSMutableArray arrayWithObjects:@"23",@"67",@"9",@"10", nil];

for (int i = 0; i < titles.count; i++) {

for (int j = 0; j < titles.count - i - 1; j++) {

if ([titles[j] integerValue] > [titles[j+1] integerValue]) {

[titles exchangeObjectAtIndex:j withObjectAtIndex:j+1];

}

}

}

}

算法的優化: 我們判定上一次有沒有進行排序 如果沒有則說明已經排序完成

-(void)bubbling{

BOOL flag = NO;? ? //記錄是否存在交換

NSMutableArray * titles = [NSMutableArray arrayWithObjects:@"2",@"3",@"34",@"56",@"12",@"22", nil];

for (int i = 0; i < titles.count; i++) {

flag = NO ;

for (int j = 0; j < titles.count - i - 1; j++) {

if ([titles[j] integerValue] >[titles[j+1] integerValue]) {

flag = YES ;

[titles exchangeObjectAtIndex:j withObjectAtIndex:j+1];

}

}

if (flag == NO) {//退出排序

break ;

}

}

NSLog(@"--===%@",titles);

}

2、選擇排序:

實現思路:

1.?設數組內存放了n個待排數字,數組下標從1開始,到n結束。

2.?i=1

3.?從數組的第i個元素開始到第n個元素,尋找最小的元素。(具體過程為:先設arr[i]為最小,逐一比較,若遇到比之小的則交換)

4.?將上一步找到的最小元素和第i位元素交換。

5.?如果i=n-1算法結束,否則回到第3步

復雜度:

平均時間復雜度:O(n^2)

平均空間復雜度:O(1)

- (void)selectionAscendingOrderSortWithArray:(NSMutableArray *)ascendingArr

{

for (int i = 0; i < ascendingArr.count; i ++) {

for (int j = i + 1; j < ascendingArr.count; j ++) {

if ([ascendingArr[i] integerValue] > [ascendingArr[j] integerValue]) {

int temp = [ascendingArr[i] intValue];

ascendingArr[i] = ascendingArr[j];

ascendingArr[j] = [NSNumber numberWithInt:temp];

? } }}

NSLog(@"選擇升序排序后結果:%@", ascendingArr);

}

3、快速排序:

實現思路:

1.?從數列中挑出一個元素,稱為?"基準"(pivot),

2.?重新排序數列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的后面(相同的數可以到任一邊)。在這個分割之后,該基準是它的最后位置。這個稱為分割(partition)操作。

3.?遞歸地(recursive)把小于基準值元素的子數列和大于基準值元素的子數列排序。

快速排序是基于分治模式處理的,對一個典型子數組A[p...r]排序的分治過程為三個步驟:

1.分解:

A[p..r]被劃分為倆個(可能空)的子數組A[p ..q-1]和A[q+1 ..r],使得

A[p ..q-1] <= A[q] <= A[q+1 ..r]

2.解決:通過遞歸調用快速排序,對子數組A[p ..q-1]和A[q+1 ..r]排序。

3.合并。

遞回的最底部情形,是數列的大小是零或一,也就是永遠都已經被排序好了。雖然一直遞回下去,但是這個算法總會結束,因為在每次的迭代(iteration)中,它至少會把一個元素擺到它最后的位置去。

復雜度:

平均時間復雜度:O(n^2)

平均空間復雜度:O(nlogn)???????O(nlogn)~O(n^2)

#pragma mark - 快速升序排序

- (void)quickAscendingOrderSort:(NSMutableArray *)arr leftIndex:(NSInteger)left rightIndex:(NSInteger)right

{

if (left < right) {

NSInteger temp = [self getMiddleIndex:arr leftIndex:left rightIndex:right];

[self quickAscendingOrderSort:arr leftIndex:left rightIndex:temp - 1];

[self quickAscendingOrderSort:arr leftIndex:temp + 1 rightIndex:right];

}

NSLog(@"快速升序排序結果:%@", arr);

}

- (NSInteger)getMiddleIndex:(NSMutableArray *)arr leftIndex:(NSInteger)left rightIndex:(NSInteger)right

{

NSInteger tempValue = [arr[left] integerValue];

while (left < right) {

while (left < right && tempValue <= [arr[right] integerValue]) {

right --;

}

if (left < right) {

arr[left] = arr[right];

}

while (left < right && [arr[left] integerValue] <= tempValue) {

left ++;

}

if (left < right) {

arr[right] = arr[left];

}

}

arr[left] = [NSNumber numberWithInteger:tempValue];

return left;

}





4、插入排序:

實現思路:

1.?從第一個元素開始,認為該元素已經是排好序的。

2.?取下一個元素,在已經排好序的元素序列中從后向前掃描。

3.?如果已經排好序的序列中元素大于新元素,則將該元素往右移動一個位置。

4.?重復步驟3,直到已排好序的元素小于或等于新元素。

5.?在當前位置插入新元素。

6.?重復步驟2。

#pragma mark - 插入升序排序

- (void)insertionAscendingOrderSort:(NSMutableArray *)ascendingArr

{

for (NSInteger i = 1; i < ascendingArr.count; i ++) {

NSInteger temp = [ascendingArr[i] integerValue];

for (NSInteger j = i - 1; j >= 0 && temp < [ascendingArr[j] integerValue]; j --) {

ascendingArr[j + 1] = ascendingArr[j];

ascendingArr[j] = [NSNumber numberWithInteger:temp];

}

}

NSLog(@"插入升序排序結果:%@",ascendingArr);

}

五、歸并排序:

把序列分成元素盡可能相等的兩半。

把兩半元素分別進行排序。

把兩個有序表合并成一個。

#pragma mark - 歸并升序排序

- (void)megerSortAscendingOrderSort:(NSMutableArray *)ascendingArr

{

NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];

for (NSNumber *num in ascendingArr) {

NSMutableArray *subArray = [NSMutableArray array];

[subArray addObject:num];

[tempArray addObject:subArray];

}

while (tempArray.count != 1) {

NSInteger i = 0;

while (i < tempArray.count - 1) {

tempArray[i] = [self mergeArrayFirstList:tempArray[i] secondList:tempArray[i + 1]];

[tempArray removeObjectAtIndex:i + 1];

i++;

}

}

NSLog(@"歸并升序排序結果:%@", ascendingArr);

}

- (NSArray *)mergeArrayFirstList:(NSArray *)array1 secondList:(NSArray *)array2 {

NSMutableArray *resultArray = [NSMutableArray array];

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;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容