用OC写一个算法

    

    NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:@"16",@"2",@"20",@"5",@"3",@"9",@"7",nil ];

    

    

    id temp;

 

    for (int i=0; i<[arr count]-1; i++) {

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

            if ([[arr objectAtIndex:j] intValue]>[[arr objectAtIndex:j+1] intValue]) {


                temp=[arr objectAtIndex:j];

                [arr replaceObjectAtIndex:j withObject:[arr objectAtIndex:j+1]];

                [arr replaceObjectAtIndex:j+1 withObject:temp];

                

            }

        }

    }

    


NSLog(@"%@",arr);


你可能感兴趣的:(iphone)