// 冒泡排序
- (NSMutableArray *)arrayOrderMethod:(NSMutableArray *)array
{
// 循环一遍数组长度
for (int n = 0; n < array.count; n++)
{
// 内部循环:数组长度-1减去当前外循环长度
for (int m = 0; m < array.count - n - 1; m++)
{
// 比较当前值和下一个的大小进行交换
if (array[m] > array [m + 1])
{
// 交换位置
[self swapWithData:array index1:m index2:m+1];
}
}
}
return array;
}
// 快速排序
-(void)quickSortWithArray:(NSMutableArray *)aData left:(NSInteger)left right:(NSInteger)right
{
if (right > left) {
// 最左端
NSInteger i = left;
// 最右端
NSInteger j = right + 1;
while (true) {
while (i+1 < [aData count] && [aData objectAtIndex:++i] < [aData objectAtIndex:left]) ;
while (j-1 > -1 && [aData objectAtIndex:--j] > [aData objectAtIndex:left]) ;
if (i >= j) {
break;
}
[self swapWithData:aData index1:i index2:j];
}
[self swapWithData:aData index1:left index2:j];
[self quickSortWithArray:aData left:left right:j-1];
[self quickSortWithArray:aData left:j+1 right:right];
}
}
// 选择排序
-(void)SelectSort:(NSMutableArray *)list{
for(int i = 0 ; i<[list count]; i++){
int k = i;
for(int j = i+1 ; j<[list count]; j++){
NSInteger jvalue = [[list objectAtIndex:j] intValue];
NSInteger kvalue = [[list objectAtIndex:k] intValue];
if(jvalue < kvalue){
k = j;
}
}
if(k != i){
[list exchangeObjectAtIndex:i withObjectAtIndex:k];
}
}
}
// 插入排序
-(void)InsertSort:(NSMutableArray *)list{
for(int i = 1 ; i < [list count] ; i++){
int j = i;
NSInteger temp= [[list objectAtIndex:i] intValue];
while (j > 0 && temp < [[list objectAtIndex:j - 1]intValue]) {
[list replaceObjectAtIndex:j withObject:[list objectAtIndex:(j-1)]];
//list[j] = list[j-1];
j--;
}
[list replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
//list[j] = temp;
}
}
// 交换方法:数组名,两个交换位置
-(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2{
NSNumber *tmp = [aData objectAtIndex:index1];
[aData replaceObjectAtIndex:index1 withObject:[aData objectAtIndex:index2]];
[aData replaceObjectAtIndex:index2 withObject:tmp];
}
- (NSMutableArray *)arrayOrderMethod:(NSMutableArray *)array
{
// 循环一遍数组长度
for (int n = 0; n < array.count; n++)
{
// 内部循环:数组长度-1减去当前外循环长度
for (int m = 0; m < array.count - n - 1; m++)
{
// 比较当前值和下一个的大小进行交换
if (array[m] > array [m + 1])
{
// 交换位置
[self swapWithData:array index1:m index2:m+1];
}
}
}
return array;
}
// 快速排序
-(void)quickSortWithArray:(NSMutableArray *)aData left:(NSInteger)left right:(NSInteger)right
{
if (right > left) {
// 最左端
NSInteger i = left;
// 最右端
NSInteger j = right + 1;
while (true) {
while (i+1 < [aData count] && [aData objectAtIndex:++i] < [aData objectAtIndex:left]) ;
while (j-1 > -1 && [aData objectAtIndex:--j] > [aData objectAtIndex:left]) ;
if (i >= j) {
break;
}
[self swapWithData:aData index1:i index2:j];
}
[self swapWithData:aData index1:left index2:j];
[self quickSortWithArray:aData left:left right:j-1];
[self quickSortWithArray:aData left:j+1 right:right];
}
}
// 选择排序
-(void)SelectSort:(NSMutableArray *)list{
for(int i = 0 ; i<[list count]; i++){
int k = i;
for(int j = i+1 ; j<[list count]; j++){
NSInteger jvalue = [[list objectAtIndex:j] intValue];
NSInteger kvalue = [[list objectAtIndex:k] intValue];
if(jvalue < kvalue){
k = j;
}
}
if(k != i){
[list exchangeObjectAtIndex:i withObjectAtIndex:k];
}
}
}
// 插入排序
-(void)InsertSort:(NSMutableArray *)list{
for(int i = 1 ; i < [list count] ; i++){
int j = i;
NSInteger temp= [[list objectAtIndex:i] intValue];
while (j > 0 && temp < [[list objectAtIndex:j - 1]intValue]) {
[list replaceObjectAtIndex:j withObject:[list objectAtIndex:(j-1)]];
//list[j] = list[j-1];
j--;
}
[list replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
//list[j] = temp;
}
}
// 交换方法:数组名,两个交换位置
-(void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2{
NSNumber *tmp = [aData objectAtIndex:index1];
[aData replaceObjectAtIndex:index1 withObject:[aData objectAtIndex:index2]];
[aData replaceObjectAtIndex:index2 withObject:tmp];
}