冒泡排序
2018-11-15 本文已影响0人
mrChan1234
//MARK:冒泡排序的oc写法
- (NSMutableArray *)bubbleSortWithArray:(NSMutableArray *)aArray {
for (int i = 0 ; i < aArray.count; i ++) {
//比较n-1次
for (int j = 0 ; j < aArray.count - 1; j ++) {
if (aArray[j] < aArray[j + 1]) {
[aArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
return aArray;
}
//MARK:C语言版本的冒泡排序
- (void)bubbleSortWithCLanguageMethod {
int a[10],i,j;
int t;
for ( i = 0 ; i < 10; i ++) {
//依次输入
scanf("%d",&a[i]);
}
for (i = 0; i < 9; i ++) {
for ( j = 0; j < 9-i; j ++) {
if (a[j] < a[j + 1]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
//输出排序结果
for (int i = 0 ; i < 10; i ++) {
printf("%d",a[i]);
}
}