forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Scarlett Che
committed
Dec 12, 2018
1 parent
6b50ac0
commit 90afeee
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Sort.h | ||
// test1231231 | ||
// | ||
// Created by Scarlett Che on 2018/12/12. | ||
// Copyright © 2018 Scarlett Che. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface Sort : NSObject | ||
// 冒泡排序 | ||
+ (NSArray *)bubbleSortWithArray:(NSArray *)array; | ||
|
||
// 插入排序 | ||
+ (NSArray *)insertionSortWithArray:(NSArray *)array; | ||
|
||
// 选择排序 | ||
+ (NSArray *)selectionSortWithArray:(NSArray *)array; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// Sort.m | ||
// test1231231 | ||
// | ||
// Created by Scarlett Che on 2018/12/12. | ||
// Copyright © 2018 Scarlett Che. All rights reserved. | ||
// | ||
|
||
#import "Sort.h" | ||
|
||
@implementation Sort | ||
// 冒泡排序 | ||
+ (NSArray *)bubbleSortWithArray:(NSArray *)array { | ||
if (array.count <= 1) { | ||
return array; | ||
} | ||
|
||
NSMutableArray *aryM = array.mutableCopy; | ||
|
||
for (int i = 0; i < aryM.count - 1; i++) { | ||
BOOL flag = NO; // 提前结束标记 | ||
for (int j = 0; j < aryM.count - i - 1; j++) { | ||
NSInteger value1 = [aryM[j] integerValue]; | ||
NSInteger value2 = [aryM[j + 1] integerValue]; | ||
|
||
if (value1 > value2) { | ||
flag = YES; | ||
[aryM exchangeObjectAtIndex:j withObjectAtIndex:j+1]; | ||
} | ||
} | ||
|
||
|
||
if (flag == NO) { | ||
// 提前结束 | ||
break; | ||
} | ||
} | ||
return aryM.copy; | ||
} | ||
|
||
// 插入排序 | ||
+ (NSArray *)insertionSortWithArray:(NSArray *)array { | ||
NSMutableArray *aryU = array.mutableCopy; | ||
|
||
for (int i = 1; i < aryU.count; i++) { | ||
NSInteger value = [aryU[i] integerValue]; | ||
|
||
for (int j = 0; j < i; j ++) { | ||
NSInteger sortedValue = [aryU[j] integerValue]; | ||
if (value < sortedValue) { | ||
id obj = aryU[i]; | ||
[aryU removeObjectAtIndex:i]; | ||
[aryU insertObject:obj atIndex:j]; | ||
break; | ||
} | ||
} | ||
} | ||
return aryU.copy; | ||
} | ||
|
||
// 选择排序 | ||
+ (NSArray *)selectionSortWithArray:(NSArray *)array { | ||
if (array.count <= 1) { | ||
return array; | ||
} | ||
|
||
NSMutableArray *aryM = array.mutableCopy; | ||
for (int i = 0; i < array.count - 1; i++) { | ||
NSInteger minIndex = NSNotFound; | ||
NSInteger minValue = NSNotFound; | ||
for (int j = i + 1; j < array.count - 1; j++) { | ||
NSInteger tmp = [array[j] integerValue]; | ||
if (tmp < minValue) { | ||
minValue = tmp; | ||
minIndex = j; | ||
} | ||
} | ||
|
||
if (minIndex != NSNotFound && minValue != NSNotFound && minValue < [array[i] integerValue]) { | ||
[aryM exchangeObjectAtIndex:minIndex withObjectAtIndex:i]; | ||
} | ||
|
||
} | ||
return array; | ||
} | ||
@end |