Skip to content

Commit 7a7380d

Browse files
author
wupeng10
committed
排序
1 parent 78dde6c commit 7a7380d

File tree

4 files changed

+316
-19
lines changed

4 files changed

+316
-19
lines changed

iOSInterView/iOSInterView.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
60BAEAE61DBF230700F35185 /* iOSInterViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 60BAEAE51DBF230700F35185 /* iOSInterViewUITests.m */; };
1919
60BAEAF51DBF239A00F35185 /* BinarySortTree.m in Sources */ = {isa = PBXBuildFile; fileRef = 60BAEAF41DBF239A00F35185 /* BinarySortTree.m */; };
2020
60BAEAF81DBF23E100F35185 /* LinkedList.m in Sources */ = {isa = PBXBuildFile; fileRef = 60BAEAF71DBF23E100F35185 /* LinkedList.m */; };
21+
60C0ED8D1DC33634000D4B3A /* NSArraySort.m in Sources */ = {isa = PBXBuildFile; fileRef = 60C0ED8C1DC33634000D4B3A /* NSArraySort.m */; };
2122
/* End PBXBuildFile section */
2223

2324
/* Begin PBXContainerItemProxy section */
@@ -60,6 +61,8 @@
6061
60BAEAF41DBF239A00F35185 /* BinarySortTree.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BinarySortTree.m; sourceTree = "<group>"; };
6162
60BAEAF61DBF23E100F35185 /* LinkedList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LinkedList.h; sourceTree = "<group>"; };
6263
60BAEAF71DBF23E100F35185 /* LinkedList.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LinkedList.m; sourceTree = "<group>"; };
64+
60C0ED8B1DC33634000D4B3A /* NSArraySort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSArraySort.h; sourceTree = "<group>"; };
65+
60C0ED8C1DC33634000D4B3A /* NSArraySort.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSArraySort.m; sourceTree = "<group>"; };
6366
/* End PBXFileReference section */
6467

