冒泡排序是一种最基本的交换排序,把相邻的元素两两比较,根据大小来交换元素的位置.
原始的冒泡排序是稳定排序,需要遍历所有元素,时间复杂度是O(N^2)
我用OC来写这个算法,分别写了三个,后两个是在基础上进行优化的,以最后一个优化的为准.
/**
这是冒泡排序第一版
@param list NSArray *date = @[@"5",@"8",@"6",@"3",@"9",@"2",@"1",@"7"]
@return 返回排序的list
*/
- (NSArray *)sort:(NSArray *)list
{
NSMutableArray *array = [NSMutableArray arrayWithArray:list];
NSInteger count = array.count;
int tmp = 0;
for(int i = 0; i < count; i++){
for (int j = 0; j< count - i -1; j++) {
int a =[array[j] intValue];
int b = [array[j+1] intValue];
if (a > b) {
tmp =(int)array[j];
array[j] = [NSString stringWithFormat:@"%d",b];
array[j+1] = [NSString stringWithFormat:@"%d",a];;
}
}
}
return [NSArray arrayWithArray:array];
}
/**
这是冒泡排序第二版
判断当前数组是否已经有序,并作出标记,如果已经有序,剩下的不必执行
@param list NSArray *date = @[@"5",@"8",@"6",@"3",@"9",@"2",@"1",@"7"]
@return <#return value description#>
*/
- (NSArray *)sort1:(NSArray *)list
{
NSMutableArray *array = [NSMutableArray arrayWithArray:list];
NSInteger count = array.count;
int tmp = 0;
for(int i = 0; i < count; i++){
//有序标记,每一轮初始化YES
BOOL isSorted = YES;
for (int j = 0; j< count - i -1; j++) {
int a =[array[j] intValue];
int b = [array[j+1] intValue];
if (a > b) {
tmp =(int)array[j];
array[j] = [NSString stringWithFormat:@"%d",b];
array[j+1] = [NSString stringWithFormat:@"%d",a];;
isSorted = NO;//有交换的话,置为NO
}
}
if (isSorted) {
break;
}
}
return [NSArray arrayWithArray:array];
}
/**
这是冒泡排序第三版
记录边界,判断如例子一样,设置边界,sortBorder之后元素完全不需要比较了
@param list NSArray *date = @[@"3",@"4",@"2",@"1",@"5",@"6",@"7",@"8"]
@return <#return value description#>
*/
- (NSArray *)sort2:(NSArray *)list
{
NSMutableArray *array = [NSMutableArray arrayWithArray:list];
NSInteger count = array.count;
int tmp = 0;
//记录最后一次交换的位置
int lastExchangeIndex = 0;
//无序数组的边界,每次比较只需要比到这里为止
int sortBorder = (int)count;
for(int i = 0; i < count; i++){
//有序标记,每一轮初始化YES
BOOL isSorted = YES;
for (int j = 0; j< sortBorder; j++) {
int a =[array[j] intValue];
int b = [array[j+1] intValue];
if (a > b) {
tmp =(int)array[j];
array[j] = [NSString stringWithFormat:@"%d",b];
array[j+1] = [NSString stringWithFormat:@"%d",a];;
isSorted = NO;//有交换的话,置为NO
lastExchangeIndex = j;
}
}
sortBorder = lastExchangeIndex;
if (isSorted) {
break;
}
}
return [NSArray arrayWithArray:array];
}
当然还有鸡尾酒排序,我后续会补充上去,也是对于冒泡排序的优化.
从这里我们可以看到一个简单的冒泡排序,都有这么多可以优化的地方,需要我们多多深入去学习深挖.
鸡尾酒排序代码:
private static void sort(int array[])
{
int tmp = 0;
for (int i =0; i<array.length/2;i++)
{
boolean isSorted = true;
for (int j = i; j < array.length-i-1;j++)
{
System.out.print(i);
System.out.print(j);
if (array[j] > array[j+1])
{
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
isSorted = false;
}
}
if (isSorted){
break;
}
isSorted = true;
for (int j = array.length-i-1;j>i;j--)
{
if (array[j] < array[j-1])
{
tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
isSorted = false;
}
}
if (isSorted){
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = new int[]{2,3,4,5,6,7,1,8,9};
sort(array);
System.out.print(Arrays.toString(array));
}