int i, j, temp;
int a[] = {1,3,9,8,2,4,5,7,6,0,11};
int length = sizeof(a)/sizeof(a[0]);
for (j = 0; j < length; j++)
{
for (i = 0; i < length - j - 1; i++)
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (i = 0; i < length; i++)
{
printf("%d,", a[i]);
}
printf("\n");
然后看一波我们熟悉的OC版
NSMutableArray *p = [[NSMutableArray alloc] initWithObjects:@"3",@"5",@"4",@"1",@"9",@"0",nil];
for (int i = 0; i<p.count; i++)
{
for (int j=i+1; j<p.count; j++)
{
int a = [p[i] intValue];
int b = [p[j] intValue];
if (a > b)
{
[p replaceObjectAtIndex:i withObject:p[j]];
[p replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@"%d",a]];
}
}
}
NSLog(@"%@",p);