6568
/* Begin PBXFrameworksBuildPhase section */
@@ -125,6 +128,8 @@
125128
60BAEAC01DBF230700F35185 /* Supporting Files */,
126129
602502731DC1C93C0051BE52 /* NSArray+StackAndQueue.h */,
127130
602502741DC1C93C0051BE52 /* NSArray+StackAndQueue.m */,
131+
60C0ED8B1DC33634000D4B3A /* NSArraySort.h */,
132+
60C0ED8C1DC33634000D4B3A /* NSArraySort.m */,
128133
);
129134
path = iOSInterView;
130135
sourceTree = "<group>";
@@ -291,6 +296,7 @@
291296
602502751DC1C93C0051BE52 /* NSArray+StackAndQueue.m in Sources */,
292297
60BAEAF51DBF239A00F35185 /* BinarySortTree.m in Sources */,
293298
60BAEAC81DBF230700F35185 /* ViewController.m in Sources */,
299+
60C0ED8D1DC33634000D4B3A /* NSArraySort.m in Sources */,
294300
60BAEAC51DBF230700F35185 /* AppDelegate.m in Sources */,
295301
60BAEAF81DBF23E100F35185 /* LinkedList.m in Sources */,
296302
60BAEAC21DBF230700F35185 /* main.m in Sources */,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// NSArray+Sort.h
3+
// iOSInterView
4+
//
5+
// Created by smart on 2016/10/28.
6+
// Copyright © 2016年 smartfutureplayer@gmail.com. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSArraySort : NSObject
12+
13+
/**
14+
* desc: 冒泡排序
15+
* 1. 进行两次便利,第一个for是从 0 - arr.length, 第二次是从0到 arr.length - i - 1;
16+
* 2. 每一趟之后最后一个是最大值;
17+
*
18+
* 时间复杂度: 最好情况o(n), 最差o(n^2), 平均o(n^2)
19+
*
20+
* 空间复杂度: 0(1)
21+
*/
22+
+ (NSMutableArray *)bubbleSort:(NSArray *)array;
23+
24+
25+
/**
26+
* desc: 选择排序
27+
* 1. 分为两趟,第一趟从0-arr.length-1, 第二趟从i + 1到arr.length
28+
* 2. 每趟的结果是最小值在最左边
29+
*
30+
* 时间复杂度:最好情况o(n^2), 最差o(n^2), 平均o(n^2)
31+
*
32+
* 空间复杂度:o(1)
33+
*/
34+
+ (NSMutableArray *)selectSort:(NSArray *)array;
35+
36+
/**
37+
* desc: 插入排序
38+
* 1. 分为两步,第一步,默认从第1个元素开始便利,第二趟,从该元素开始,从后往前遍历插入
39+
* 2. 每趟结束之后前面的有序+1
40+
*
41+
* 时间复杂度:最好情况o(n), 最差o(n^2), 平均o(n^2)
42+
*
43+
* 空间复杂度:o(1)
44+
*/
45+
+ (NSMutableArray *)insertSort:(NSArray *)array;
46+
47+
/**
48+
* desc: 归并排序
49+
* 1. 取中间值为哨兵,将数组分为两个数组,左数组都小于等于哨兵,右边都大于哨兵
50+
* 2. 进行递归合并,合并函数为:定义一个数组,比较两数组中的第一个元素,将小的插入到result中,直到一个数组length为0
51+
* 3. 检查两数组是否长度有不为0的,如果不为0,将其接在result上面
52+
*
53+
* 时间复杂度:o(nlongn)
54+
*
55+
* 空间复杂度:0(n)
56+
*/
57+
+ (NSMutableArray *)mergeSort:(NSArray *)array;
58+
59+
/**
60+
* desc: 快速排序
61+
* 1. 找到中间值,将其分为两个数组,不包括中间的值,左边的小于等于中间值,右边大于中间值;
62+
* 2. 递归调用快排,返回结果为左边数组连接中间值和右边数组
63+
*
64+
* 时间复杂度:最好情况o(nlogn), 最差o(n^2), 平均o(nlogn)
65+
*
66+
* 空间复杂度:o(logn)
67+
*/
68+
+ (NSMutableArray *)quickSort:(NSArray *)array;
69+
70+
/**
71+
* desc: bucket sort
72+
* 1. 找出数组中的最大值和最小值
73+
* 2. 确定桶间距 space = (max - min + 1) / num
74+
* 3. for循环进行分桶
75+
* 1. 确定桶的索引值 index = Math.floor((arr[i] - min) / space)
76+
* 2. 入桶,如果桶对应的索引值存在,则加入其中并进行排序,否则新建该索引下的桶数组
77+
* 4. 将桶中的每个数组连接起来
78+
*/
79+
+ (NSMutableArray *)bucketSort:(NSMutableArray *)array numbersOfBuckets:(NSInteger)num;
80+
81+
@end
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
//
2+
// NSArray+Sort.m
3+
// iOSInterView
4+
//
5+
// Created by smart on 2016/10/28.
6+
// Copyright © 2016年 smartfutureplayer@gmail.com. All rights reserved.
7+
//
8+
9+
#import "NSArraySort.h"
10+
11+
@implementation NSArraySort
12+
13+
// 冒泡排序
14+
+ (NSMutableArray *)bubbleSort:(NSArray *)array {
15+
if (!([array isKindOfClass:[NSArray class]] && [array count] > 0)) {
16+
return nil;
17+
}
18+
19+
NSMutableArray *finalArray = [[NSMutableArray alloc] initWithArray:array];
20+
for (NSInteger i = 0; i < [finalArray count]; i++) {
21+
for (NSInteger j = 0; j < [finalArray count] - i - 1; j++) {
22+
if (array[j] > array[j + 1]) {
23+
id temp = finalArray[j + 1];
24+
finalArray[j + 1] = finalArray[j];
25+
finalArray[j] = temp;
26+
}
27+
}
28+
}
29+
return finalArray;
30+
}
31+
32+
// 选择排序
33+
+ (NSMutableArray *)selectSort:(NSArray *)array {
34+
if (!([array isKindOfClass:[NSArray class]] && [array count] > 0)) {
35+
return nil;
36+
}
37+
38+
NSMutableArray *finalArray = [[NSMutableArray alloc] initWithArray:array];
39+
NSInteger maxIndex = 0;
40+
for (NSInteger i = 0; i < [finalArray count] - 1; i++) {
41+
maxIndex = i;
42+
for (NSInteger j = i + 1; j < [finalArray count]; j++) {
43+
if (finalArray[j] < finalArray[maxIndex]) {
44+
maxIndex = j;
45+
}
46+
}
47+
id temp = finalArray[i];
48+
finalArray[i] = finalArray[maxIndex];
49+
finalArray[maxIndex] = temp;
50+
}
51+
52+
return finalArray;
53+
}
54+
55+
// 插入排序
56+
+ (NSMutableArray *)insertSort:(NSArray *)array {
57+
if (!([array isKindOfClass:[NSArray class]] && [array count] > 0)) {
58+
return nil;
59+
}
60+
61+
NSMutableArray *finalArray = [[NSMutableArray alloc] initWithArray:array];
62+
for (NSInteger i = 1; i < [finalArray count]; i++) {
63+
NSInteger j = i - 1;
64+
id guard = finalArray[i];
65+
while (j >= 0 && finalArray[j] > guard) {
66+
finalArray[j + 1] = finalArray[j];
67+
j--;
68+
}
69+
finalArray[j + 1] = guard;
70+
}
71+
72+
return finalArray;
73+
}
74+
75+
// 归并排序
76+
+ (NSMutableArray *)mergeSort:(NSMutableArray *)array {
77+
if (!([array isKindOfClass:[NSMutableArray class]] && [array count] > 0)) {
78+
return nil;
79+
}
80+
81+
if ([array count] < 2) {
82+
return array;
83+
}
84+
85+
NSMutableArray *finalArray = array;
86+
NSInteger middle = ceilf([array count] / 2.0f);
87+
NSMutableArray *left = [[NSMutableArray alloc] initWithArray:[finalArray subarrayWithRange:NSMakeRange(0, middle)]];
88+
NSMutableArray *right = [[NSMutableArray alloc] initWithArray:[finalArray subarrayWithRange:NSMakeRange(middle, [array count] - middle)]];
89+
90+
return [self merge:[self mergeSort:left] rightArray:[self mergeSort:right]];
91+
}
92+
93+
+ (NSMutableArray *)merge:(NSMutableArray *)left rightArray:(NSMutableArray *)right {
94+
if (!([left isKindOfClass:[NSArray class]] && [right isKindOfClass:[NSArray class]] &&
95+
[left count] > 0 && [right count] > 0)) {
96+
return nil;
97+
}
98+
99+
NSMutableArray *result = [[NSMutableArray alloc] init];
100+
while ([left count] > 0 && [right count] > 0) {
101+
if (left[0] > right[0]) {
102+
[result addObject:right[0]];
103+
[right removeObjectAtIndex:0];
104+
105+
}
106+
else {
107+
[result addObject:left[0]];
108+
[left removeObjectAtIndex:0];
109+
}
110+
}
111+
112+
while ([left count]) {
113+
[result addObject:left[0]];
114+
[left removeObjectAtIndex:0];
115+
}
116+
117+
while ([right count]) {
118+
[result addObject:right[0]];
119+
[right removeObjectAtIndex:0];
120+
}
121+
122+
return result;
123+
}
124+
125+
// 快速排序
126+
+ (NSMutableArray *)quickSort:(NSMutableArray *)array {
127+
if (!([array isKindOfClass:[NSMutableArray class]] && [array count] > 0)) {
128+
return [[NSMutableArray alloc] init];
129+
}
130+
131+
if ([array count] < 2) {
132+
return array;
133+
}
134+
135+
NSMutableArray *left = [[NSMutableArray alloc] init];
136+
NSMutableArray *right = [[NSMutableArray alloc] init];
137+
NSInteger middle = ceilf([array count] / 2.0f);
138+
id middleVal = array[middle];
139+
[array removeObjectAtIndex:middle];
140+
for (NSInteger i = 0; i < [array count]; i++) {
141+
if (middleVal > array[i]) {
142+
[left addObject:array[i]];
143+
}
144+
else {
145+
[right addObject:array[i]];
146+
}
147+
}
148+
left = [self quickSort:left];
149+
right = [self quickSort:right];
150+
151+
NSMutableArray *leftArr = [[NSMutableArray alloc] initWithArray:[left arrayByAddingObject:middleVal]];
152+
NSMutableArray *rightArr = [[NSMutableArray alloc] initWithArray:[leftArr arrayByAddingObjectsFromArray:right]];
153+
154+
return rightArr;
155+
}
156+
157+
// 桶排序
158+
+ (NSMutableArray *)bucketSort:(NSMutableArray *)array numbersOfBuckets:(NSInteger)num {
159+
if (!([array isKindOfClass:[NSArray class]] && [array count] > 0)) {
160+
return nil;
161+
}
162+
163+
NSInteger max = 0;
164+
NSInteger min = 0;
165+
NSInteger space = 0;
166+
NSInteger n = 0;
167+
NSMutableDictionary *bucket = [[NSMutableDictionary alloc] init];
168+
NSMutableArray *result = [[NSMutableArray alloc] init];
169+
170+
for (NSInteger i = 0; i < [array count]; i++) {
171+
max = max > [array[i] intValue] ? max : [array[i] intValue];
172+
min = min < [array[i] intValue] ? min : [array[i] intValue];
173+
}
174+
space = (max - min + 1) / num;
175+
176+
for (NSInteger i = 0; i < [array count]; i++) {
177+
NSNumber *index = [NSNumber numberWithInteger:ceil(([array[i] intValue] - min) / space)];
178+
if (bucket[index]) {
179+
NSInteger k = [bucket[index] count] - 1;
180+
while (k >= 0 && bucket[index][k] > array[i]) {
181+
bucket[index][k + 1] = bucket[index][k];
182+
k--;
183+
}
184+
bucket[index][k + 1] = array[i];
185+
}
186+
else {
187+
bucket[index] = [[NSMutableArray alloc] init];
188+
[bucket[index] addObject:array[i]];
189+
}
190+
}
191+
192+
while (n < num) {
193+
result = [[NSMutableArray alloc] initWithArray:[result arrayByAddingObjectsFromArray:bucket[[NSNumber numberWithInteger:n]]]];
194+
n++;
195+
}
196+
197+
return result;
198+
}
199+
200+
@end

