Skip to content

Commit 418e025

Browse files
Merge pull request #195 from scarlettche/master
Objective-C:11.Sort
2 parents 5f17697 + 90afeee commit 418e025

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*.zip
1919
*.tar.gz
2020
*.rar
21+
*.DS_Store
2122

2223
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2324
hs_err_pid*

object-c/11_Sort/Sort.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Sort.h
3+
// test1231231
4+
//
5+
// Created by Scarlett Che on 2018/12/12.
6+
// Copyright © 2018 Scarlett Che. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface Sort : NSObject
14+
// 冒泡排序
15+
+ (NSArray *)bubbleSortWithArray:(NSArray *)array;
16+
17+
// 插入排序
18+
+ (NSArray *)insertionSortWithArray:(NSArray *)array;
19+
20+
// 选择排序
21+
+ (NSArray *)selectionSortWithArray:(NSArray *)array;
22+
@end
23+
24+
NS_ASSUME_NONNULL_END

object-c/11_Sort/Sort.m

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Sort.m
3+
// test1231231
4+
//
5+
// Created by Scarlett Che on 2018/12/12.
6+
// Copyright © 2018 Scarlett Che. All rights reserved.
7+
//
8+
9+
#import "Sort.h"
10+
11+
@implementation Sort
12+
// 冒泡排序
13+
+ (NSArray *)bubbleSortWithArray:(NSArray *)array {
14+
if (array.count <= 1) {
15+
return array;
16+
}
17+
18+
NSMutableArray *aryM = array.mutableCopy;
19+
20+
for (int i = 0; i < aryM.count - 1; i++) {
21+
BOOL flag = NO; // 提前结束标记
22+
for (int j = 0; j < aryM.count - i - 1; j++) {
23+
NSInteger value1 = [aryM[j] integerValue];
24+
NSInteger value2 = [aryM[j + 1] integerValue];
25+
26+
if (value1 > value2) {
27+
flag = YES;
28+
[aryM exchangeObjectAtIndex:j withObjectAtIndex:j+1];
29+
}
30+
}
31+
32+
33+
if (flag == NO) {
34+
// 提前结束
35+
break;
36+
}
37+
}
38+
return aryM.copy;
39+
}
40+
41+
// 插入排序
42+
+ (NSArray *)insertionSortWithArray:(NSArray *)array {
43+
NSMutableArray *aryU = array.mutableCopy;
44+
45+
for (int i = 1; i < aryU.count; i++) {
46+
NSInteger value = [aryU[i] integerValue];
47+
48+
for (int j = 0; j < i; j ++) {
49+
NSInteger sortedValue = [aryU[j] integerValue];
50+
if (value < sortedValue) {
51+
id obj = aryU[i];
52+
[aryU removeObjectAtIndex:i];
53+
[aryU insertObject:obj atIndex:j];
54+
break;
55+
}
56+
}
57+
}
58+
return aryU.copy;
59+
}
60+
61+
// 选择排序
62+
+ (NSArray *)selectionSortWithArray:(NSArray *)array {
63+
if (array.count <= 1) {
64+
return array;
65+
}
66+
67+
NSMutableArray *aryM = array.mutableCopy;
68+
for (int i = 0; i < array.count - 1; i++) {
69+
NSInteger minIndex = NSNotFound;
70+
NSInteger minValue = NSNotFound;
71+
for (int j = i + 1; j < array.count - 1; j++) {
72+
NSInteger tmp = [array[j] integerValue];
73+
if (tmp < minValue) {
74+
minValue = tmp;
75+
minIndex = j;
76+
}
77+
}
78+
79+
if (minIndex != NSNotFound && minValue != NSNotFound && minValue < [array[i] integerValue]) {
80+
[aryM exchangeObjectAtIndex:minIndex withObjectAtIndex:i];
81+
}
82+
83+
}
84+
return array;
85+
}
86+
@end

0 commit comments

Comments
 (0)