總結(jié)了下在iOS開發(fā)常用到的算法,不管是在面試中還是日常開發(fā)中厚脉,都會(huì)用到
1央拖、冒泡排序
冒泡算法是重復(fù)地走訪過要排序的數(shù)列,一次比較兩個(gè)元素垮卓,如果他們的順序錯(cuò)誤就把他們交換過來垫桂。走訪數(shù)列的工作是重復(fù)地進(jìn)行直到?jīng)]有再需要交換,也就是說該數(shù)列已經(jīng)排序完成粟按。
對(duì)以下一組數(shù)據(jù)進(jìn)行降序排序(冒泡排序)“55, 23, 93, 23, 4, 56, 1, 34, 11, 69”
int array[10] = {55, 23, 93, 23, 4, 56, 1, 34, 11, 69};
int num = sizeof(array)/sizeof(int);
for (int i = 0 ; i < num - 1; i++) {
for (int j = 0 ; j < num - 1 - i; j++) {
if (array[j] < array[j +1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int i = 0; i < num; i++) {
printf("%d", array[i]);
if (i == num - 1) {
printf("\n");
} else {
printf(" ");
}
}
2诬滩、選擇排序
選擇排序的工作原理是每一次從待排序的數(shù)據(jù)元素中選出最小(或最大)的一個(gè)元素灭将,存放在序列的起始位置疼鸟,直到全部待排序的數(shù)據(jù)元素排完。
對(duì)以下一組數(shù)據(jù)進(jìn)行升序排序(選擇排序)庙曙】站担“55, 23, 93, 23, 4, 56, 1, 34, 11, 69”
int array[10] = {55, 23, 93, 23, 4, 56, 1, 34, 11, 69};
int num = sizeof(array)/sizeof(int);
for (int i = 0; i < num - 1; i++) {
for (int j = i + 1; j < num ; j++) {
if (array[i] > array [j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int i = 0; i < num; i++) {
printf("%d", array[i]);
if (i == num - 1) {
printf("\n");
} else {
printf(" ");
}
}
3、快速排序算法
該方法的基本思想是:
1.先從數(shù)列中取出一個(gè)數(shù)作為基準(zhǔn)數(shù)。
2.分區(qū)過程吴攒,將比這個(gè)數(shù)大的數(shù)全放到它的右邊张抄,小于或等于它的數(shù)全放到它的左邊。
3.再對(duì)左右區(qū)間重復(fù)第二步舶斧,直到各區(qū)間只有一個(gè)數(shù)欣鳖。
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@(55), @(23),@(93),@(23),@(4),@(56),@(1),@(34),@(69),nil];
[self quickSortArray:arr withLeftIndex:0 andRightIndex:arr.count - 1];
NSLog(@"%@",arr);
}
- (void)quickSortArray:(NSMutableArray *)array withLeftIndex:(NSInteger)leftIndex andRightIndex:(NSInteger)rightIndex
{
if (leftIndex >= rightIndex) {//如果數(shù)組長度為0或1時(shí)返回
return ;
}
NSInteger i = leftIndex;
NSInteger j = rightIndex;
//記錄比較基準(zhǔn)數(shù)
NSInteger key = [array[i] integerValue];
while (i < j) {
/**** 首先從右邊j開始查找比基準(zhǔn)數(shù)小的值 ***/
while (i < j && [array[j] integerValue] >= key) {//如果比基準(zhǔn)數(shù)大,繼續(xù)查找
j--;
}
//如果比基準(zhǔn)數(shù)小茴厉,則將查找到的小值調(diào)換到i的位置
array[i] = array[j];
/**** 當(dāng)在右邊查找到一個(gè)比基準(zhǔn)數(shù)小的值時(shí)泽台,就從i開始往后找比基準(zhǔn)數(shù)大的值 ***/
while (i < j && [array[i] integerValue] <= key) {//如果比基準(zhǔn)數(shù)小,繼續(xù)查找
i++;
}
//如果比基準(zhǔn)數(shù)大矾缓,則將查找到的大值調(diào)換到j(luò)的位置
array[j] = array[i];
}
//將基準(zhǔn)數(shù)放到正確位置
array[i] = @(key);
/**** 遞歸排序 ***/
//排序基準(zhǔn)數(shù)左邊的
[self quickSortArray:array withLeftIndex:leftIndex andRightIndex:i - 1];
//排序基準(zhǔn)數(shù)右邊的
[self quickSortArray:array withLeftIndex:i + 1 andRightIndex:rightIndex];
}
4怀酷、 歸并排序
該方法的基本思想是:
1.分解:將待排序的問題分解成大小大致相等的兩部分。
2.求解子問題:用歸并排序的方法對(duì)兩個(gè)子問題進(jìn)行遞歸排序嗜闻。
3.合并(merge):將排好序的有序子序列進(jìn)行合并蜕依,得到符合要求的子序列。
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *tempArr;
@end
@implementation ViewController
-(NSMutableArray *)tempArr
{
if (_tempArr == nil) {
_tempArr = [NSMutableArray array];
}
return _tempArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@(55), @(29),@(93),@(23),@(4),@(56),@(1),@(34),@(69),nil];
[self mergeSortArray:arr lowIndex:0 highIndex:arr.count - 1];
NSLog(@"%@",arr);
}
- (void)mergeSortArray:(NSMutableArray *)array lowIndex:(NSInteger)lowIndex highIndex:(NSInteger)highIndex
{
if (lowIndex >= highIndex) {
return;
}
NSInteger midIndex = lowIndex + (highIndex - lowIndex) / 2;
[self mergeSortArray:array lowIndex:lowIndex highIndex:midIndex];
[self mergeSortArray:array lowIndex:midIndex + 1 highIndex:highIndex];
[self mergeArray:array lowIndex:lowIndex midIndex:midIndex highIndex:highIndex];
}
- (void)mergeArray:(NSMutableArray *)array lowIndex:(NSInteger)lowIndex midIndex:(NSInteger)midIndex highIndex:(NSInteger)highIndex
{
for (NSInteger i = lowIndex; i <= highIndex; i ++) {
self.tempArr[i] = array[i];
}
NSInteger k = lowIndex;
NSInteger l = midIndex + 1;
for (NSInteger j = lowIndex; j <= highIndex; j ++) {
if (l > highIndex) {
array[j] = self.tempArr[k];
k++;
}else if (k > midIndex)
{
array[j] = self.tempArr[l];
l++;
}else if ([self.tempArr[k] integerValue] > [self.tempArr[l] integerValue])
{
array[j] = self.tempArr[l];
l++;
}else
{
array[j] = self.tempArr[k];
k++;
}
}
}