外层循环需要循环和len一样的次数
//定义一个函数,该函数返回NSString
void bubbleSort(int nums[],unsigned long len)
{
//控制本轮循环是否发生过交换
//如果没有发生交换,那么说明该数组已经处于有序状态,可以提前结束排序
BOOL hasSwap = YES;
for (int i = 0; i<len && hasSwap; i++) {
//将hasSwap设为NO
hasSwap = NO;
for (int j = 0; j<len - 1- i; j++) {
//如果nums[j]大于nums[j+1],交换他们
if (nums[j]>nums[j+1])
{
int tmp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = tmp;
//本轮循环发生交换,将hasSwap设为yes
hasSwap = YES;
}
}
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
int nums[] = {12,2,23,15,-20,11};
int len = sizeof(nums)/sizeof(nums[0]);
bubbleSort(nums, len);
for (int i = 0; i<len; i++) {
printf("%d ",nums[i]);
}
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}