├── 1.gif ├── 2.gif ├── 3.gif ├── 4.gif ├── 5.gif ├── NSMutableArray+ZSSort.h ├── NSMutableArray+ZSSort.m ├── README.md ├── Sort.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── zhangshu.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── zhangshu.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── Sort.xcscheme │ └── xcschememanagement.plist ├── Sort ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m ├── SortTests ├── Info.plist └── SortTests.m └── SortUITests ├── Info.plist └── SortUITests.m /1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyte/sort/418975f5915eac6999df2eb9f996fde5f7c3dd6a/1.gif -------------------------------------------------------------------------------- /2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyte/sort/418975f5915eac6999df2eb9f996fde5f7c3dd6a/2.gif -------------------------------------------------------------------------------- /3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyte/sort/418975f5915eac6999df2eb9f996fde5f7c3dd6a/3.gif -------------------------------------------------------------------------------- /4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyte/sort/418975f5915eac6999df2eb9f996fde5f7c3dd6a/4.gif -------------------------------------------------------------------------------- /5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyte/sort/418975f5915eac6999df2eb9f996fde5f7c3dd6a/5.gif -------------------------------------------------------------------------------- /NSMutableArray+ZSSort.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSMutableArray+ZSSort.h 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSMutableArray (ZSSort) 12 | //block块作为参数传给函数 13 | typedef NSComparisonResult(^ZSCompare)(id obj1,id obj2); //比较函数 14 | typedef void(^ZSExchange)(id obj1,id obj2); //交换函数 15 | 16 | - (void)zs_selectSort:(ZSCompare)compare withCallback:(ZSExchange)exchange; //选择排序 17 | - (void)zs_bubbleSort:(ZSCompare)compare withCallback:(ZSExchange)exchange; 18 | - (void)zs_insertSort:(ZSCompare)compare withCallback:(ZSExchange)exchange; 19 | - (void)zs_quickSort:(ZSCompare)compare withCallback:(ZSExchange)exchange; 20 | - (void)zs_heapSort:(ZSCompare)compare withCallback:(ZSExchange)exchange; 21 | @end 22 | -------------------------------------------------------------------------------- /NSMutableArray+ZSSort.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSMutableArray+ZSSort.m 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import "NSMutableArray+ZSSort.h" 10 | 11 | @implementation NSMutableArray (ZSSort) 12 | 13 | - (void)zs_exchangeWithIndexA:(NSInteger)indexA indexB:(NSInteger)indexB Change:(ZSExchange)exchange { 14 | id temp = self[indexA]; 15 | self[indexA] = self[indexB]; 16 | self[indexB] = temp; 17 | if (exchange) { 18 | exchange(self[indexA],self[indexB]); 19 | } 20 | } 21 | 22 | #pragma mark - 选择排序 23 | - (void)zs_selectSort:(ZSCompare)compare withCallback:(ZSExchange)exchange{ 24 | if (self.count == 0) { 25 | return; 26 | } 27 | for (int i = 0; i < self.count; i++) { 28 | for (int j = i+1; j < self.count; j++) { 29 | if (compare(self[i],self[j])==NSOrderedDescending) { 30 | [self zs_exchangeWithIndexA:i indexB:j Change:exchange]; 31 | } 32 | } 33 | } 34 | } 35 | 36 | #pragma mark - 冒泡排序 37 | - (void)zs_bubbleSort:(ZSCompare)compare withCallback:(ZSExchange)exchange{ 38 | if (self.count == 0) { 39 | return; 40 | } 41 | for (NSInteger i = self.count-1; i > 0; i --) { 42 | for (NSInteger j = 0; j < i; j++) { 43 | if (compare(self[j],self[j+1])==NSOrderedDescending) { 44 | [self zs_exchangeWithIndexA:j indexB:j+1 Change:exchange]; 45 | } 46 | } 47 | } 48 | } 49 | 50 | #pragma mark - 插入排序 51 | - (void)zs_insertSort:(ZSCompare)compare withCallback:(ZSExchange)exchange{ 52 | if (self.count == 0) { 53 | return; 54 | } 55 | for (NSInteger i = 0; i < self.count-1; i++) { 56 | for (NSInteger j = i+1; j > 0; j--) { 57 | if (compare(self[j],self[j-1])==NSOrderedAscending) { 58 | [self zs_exchangeWithIndexA:j indexB:j-1 Change:exchange]; 59 | } 60 | else{ 61 | break; 62 | } 63 | } 64 | } 65 | } 66 | 67 | #pragma mark - 原始快速排序 68 | - (void)zs_quickSort:(ZSCompare)compare withCallback:(ZSExchange)exchange{ 69 | if (self.count == 0) { 70 | return; 71 | } 72 | [self zs_quickSortWithLow:0 High:self.count-1 Compare:compare Callback:exchange]; 73 | } 74 | 75 | //递归函数,必须有返回 76 | - (void)zs_quickSortWithLow:(NSInteger)low High:(NSInteger)high Compare:(ZSCompare)compare Callback:(ZSExchange)exchange{ 77 | if (low >= high) { 78 | return; 79 | } 80 | NSInteger pivotIndex = [self zs_pivotIndexWithLeft:low Right:high Compare:compare Callback:exchange]; 81 | [self zs_quickSortWithLow:low High:pivotIndex-1 Compare:compare Callback:exchange]; 82 | [self zs_quickSortWithLow:pivotIndex+1 High:high Compare:compare Callback:exchange]; 83 | } 84 | 85 | - (NSInteger)zs_pivotIndexWithLeft:(NSInteger)left Right:(NSInteger)right Compare:(ZSCompare)compare Callback:(ZSExchange)exchange{ 86 | NSInteger i = left; 87 | NSInteger j = right; 88 | id pivot = self[left]; 89 | while (i < j) { 90 | //从右边找小的,如果右边大于等于pivot,则右游标向左移动 91 | while (i < j && compare(pivot,self[j])!= NSOrderedDescending) { 92 | j--; 93 | } 94 | //右边的值小于pivot 95 | if (i < j) { 96 | [self zs_exchangeWithIndexA:i indexB:j Change:exchange]; 97 | i++; 98 | } 99 | //从左边找大的,如果左边小于等于pivot,则左游标向右移动 100 | while (i < j && compare(self[i],pivot)!=NSOrderedDescending) { 101 | i++; 102 | } 103 | //左边的值大于pivot 104 | if (i < j) { 105 | [self zs_exchangeWithIndexA:i indexB:j Change:exchange]; 106 | j--; 107 | } 108 | 109 | } 110 | return i; 111 | } 112 | 113 | #pragma mark - 堆排序 114 | - (void)zs_heapSort:(ZSCompare)compare withCallback:(ZSExchange)exchange{ 115 | if (self.count == 0) { 116 | return; 117 | } 118 | [self insertObject:[[NSNull alloc]init] atIndex:0]; 119 | //初始化最大堆排序 120 | for (NSInteger index = (self.count-1)/2; index > 0; index--) { 121 | [self sinkIndex:index bottomIndex:self.count-1 compare:compare Callback:exchange]; 122 | } 123 | //第一次交换根结点与最后一个元素,然后不断沉底第一个元素 124 | for (NSInteger index = self.count-1; index > 1; index--) { 125 | [self zs_exchangeWithIndexA:1 indexB:index Change:exchange]; 126 | [self sinkIndex:1 bottomIndex:index-1 compare:compare Callback:exchange]; 127 | } 128 | [self removeObjectAtIndex:0]; 129 | } 130 | 131 | //第一个参数是需要沉底的元素索引值,第二个元素是能允许沉底的最大索引值 132 | - (void)sinkIndex:(NSInteger)currentIndex bottomIndex:(NSInteger)bottomIndex compare:(ZSCompare)compare Callback:(ZSExchange)exchange{ 133 | //数组第一个数为空,就是为了子左结点刚好是2倍,子右节点是2倍+1 134 | for (NSInteger maxIndex = 2*currentIndex; maxIndex <= bottomIndex ; maxIndex *= 2) { 135 | //找到子左右节点的最大值,父节点与这个值比较,首先必须保证右节点要存在 136 | if ((maxIndex+1)<=bottomIndex && compare(self[maxIndex],self[maxIndex+1])==NSOrderedAscending) { 137 | ++ maxIndex; 138 | } 139 | //比较父结点与子左右节点的最大值,如果小于,则交换,否则break跳出 140 | if (compare(self[currentIndex],self[maxIndex])==NSOrderedAscending) { 141 | [self zs_exchangeWithIndexA:currentIndex indexB:maxIndex Change:exchange]; 142 | } 143 | else{ 144 | break; 145 | } 146 | //将当前需要改变的节点位置currentIndex变成maxIndex,这样就可以继续沉底 147 | currentIndex = maxIndex; 148 | } 149 | } 150 | @end 151 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## objective-c 图像化演示5种常见排序算法 3 | 4 | ### 选择排序 5 | ![选择排序](https://github.com/happyte/sort/blob/master/1.gif) 6 | 7 | ### 冒泡排序 8 | ![冒泡排序](https://github.com/happyte/sort/blob/master/2.gif) 9 | 10 | ### 插入排序 11 | ![插入排序](https://github.com/happyte/sort/blob/master/3.gif) 12 | 13 | ### 快速排序 14 | ![快速排序](https://github.com/happyte/sort/blob/master/4.gif) 15 | 16 | ### 堆排序 17 | ![堆排序](https://github.com/happyte/sort/blob/master/5.gif) 18 | 19 | 20 | -------------------------------------------------------------------------------- /Sort.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 5AEC53A91DEC46BC00542FE2 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5AEC53A81DEC46BC00542FE2 /* main.m */; }; 11 | 5AEC53AC1DEC46BC00542FE2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5AEC53AB1DEC46BC00542FE2 /* AppDelegate.m */; }; 12 | 5AEC53AF1DEC46BC00542FE2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5AEC53AE1DEC46BC00542FE2 /* ViewController.m */; }; 13 | 5AEC53B21DEC46BC00542FE2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5AEC53B01DEC46BC00542FE2 /* Main.storyboard */; }; 14 | 5AEC53B41DEC46BC00542FE2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5AEC53B31DEC46BC00542FE2 /* Assets.xcassets */; }; 15 | 5AEC53B71DEC46BC00542FE2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5AEC53B51DEC46BC00542FE2 /* LaunchScreen.storyboard */; }; 16 | 5AEC53C21DEC46BD00542FE2 /* SortTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5AEC53C11DEC46BD00542FE2 /* SortTests.m */; }; 17 | 5AEC53CD1DEC46BD00542FE2 /* SortUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5AEC53CC1DEC46BD00542FE2 /* SortUITests.m */; }; 18 | 5AEC53DC1DEC5DE300542FE2 /* NSMutableArray+ZSSort.m in Sources */ = {isa = PBXBuildFile; fileRef = 5AEC53DB1DEC5DE300542FE2 /* NSMutableArray+ZSSort.m */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | 5AEC53BE1DEC46BD00542FE2 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = 5AEC539C1DEC46BC00542FE2 /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = 5AEC53A31DEC46BC00542FE2; 27 | remoteInfo = Sort; 28 | }; 29 | 5AEC53C91DEC46BD00542FE2 /* PBXContainerItemProxy */ = { 30 | isa = PBXContainerItemProxy; 31 | containerPortal = 5AEC539C1DEC46BC00542FE2 /* Project object */; 32 | proxyType = 1; 33 | remoteGlobalIDString = 5AEC53A31DEC46BC00542FE2; 34 | remoteInfo = Sort; 35 | }; 36 | /* End PBXContainerItemProxy section */ 37 | 38 | /* Begin PBXFileReference section */ 39 | 5AEC53A41DEC46BC00542FE2 /* Sort.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sort.app; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | 5AEC53A81DEC46BC00542FE2 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 41 | 5AEC53AA1DEC46BC00542FE2 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 42 | 5AEC53AB1DEC46BC00542FE2 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 43 | 5AEC53AD1DEC46BC00542FE2 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 44 | 5AEC53AE1DEC46BC00542FE2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 45 | 5AEC53B11DEC46BC00542FE2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 46 | 5AEC53B31DEC46BC00542FE2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 47 | 5AEC53B61DEC46BC00542FE2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 48 | 5AEC53B81DEC46BC00542FE2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 49 | 5AEC53BD1DEC46BD00542FE2 /* SortTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SortTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 50 | 5AEC53C11DEC46BD00542FE2 /* SortTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SortTests.m; sourceTree = ""; }; 51 | 5AEC53C31DEC46BD00542FE2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 52 | 5AEC53C81DEC46BD00542FE2 /* SortUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SortUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 53 | 5AEC53CC1DEC46BD00542FE2 /* SortUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SortUITests.m; sourceTree = ""; }; 54 | 5AEC53CE1DEC46BD00542FE2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 55 | 5AEC53DA1DEC5DE300542FE2 /* NSMutableArray+ZSSort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSMutableArray+ZSSort.h"; path = "../NSMutableArray+ZSSort.h"; sourceTree = ""; }; 56 | 5AEC53DB1DEC5DE300542FE2 /* NSMutableArray+ZSSort.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSMutableArray+ZSSort.m"; path = "../NSMutableArray+ZSSort.m"; sourceTree = ""; }; 57 | /* End PBXFileReference section */ 58 | 59 | /* Begin PBXFrameworksBuildPhase section */ 60 | 5AEC53A11DEC46BC00542FE2 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | ); 65 | runOnlyForDeploymentPostprocessing = 0; 66 | }; 67 | 5AEC53BA1DEC46BD00542FE2 /* Frameworks */ = { 68 | isa = PBXFrameworksBuildPhase; 69 | buildActionMask = 2147483647; 70 | files = ( 71 | ); 72 | runOnlyForDeploymentPostprocessing = 0; 73 | }; 74 | 5AEC53C51DEC46BD00542FE2 /* Frameworks */ = { 75 | isa = PBXFrameworksBuildPhase; 76 | buildActionMask = 2147483647; 77 | files = ( 78 | ); 79 | runOnlyForDeploymentPostprocessing = 0; 80 | }; 81 | /* End PBXFrameworksBuildPhase section */ 82 | 83 | /* Begin PBXGroup section */ 84 | 5AEC539B1DEC46BC00542FE2 = { 85 | isa = PBXGroup; 86 | children = ( 87 | 5AEC53A61DEC46BC00542FE2 /* Sort */, 88 | 5AEC53C01DEC46BD00542FE2 /* SortTests */, 89 | 5AEC53CB1DEC46BD00542FE2 /* SortUITests */, 90 | 5AEC53A51DEC46BC00542FE2 /* Products */, 91 | ); 92 | sourceTree = ""; 93 | }; 94 | 5AEC53A51DEC46BC00542FE2 /* Products */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 5AEC53A41DEC46BC00542FE2 /* Sort.app */, 98 | 5AEC53BD1DEC46BD00542FE2 /* SortTests.xctest */, 99 | 5AEC53C81DEC46BD00542FE2 /* SortUITests.xctest */, 100 | ); 101 | name = Products; 102 | sourceTree = ""; 103 | }; 104 | 5AEC53A61DEC46BC00542FE2 /* Sort */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | 5AEC53AA1DEC46BC00542FE2 /* AppDelegate.h */, 108 | 5AEC53AB1DEC46BC00542FE2 /* AppDelegate.m */, 109 | 5AEC53DA1DEC5DE300542FE2 /* NSMutableArray+ZSSort.h */, 110 | 5AEC53DB1DEC5DE300542FE2 /* NSMutableArray+ZSSort.m */, 111 | 5AEC53AD1DEC46BC00542FE2 /* ViewController.h */, 112 | 5AEC53AE1DEC46BC00542FE2 /* ViewController.m */, 113 | 5AEC53B01DEC46BC00542FE2 /* Main.storyboard */, 114 | 5AEC53B31DEC46BC00542FE2 /* Assets.xcassets */, 115 | 5AEC53B51DEC46BC00542FE2 /* LaunchScreen.storyboard */, 116 | 5AEC53B81DEC46BC00542FE2 /* Info.plist */, 117 | 5AEC53A71DEC46BC00542FE2 /* Supporting Files */, 118 | ); 119 | path = Sort; 120 | sourceTree = ""; 121 | }; 122 | 5AEC53A71DEC46BC00542FE2 /* Supporting Files */ = { 123 | isa = PBXGroup; 124 | children = ( 125 | 5AEC53A81DEC46BC00542FE2 /* main.m */, 126 | ); 127 | name = "Supporting Files"; 128 | sourceTree = ""; 129 | }; 130 | 5AEC53C01DEC46BD00542FE2 /* SortTests */ = { 131 | isa = PBXGroup; 132 | children = ( 133 | 5AEC53C11DEC46BD00542FE2 /* SortTests.m */, 134 | 5AEC53C31DEC46BD00542FE2 /* Info.plist */, 135 | ); 136 | path = SortTests; 137 | sourceTree = ""; 138 | }; 139 | 5AEC53CB1DEC46BD00542FE2 /* SortUITests */ = { 140 | isa = PBXGroup; 141 | children = ( 142 | 5AEC53CC1DEC46BD00542FE2 /* SortUITests.m */, 143 | 5AEC53CE1DEC46BD00542FE2 /* Info.plist */, 144 | ); 145 | path = SortUITests; 146 | sourceTree = ""; 147 | }; 148 | /* End PBXGroup section */ 149 | 150 | /* Begin PBXNativeTarget section */ 151 | 5AEC53A31DEC46BC00542FE2 /* Sort */ = { 152 | isa = PBXNativeTarget; 153 | buildConfigurationList = 5AEC53D11DEC46BD00542FE2 /* Build configuration list for PBXNativeTarget "Sort" */; 154 | buildPhases = ( 155 | 5AEC53A01DEC46BC00542FE2 /* Sources */, 156 | 5AEC53A11DEC46BC00542FE2 /* Frameworks */, 157 | 5AEC53A21DEC46BC00542FE2 /* Resources */, 158 | ); 159 | buildRules = ( 160 | ); 161 | dependencies = ( 162 | ); 163 | name = Sort; 164 | productName = Sort; 165 | productReference = 5AEC53A41DEC46BC00542FE2 /* Sort.app */; 166 | productType = "com.apple.product-type.application"; 167 | }; 168 | 5AEC53BC1DEC46BD00542FE2 /* SortTests */ = { 169 | isa = PBXNativeTarget; 170 | buildConfigurationList = 5AEC53D41DEC46BD00542FE2 /* Build configuration list for PBXNativeTarget "SortTests" */; 171 | buildPhases = ( 172 | 5AEC53B91DEC46BD00542FE2 /* Sources */, 173 | 5AEC53BA1DEC46BD00542FE2 /* Frameworks */, 174 | 5AEC53BB1DEC46BD00542FE2 /* Resources */, 175 | ); 176 | buildRules = ( 177 | ); 178 | dependencies = ( 179 | 5AEC53BF1DEC46BD00542FE2 /* PBXTargetDependency */, 180 | ); 181 | name = SortTests; 182 | productName = SortTests; 183 | productReference = 5AEC53BD1DEC46BD00542FE2 /* SortTests.xctest */; 184 | productType = "com.apple.product-type.bundle.unit-test"; 185 | }; 186 | 5AEC53C71DEC46BD00542FE2 /* SortUITests */ = { 187 | isa = PBXNativeTarget; 188 | buildConfigurationList = 5AEC53D71DEC46BD00542FE2 /* Build configuration list for PBXNativeTarget "SortUITests" */; 189 | buildPhases = ( 190 | 5AEC53C41DEC46BD00542FE2 /* Sources */, 191 | 5AEC53C51DEC46BD00542FE2 /* Frameworks */, 192 | 5AEC53C61DEC46BD00542FE2 /* Resources */, 193 | ); 194 | buildRules = ( 195 | ); 196 | dependencies = ( 197 | 5AEC53CA1DEC46BD00542FE2 /* PBXTargetDependency */, 198 | ); 199 | name = SortUITests; 200 | productName = SortUITests; 201 | productReference = 5AEC53C81DEC46BD00542FE2 /* SortUITests.xctest */; 202 | productType = "com.apple.product-type.bundle.ui-testing"; 203 | }; 204 | /* End PBXNativeTarget section */ 205 | 206 | /* Begin PBXProject section */ 207 | 5AEC539C1DEC46BC00542FE2 /* Project object */ = { 208 | isa = PBXProject; 209 | attributes = { 210 | LastUpgradeCheck = 0810; 211 | ORGANIZATIONNAME = com.zs; 212 | TargetAttributes = { 213 | 5AEC53A31DEC46BC00542FE2 = { 214 | CreatedOnToolsVersion = 8.1; 215 | ProvisioningStyle = Automatic; 216 | }; 217 | 5AEC53BC1DEC46BD00542FE2 = { 218 | CreatedOnToolsVersion = 8.1; 219 | ProvisioningStyle = Automatic; 220 | TestTargetID = 5AEC53A31DEC46BC00542FE2; 221 | }; 222 | 5AEC53C71DEC46BD00542FE2 = { 223 | CreatedOnToolsVersion = 8.1; 224 | ProvisioningStyle = Automatic; 225 | TestTargetID = 5AEC53A31DEC46BC00542FE2; 226 | }; 227 | }; 228 | }; 229 | buildConfigurationList = 5AEC539F1DEC46BC00542FE2 /* Build configuration list for PBXProject "Sort" */; 230 | compatibilityVersion = "Xcode 3.2"; 231 | developmentRegion = English; 232 | hasScannedForEncodings = 0; 233 | knownRegions = ( 234 | en, 235 | Base, 236 | ); 237 | mainGroup = 5AEC539B1DEC46BC00542FE2; 238 | productRefGroup = 5AEC53A51DEC46BC00542FE2 /* Products */; 239 | projectDirPath = ""; 240 | projectRoot = ""; 241 | targets = ( 242 | 5AEC53A31DEC46BC00542FE2 /* Sort */, 243 | 5AEC53BC1DEC46BD00542FE2 /* SortTests */, 244 | 5AEC53C71DEC46BD00542FE2 /* SortUITests */, 245 | ); 246 | }; 247 | /* End PBXProject section */ 248 | 249 | /* Begin PBXResourcesBuildPhase section */ 250 | 5AEC53A21DEC46BC00542FE2 /* Resources */ = { 251 | isa = PBXResourcesBuildPhase; 252 | buildActionMask = 2147483647; 253 | files = ( 254 | 5AEC53B71DEC46BC00542FE2 /* LaunchScreen.storyboard in Resources */, 255 | 5AEC53B41DEC46BC00542FE2 /* Assets.xcassets in Resources */, 256 | 5AEC53B21DEC46BC00542FE2 /* Main.storyboard in Resources */, 257 | ); 258 | runOnlyForDeploymentPostprocessing = 0; 259 | }; 260 | 5AEC53BB1DEC46BD00542FE2 /* Resources */ = { 261 | isa = PBXResourcesBuildPhase; 262 | buildActionMask = 2147483647; 263 | files = ( 264 | ); 265 | runOnlyForDeploymentPostprocessing = 0; 266 | }; 267 | 5AEC53C61DEC46BD00542FE2 /* Resources */ = { 268 | isa = PBXResourcesBuildPhase; 269 | buildActionMask = 2147483647; 270 | files = ( 271 | ); 272 | runOnlyForDeploymentPostprocessing = 0; 273 | }; 274 | /* End PBXResourcesBuildPhase section */ 275 | 276 | /* Begin PBXSourcesBuildPhase section */ 277 | 5AEC53A01DEC46BC00542FE2 /* Sources */ = { 278 | isa = PBXSourcesBuildPhase; 279 | buildActionMask = 2147483647; 280 | files = ( 281 | 5AEC53DC1DEC5DE300542FE2 /* NSMutableArray+ZSSort.m in Sources */, 282 | 5AEC53AF1DEC46BC00542FE2 /* ViewController.m in Sources */, 283 | 5AEC53AC1DEC46BC00542FE2 /* AppDelegate.m in Sources */, 284 | 5AEC53A91DEC46BC00542FE2 /* main.m in Sources */, 285 | ); 286 | runOnlyForDeploymentPostprocessing = 0; 287 | }; 288 | 5AEC53B91DEC46BD00542FE2 /* Sources */ = { 289 | isa = PBXSourcesBuildPhase; 290 | buildActionMask = 2147483647; 291 | files = ( 292 | 5AEC53C21DEC46BD00542FE2 /* SortTests.m in Sources */, 293 | ); 294 | runOnlyForDeploymentPostprocessing = 0; 295 | }; 296 | 5AEC53C41DEC46BD00542FE2 /* Sources */ = { 297 | isa = PBXSourcesBuildPhase; 298 | buildActionMask = 2147483647; 299 | files = ( 300 | 5AEC53CD1DEC46BD00542FE2 /* SortUITests.m in Sources */, 301 | ); 302 | runOnlyForDeploymentPostprocessing = 0; 303 | }; 304 | /* End PBXSourcesBuildPhase section */ 305 | 306 | /* Begin PBXTargetDependency section */ 307 | 5AEC53BF1DEC46BD00542FE2 /* PBXTargetDependency */ = { 308 | isa = PBXTargetDependency; 309 | target = 5AEC53A31DEC46BC00542FE2 /* Sort */; 310 | targetProxy = 5AEC53BE1DEC46BD00542FE2 /* PBXContainerItemProxy */; 311 | }; 312 | 5AEC53CA1DEC46BD00542FE2 /* PBXTargetDependency */ = { 313 | isa = PBXTargetDependency; 314 | target = 5AEC53A31DEC46BC00542FE2 /* Sort */; 315 | targetProxy = 5AEC53C91DEC46BD00542FE2 /* PBXContainerItemProxy */; 316 | }; 317 | /* End PBXTargetDependency section */ 318 | 319 | /* Begin PBXVariantGroup section */ 320 | 5AEC53B01DEC46BC00542FE2 /* Main.storyboard */ = { 321 | isa = PBXVariantGroup; 322 | children = ( 323 | 5AEC53B11DEC46BC00542FE2 /* Base */, 324 | ); 325 | name = Main.storyboard; 326 | sourceTree = ""; 327 | }; 328 | 5AEC53B51DEC46BC00542FE2 /* LaunchScreen.storyboard */ = { 329 | isa = PBXVariantGroup; 330 | children = ( 331 | 5AEC53B61DEC46BC00542FE2 /* Base */, 332 | ); 333 | name = LaunchScreen.storyboard; 334 | sourceTree = ""; 335 | }; 336 | /* End PBXVariantGroup section */ 337 | 338 | /* Begin XCBuildConfiguration section */ 339 | 5AEC53CF1DEC46BD00542FE2 /* Debug */ = { 340 | isa = XCBuildConfiguration; 341 | buildSettings = { 342 | ALWAYS_SEARCH_USER_PATHS = NO; 343 | CLANG_ANALYZER_NONNULL = YES; 344 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 345 | CLANG_CXX_LIBRARY = "libc++"; 346 | CLANG_ENABLE_MODULES = YES; 347 | CLANG_ENABLE_OBJC_ARC = YES; 348 | CLANG_WARN_BOOL_CONVERSION = YES; 349 | CLANG_WARN_CONSTANT_CONVERSION = YES; 350 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 351 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 352 | CLANG_WARN_EMPTY_BODY = YES; 353 | CLANG_WARN_ENUM_CONVERSION = YES; 354 | CLANG_WARN_INFINITE_RECURSION = YES; 355 | CLANG_WARN_INT_CONVERSION = YES; 356 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 357 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 358 | CLANG_WARN_UNREACHABLE_CODE = YES; 359 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 360 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 361 | COPY_PHASE_STRIP = NO; 362 | DEBUG_INFORMATION_FORMAT = dwarf; 363 | ENABLE_STRICT_OBJC_MSGSEND = YES; 364 | ENABLE_TESTABILITY = YES; 365 | GCC_C_LANGUAGE_STANDARD = gnu99; 366 | GCC_DYNAMIC_NO_PIC = NO; 367 | GCC_NO_COMMON_BLOCKS = YES; 368 | GCC_OPTIMIZATION_LEVEL = 0; 369 | GCC_PREPROCESSOR_DEFINITIONS = ( 370 | "DEBUG=1", 371 | "$(inherited)", 372 | ); 373 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 374 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 375 | GCC_WARN_UNDECLARED_SELECTOR = YES; 376 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 377 | GCC_WARN_UNUSED_FUNCTION = YES; 378 | GCC_WARN_UNUSED_VARIABLE = YES; 379 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 380 | MTL_ENABLE_DEBUG_INFO = YES; 381 | ONLY_ACTIVE_ARCH = YES; 382 | SDKROOT = iphoneos; 383 | TARGETED_DEVICE_FAMILY = "1,2"; 384 | }; 385 | name = Debug; 386 | }; 387 | 5AEC53D01DEC46BD00542FE2 /* Release */ = { 388 | isa = XCBuildConfiguration; 389 | buildSettings = { 390 | ALWAYS_SEARCH_USER_PATHS = NO; 391 | CLANG_ANALYZER_NONNULL = YES; 392 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 393 | CLANG_CXX_LIBRARY = "libc++"; 394 | CLANG_ENABLE_MODULES = YES; 395 | CLANG_ENABLE_OBJC_ARC = YES; 396 | CLANG_WARN_BOOL_CONVERSION = YES; 397 | CLANG_WARN_CONSTANT_CONVERSION = YES; 398 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 399 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 400 | CLANG_WARN_EMPTY_BODY = YES; 401 | CLANG_WARN_ENUM_CONVERSION = YES; 402 | CLANG_WARN_INFINITE_RECURSION = YES; 403 | CLANG_WARN_INT_CONVERSION = YES; 404 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 405 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 406 | CLANG_WARN_UNREACHABLE_CODE = YES; 407 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 408 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 409 | COPY_PHASE_STRIP = NO; 410 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 411 | ENABLE_NS_ASSERTIONS = NO; 412 | ENABLE_STRICT_OBJC_MSGSEND = YES; 413 | GCC_C_LANGUAGE_STANDARD = gnu99; 414 | GCC_NO_COMMON_BLOCKS = YES; 415 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 416 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 417 | GCC_WARN_UNDECLARED_SELECTOR = YES; 418 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 419 | GCC_WARN_UNUSED_FUNCTION = YES; 420 | GCC_WARN_UNUSED_VARIABLE = YES; 421 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 422 | MTL_ENABLE_DEBUG_INFO = NO; 423 | SDKROOT = iphoneos; 424 | TARGETED_DEVICE_FAMILY = "1,2"; 425 | VALIDATE_PRODUCT = YES; 426 | }; 427 | name = Release; 428 | }; 429 | 5AEC53D21DEC46BD00542FE2 /* Debug */ = { 430 | isa = XCBuildConfiguration; 431 | buildSettings = { 432 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 433 | INFOPLIST_FILE = Sort/Info.plist; 434 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 435 | PRODUCT_BUNDLE_IDENTIFIER = zs.Sort; 436 | PRODUCT_NAME = "$(TARGET_NAME)"; 437 | }; 438 | name = Debug; 439 | }; 440 | 5AEC53D31DEC46BD00542FE2 /* Release */ = { 441 | isa = XCBuildConfiguration; 442 | buildSettings = { 443 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 444 | INFOPLIST_FILE = Sort/Info.plist; 445 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 446 | PRODUCT_BUNDLE_IDENTIFIER = zs.Sort; 447 | PRODUCT_NAME = "$(TARGET_NAME)"; 448 | }; 449 | name = Release; 450 | }; 451 | 5AEC53D51DEC46BD00542FE2 /* Debug */ = { 452 | isa = XCBuildConfiguration; 453 | buildSettings = { 454 | BUNDLE_LOADER = "$(TEST_HOST)"; 455 | INFOPLIST_FILE = SortTests/Info.plist; 456 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 457 | PRODUCT_BUNDLE_IDENTIFIER = zs.SortTests; 458 | PRODUCT_NAME = "$(TARGET_NAME)"; 459 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Sort.app/Sort"; 460 | }; 461 | name = Debug; 462 | }; 463 | 5AEC53D61DEC46BD00542FE2 /* Release */ = { 464 | isa = XCBuildConfiguration; 465 | buildSettings = { 466 | BUNDLE_LOADER = "$(TEST_HOST)"; 467 | INFOPLIST_FILE = SortTests/Info.plist; 468 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 469 | PRODUCT_BUNDLE_IDENTIFIER = zs.SortTests; 470 | PRODUCT_NAME = "$(TARGET_NAME)"; 471 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Sort.app/Sort"; 472 | }; 473 | name = Release; 474 | }; 475 | 5AEC53D81DEC46BD00542FE2 /* Debug */ = { 476 | isa = XCBuildConfiguration; 477 | buildSettings = { 478 | INFOPLIST_FILE = SortUITests/Info.plist; 479 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 480 | PRODUCT_BUNDLE_IDENTIFIER = zs.SortUITests; 481 | PRODUCT_NAME = "$(TARGET_NAME)"; 482 | TEST_TARGET_NAME = Sort; 483 | }; 484 | name = Debug; 485 | }; 486 | 5AEC53D91DEC46BD00542FE2 /* Release */ = { 487 | isa = XCBuildConfiguration; 488 | buildSettings = { 489 | INFOPLIST_FILE = SortUITests/Info.plist; 490 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 491 | PRODUCT_BUNDLE_IDENTIFIER = zs.SortUITests; 492 | PRODUCT_NAME = "$(TARGET_NAME)"; 493 | TEST_TARGET_NAME = Sort; 494 | }; 495 | name = Release; 496 | }; 497 | /* End XCBuildConfiguration section */ 498 | 499 | /* Begin XCConfigurationList section */ 500 | 5AEC539F1DEC46BC00542FE2 /* Build configuration list for PBXProject "Sort" */ = { 501 | isa = XCConfigurationList; 502 | buildConfigurations = ( 503 | 5AEC53CF1DEC46BD00542FE2 /* Debug */, 504 | 5AEC53D01DEC46BD00542FE2 /* Release */, 505 | ); 506 | defaultConfigurationIsVisible = 0; 507 | defaultConfigurationName = Release; 508 | }; 509 | 5AEC53D11DEC46BD00542FE2 /* Build configuration list for PBXNativeTarget "Sort" */ = { 510 | isa = XCConfigurationList; 511 | buildConfigurations = ( 512 | 5AEC53D21DEC46BD00542FE2 /* Debug */, 513 | 5AEC53D31DEC46BD00542FE2 /* Release */, 514 | ); 515 | defaultConfigurationIsVisible = 0; 516 | }; 517 | 5AEC53D41DEC46BD00542FE2 /* Build configuration list for PBXNativeTarget "SortTests" */ = { 518 | isa = XCConfigurationList; 519 | buildConfigurations = ( 520 | 5AEC53D51DEC46BD00542FE2 /* Debug */, 521 | 5AEC53D61DEC46BD00542FE2 /* Release */, 522 | ); 523 | defaultConfigurationIsVisible = 0; 524 | }; 525 | 5AEC53D71DEC46BD00542FE2 /* Build configuration list for PBXNativeTarget "SortUITests" */ = { 526 | isa = XCConfigurationList; 527 | buildConfigurations = ( 528 | 5AEC53D81DEC46BD00542FE2 /* Debug */, 529 | 5AEC53D91DEC46BD00542FE2 /* Release */, 530 | ); 531 | defaultConfigurationIsVisible = 0; 532 | }; 533 | /* End XCConfigurationList section */ 534 | }; 535 | rootObject = 5AEC539C1DEC46BC00542FE2 /* Project object */; 536 | } 537 | -------------------------------------------------------------------------------- /Sort.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Sort.xcodeproj/project.xcworkspace/xcuserdata/zhangshu.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/happyte/sort/418975f5915eac6999df2eb9f996fde5f7c3dd6a/Sort.xcodeproj/project.xcworkspace/xcuserdata/zhangshu.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Sort.xcodeproj/xcuserdata/zhangshu.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Sort.xcodeproj/xcuserdata/zhangshu.xcuserdatad/xcschemes/Sort.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Sort.xcodeproj/xcuserdata/zhangshu.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Sort.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 5AEC53A31DEC46BC00542FE2 16 | 17 | primary 18 | 19 | 20 | 5AEC53BC1DEC46BD00542FE2 21 | 22 | primary 23 | 24 | 25 | 5AEC53C71DEC46BD00542FE2 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Sort/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /Sort/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /Sort/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Sort/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Sort/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Sort/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Sort/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /Sort/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "NSMutableArray+ZSSort.h" 11 | 12 | static int num = 50; 13 | 14 | @interface ViewController () 15 | 16 | @property(nonatomic,strong)UISegmentedControl *segment; 17 | @property(nonatomic,strong)NSMutableArray *barViewArray; 18 | @property(nonatomic,strong)NSTimer *timer; 19 | @property(nonatomic,strong)UILabel *timeLabel; 20 | @property (nonatomic, strong) dispatch_semaphore_t sema; 21 | @end 22 | 23 | @implementation ViewController 24 | 25 | - (UILabel *)timeLabel { 26 | if (_timeLabel == nil) { 27 | _timeLabel = [[UILabel alloc]init]; 28 | _timeLabel.font = [UIFont systemFontOfSize:15]; 29 | [self.view addSubview:_timeLabel]; 30 | } 31 | return _timeLabel; 32 | } 33 | 34 | - (NSMutableArray *)barViewArray{ 35 | if (_barViewArray == nil) { 36 | _barViewArray = [NSMutableArray arrayWithCapacity:num]; 37 | for (int i = 0; i < num; i++) { 38 | UIView *barView = [[UIView alloc]init]; 39 | barView.backgroundColor = [UIColor blueColor]; 40 | [_barViewArray addObject:barView]; 41 | [self.view addSubview:barView]; 42 | } 43 | } 44 | return _barViewArray; 45 | } 46 | 47 | - (UISegmentedControl *)segment{ 48 | if (_segment == nil) { 49 | _segment = [[UISegmentedControl alloc]initWithItems:@[@"选择",@"冒泡",@"插入",@"快速",@"堆排"]]; 50 | [_segment addTarget:self action:@selector(segmentChanged) forControlEvents:UIControlEventValueChanged]; 51 | [self.view addSubview:_segment]; 52 | } 53 | return _segment; 54 | } 55 | 56 | - (void)viewDidLoad { 57 | [super viewDidLoad]; 58 | self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"重置" style:UIBarButtonItemStylePlain target:self action:@selector(reset)]; 59 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"排序" style:UIBarButtonItemStylePlain target:self action:@selector(sort)]; 60 | self.timeLabel.frame = CGRectMake(CGRectGetWidth(self.view.bounds)*0.5-40, 560, 80, 30); 61 | self.segment.frame = CGRectMake(40, 64+10, CGRectGetWidth(self.view.frame)*0.8, 30); 62 | self.segment.selectedSegmentIndex = 0; 63 | [self reset]; 64 | } 65 | 66 | - (void)segmentChanged{ 67 | [self reset]; 68 | } 69 | 70 | - (void)reset{ 71 | CGFloat barMargin = 1; 72 | self.timeLabel.text = @""; 73 | CGFloat barWidth = (0.8*CGRectGetWidth(self.view.bounds) - (num-1)*barMargin)/num; 74 | [self.barViewArray enumerateObjectsUsingBlock:^(UIView *_Nonnull bar, NSUInteger idx, BOOL * _Nonnull stop) { 75 | CGFloat barHeight = 10+arc4random_uniform(CGRectGetHeight(self.view.bounds)*0.6); 76 | bar.frame = CGRectMake(40+idx*(barWidth+barMargin), 550, barWidth, -barHeight); 77 | }]; 78 | } 79 | 80 | - (void)sort{ 81 | [self timerInvalid]; 82 | NSTimeInterval currentTime = [[NSDate date]timeIntervalSince1970]; 83 | self.sema = dispatch_semaphore_create(0); //创建请求 84 | __weak typeof(self) weakSelf = self; 85 | // 定时器信号 86 | self.timer = [NSTimer scheduledTimerWithTimeInterval:0.005 repeats:YES block:^(NSTimer * _Nonnull timer) { 87 | //发出信号量 88 | dispatch_semaphore_signal(self.sema); 89 | NSTimeInterval delta = [[NSDate date]timeIntervalSince1970]-currentTime; 90 | self.timeLabel.text = [NSString stringWithFormat:@"耗时%.2f",delta]; 91 | }]; 92 | //下面这条线程不是主线程 93 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 94 | switch (self.segment.selectedSegmentIndex) { 95 | case 0: 96 | [self selectSort]; 97 | break; 98 | case 1: 99 | [self bubbleSort]; 100 | break; 101 | case 2: 102 | [self insertSort]; 103 | break; 104 | case 3: 105 | [self quickSort]; 106 | break; 107 | case 4: 108 | [self heapSort]; 109 | break; 110 | default: 111 | break; 112 | } 113 | [self timerInvalid]; 114 | }); 115 | } 116 | 117 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 118 | { 119 | [self timerInvalid]; 120 | } 121 | 122 | - (void)timerInvalid{ 123 | if (self.timer) { 124 | [self.timer invalidate]; 125 | self.timer = nil; 126 | } 127 | self.sema = nil; 128 | } 129 | 130 | - (void)selectSort{ 131 | [self.barViewArray zs_selectSort:^NSComparisonResult(id obj1, id obj2) { 132 | return [self compareWithOne:obj1 Two:obj2]; 133 | } withCallback:^(id obj1, id obj2) { 134 | [self exchangeWithOne:obj1 Two:obj2]; 135 | }]; 136 | } 137 | 138 | - (void)bubbleSort{ 139 | [self.barViewArray zs_bubbleSort:^NSComparisonResult(id obj1, id obj2) { 140 | return [self compareWithOne:obj1 Two:obj2]; 141 | } withCallback:^(id obj1, id obj2) { 142 | [self exchangeWithOne:obj1 Two:obj2]; 143 | }]; 144 | } 145 | 146 | - (void)insertSort{ 147 | [self.barViewArray zs_insertSort:^NSComparisonResult(id obj1, id obj2) { 148 | return [self compareWithOne:obj1 Two:obj2]; 149 | } withCallback:^(id obj1, id obj2) { 150 | [self exchangeWithOne:obj1 Two:obj2]; 151 | }]; 152 | } 153 | 154 | - (void)quickSort{ 155 | [self.barViewArray zs_quickSort:^NSComparisonResult(id obj1, id obj2) { 156 | return [self compareWithOne:obj1 Two:obj2]; 157 | } withCallback:^(id obj1, id obj2) { 158 | [self exchangeWithOne:obj1 Two:obj2]; 159 | }]; 160 | } 161 | 162 | - (void)heapSort{ 163 | [self.barViewArray zs_heapSort:^NSComparisonResult(id obj1, id obj2) { 164 | return [self compareWithOne:obj1 Two:obj2]; 165 | } withCallback:^(id obj1, id obj2) { 166 | [self exchangeWithOne:obj1 Two:obj2]; 167 | }]; 168 | } 169 | 170 | - (NSComparisonResult)compareWithOne:(UIView *)one Two:(UIView *)two{ 171 | dispatch_semaphore_wait(self.sema, DISPATCH_TIME_FOREVER); 172 | CGFloat oneHeight = one.frame.size.height; 173 | CGFloat twoHeight = two.frame.size.height; 174 | if (oneHeight == twoHeight) { 175 | return NSOrderedSame; 176 | } 177 | return oneHeight < twoHeight? NSOrderedAscending:NSOrderedDescending; 178 | } 179 | 180 | - (void)exchangeWithOne:(UIView *)one Two:(UIView *)two{ 181 | dispatch_async(dispatch_get_main_queue(), ^{ 182 | CGRect oneFrame = one.frame; 183 | CGRect twoFrame = two.frame; 184 | oneFrame.origin.x = two.frame.origin.x; 185 | twoFrame.origin.x = one.frame.origin.x; 186 | one.frame = oneFrame; 187 | two.frame = twoFrame; 188 | }); 189 | } 190 | 191 | @end 192 | -------------------------------------------------------------------------------- /Sort/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Sort 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /SortTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /SortTests/SortTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // SortTests.m 3 | // SortTests 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SortTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation SortTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | // Use XCTAssert and related functions to verify your tests produce the correct results. 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /SortUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /SortUITests/SortUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // SortUITests.m 3 | // SortUITests 4 | // 5 | // Created by 张树 on 16/11/28. 6 | // Copyright © 2016年 com.zs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SortUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation SortUITests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | 22 | // In UI tests it is usually best to stop immediately when a failure occurs. 23 | self.continueAfterFailure = NO; 24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 25 | [[[XCUIApplication alloc] init] launch]; 26 | 27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 28 | } 29 | 30 | - (void)tearDown { 31 | // Put teardown code here. This method is called after the invocation of each test method in the class. 32 | [super tearDown]; 33 | } 34 | 35 | - (void)testExample { 36 | // Use recording to get started writing UI tests. 37 | // Use XCTAssert and related functions to verify your tests produce the correct results. 38 | } 39 | 40 | @end 41 | --------------------------------------------------------------------------------