|
| 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 |
0 commit comments