iOSInterView/iOSInterView/ViewController.m

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "BinarySortTree.h"
1111
#import "LinkedList.h"
1212
#import "NSArray+StackAndQueue.h"
13+
#import "NSArraySort.h"
1314

1415
@interface ViewController ()
1516

@@ -25,26 +26,35 @@ - (void)viewDidLoad {
2526
self.view.backgroundColor = [UIColor whiteColor];
2627

2728
// 二叉树测试
28-
NSArray *binaryTree = @[@7,@2,@1,@4,@6,@8,@9,@34,@21,@23,@12];
29-
BinarySortTree *root = [BinarySortTree binarySortTreeCreate:binaryTree];
29+
// NSArray *binaryTree = @[@7,@2,@1,@4,@6,@8,@9,@34,@21,@23,@12];
30+
// BinarySortTree *root = [BinarySortTree binarySortTreeCreate:binaryTree];
31+
//
32+
// // 数组测试
33+
// NSMutableArray *stack = [[NSMutableArray alloc] init];
34+
// [stack push:@1];
35+
// [stack push:@4];
36+
// [stack push:@2];
37+
// [stack push:@5];
38+
// [stack push:@8];
39+
// [stack push:@7];
40+
// [stack push:@6];
41+
//// [stack pop];
42+
//// [stack reverToHeap];
43+
//// [stack sortStackWidthDoubleStack];
44+
// [stack inOrderBinaryTreeWithStack:root];
45+
// NSLog(@"array is %@;", stack);
46+
//
47+
// NSString *charString = @"({[]})";
48+
// [NSArray judgeVaildString:charString];
3049

31-
// 数组测试
32-
NSMutableArray *stack = [[NSMutableArray alloc] init];
33-
[stack push:@1];
34-
[stack push:@4];
35-
[stack push:@2];
36-
[stack push:@5];
37-
[stack push:@8];
38-
[stack push:@7];
39-
[stack push:@6];
40-
// [stack pop];
41-
// [stack reverToHeap];
42-
// [stack sortStackWidthDoubleStack];
43-
[stack inOrderBinaryTreeWithStack:root];
44-
NSLog(@"array is %@;", stack);
45-
46-
NSString *charString = @"({[]})";
47-
[NSArray judgeVaildString:charString];
50+
NSMutableArray *binaryTree = [[NSMutableArray alloc] initWithObjects:@7,@2,@1,@4,@6,@8,@9,@34,@21,@23,@12,nil];
51+
// NSMutableArray *array = [NSArraySort bubbleSort:binaryTree];
52+
// NSMutableArray *array = [NSArraySort selectSort:binaryTree];
53+
// NSMutableArray *array = [NSArraySort insertSort:binaryTree];
54+
// NSMutableArray *array = [NSArraySort mergeSort:binaryTree];
55+
// NSMutableArray *array = [NSArraySort quickSort:binaryTree];
56+
NSMutableArray *array = [NSArraySort bucketSort:binaryTree numbersOfBuckets:5];
57+
NSLog(@"%@", array);
4858
}
4959

5060
@end

0 commit comments

Comments
 (0)