美文网首页程昆仑的成神之路
两种语言的冒泡排序

两种语言的冒泡排序

作者: 少年_如他 | 来源:发表于2016-04-28 16:09 被阅读37次

    //=============C语言的冒泡排序

    inta[10] = {0,8,2,3,4,6,5,7,1,9};

    //arc4random() % (b -a + 1) +a随机数

    intcount =10;

    for(inti =0; i < count -1; i++) {

    for(intj =0; j < count -1- i; j++) {

    if(a[j] > a[j +1] ) {

    inttemp ;

    temp = a[j];

    a[j] = a[j+1];

    a[j+1] = temp;

    }

    }

    }

    for(inti =0; i < count ; i++) {

    printf("%d ",a[i]);

    }

    //=============OC的冒泡排序(可变数组的exchangeObjectAtIndex:方法)

    NSMutableArray*a =[NSMutableArray arrayWithObjects:@"0",@"11",@"2",@"13",@"4",@"6",@"5",@"7",@"8",@"9",nil];

    for(inti =0; i < [a count] -1; i++) {

    for(intj =0; j < [a count] -1- i; j++) {

    if([[a objectAtIndex:j] intValue]  > [[a objectAtIndex:(j+1)] intValue] )//字符串转换int比较大小

    {

    [a exchangeObjectAtIndex:j withObjectAtIndex:(j+1)];

    }

    }

    }

    NSLog(@"%@",a);

    for(idobjina) {

    NSLog(@"%@",obj);

    }

    [a sortUsingSelector:@selector(compare:)];//compare比较不是按照字符的数值大小而是比较的字母的顺序

    NSLog(@"%@",a);

    相关文章

      网友评论

        本文标题:两种语言的冒泡排序

        本文链接:https://www.haomeiwen.com/subject/quablttx.html