基于有太多的面試問(wèn)題都會(huì)讓直接手寫(xiě)或者敲代碼,做出如下的幾個(gè)排序方式(有些公司招聘信息竟然公然寫(xiě)著:不會(huì)快排的勿投U习)凌外。這就有點(diǎn)好奇心了。所以涛浙,寫(xiě)幾個(gè)方法康辑。當(dāng)然,網(wǎng)上有很多例子轿亮。
OK疮薇,接下來(lái)我們進(jìn)行逐一講解。
1我注、冒泡排序
這個(gè)是我們都熟悉的排序方法按咒,幾乎每一個(gè)人都會(huì)很輕易就能夠?qū)懗鰜?lái)。下面上代碼:
#pragma mark - 冒泡排序
- (void)bubblingSort {
NSMutableArray *arr_Mut = [NSMutableArray arrayWithObjects:@1,@4,@12,@14,@22,@23,@5,nil];
static NSInteger ci = 0;
static NSInteger di = 0;
for (int i = 0; i < arr_Mut.count; i ++) {
/**
* 這里有兩種寫(xiě)法
* 1但骨、以下:for (int j = 0; j < arr_M.count - 1 - i; j ++)
* 2励七、for (int j = 0; j < arr_M.count - 1; j ++)
* 我們可以看出來(lái),這兩種不同的寫(xiě)法結(jié)果是一樣的奔缠,只是方法二循環(huán)的次數(shù)要多很多掠抬,可以打印ci查看
*/
for (int j = 0; j < arr_Mut.count - 1 - i; j ++) {
if (arr_Mut[j] > arr_Mut[j + 1]) {
[arr_Mut exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
NSLog(@"次數(shù):%ld", ci);
ci++;
}
//這個(gè)毫無(wú)疑問(wèn)就是數(shù)組的個(gè)數(shù)
NSLog(@"i次數(shù):%ld", di);
di ++;
}
//最終的數(shù)組
NSLog(@"%@", arr_Mut);
}
這個(gè)東西沒(méi)有什么要多說(shuō)的,其中的注釋就是最重要的區(qū)別了(本人比較懶添坊,不想再多寫(xiě)代碼)剿另。
2箫锤、選擇排序
選擇排序就比較簡(jiǎn)單了贬蛙,循環(huán)的過(guò)程不比冒泡少,不過(guò)就是實(shí)現(xiàn)的方法不同而已谚攒。注意循環(huán)的條件和循環(huán)內(nèi)判斷的條件與冒泡的區(qū)別阳准。
代碼如下:
#pragma mark - 選擇排序
- (void)changeSort {
NSMutableArray *arr_Mut = [NSMutableArray arrayWithObjects:@11,@4,@12,@14,@22,@23,@5,nil];
static NSInteger ci = 0;
static NSInteger di = 0;
for (int i = 0; i < arr_Mut.count; i ++) {
//注意條件
for (int j = i + 1; j < arr_Mut.count; j ++) {
//注意條件
if (arr_Mut[i] > arr_Mut[j]) {
[arr_Mut exchangeObjectAtIndex:i withObjectAtIndex:j];
NSLog(@"\n數(shù)組\n%@\n arr_Mut[i]:%@\n arr_Mut[j]:%@\n次數(shù):%ld\n", arr_Mut, arr_Mut[i], arr_Mut[j], ci);
ci++;
}
NSLog(@"這是次數(shù):%ld", di);
di ++;
}
}
NSLog(@"%@", arr_Mut);
}
3、快速排序
這個(gè)不做太多的解釋馏臭,是在網(wǎng)上看到的野蝇,原理都是相同的,相通的括儒。這個(gè)方法使用了遞歸绕沈,IT都會(huì)明白的。
代碼:
#pragma mark - 快速排列
- (void)quickData {
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"13", @"24", @"5", @"12", @"44", @"21", @"29", nil];
// NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@(6), @(1),@(2),@(5),@(9),@(4),@(3),@(7),nil];
[self quickSortArray:arr withLIndex:0 andRIndex:arr.count - 1];
NSLog(@"%@",arr);
}
- (void)quickSortArray:(NSMutableArray *)array withLIndex:(NSInteger)lIndex andRIndex:(NSInteger)rIndex {
//數(shù)組為空帮寻,直接返回
if (lIndex >= rIndex) {
return;
}
NSInteger i = lIndex;
NSInteger j = rIndex;
//基準(zhǔn)值:就是找一個(gè)值作為標(biāo)準(zhǔn)
NSInteger value = [array[i] integerValue];
while (i < j) {
//從右邊開(kāi)始查找比基準(zhǔn)值小的值乍狐。如果比基準(zhǔn)數(shù)大,繼續(xù)查找
while (i < j && [array[j] integerValue] >= value) {
j--;
NSLog(@"\ni: %ld, \nj:%ld, \nvalue:%ld, \narray[j]:%ld", i, j, value, [array[j] integerValue]);
}
//如果比基準(zhǔn)值小固逗,就調(diào)換位置
array[i] = array[j];
//當(dāng)在右邊查找到一個(gè)比基準(zhǔn)數(shù)小的值時(shí)浅蚪,就從i開(kāi)始往后找比基準(zhǔn)數(shù)大的值,如果比基準(zhǔn)數(shù)小藕帜,繼續(xù)查找
while (i < j && [array[i] integerValue] <= value) {
i ++;
NSLog(@"i: %ld, \nj:%ld, \nvalue:%ld, \narray[j]:%ld", i, j, value, [array[i] integerValue]);
}
//如果比基準(zhǔn)數(shù)大,則將查找到的大值調(diào)換到j(luò)的位置
array[j] = array[i];
}
array[i] = @(value);
[self quickSortArray:array withLIndex:lIndex andRIndex:i - 1];
[self quickSortArray:array withLIndex:i + 1 andRIndex:rIndex];
}
以上就是排序了惜傲。
鏈接:冒泡排序洽故、選擇排序、快速排序