demo地址:https://github.com/weiman152/PaiXu.git
冒泡排序,两两进行比较,比较完了之后符合条件立马交换。为了更好的理解冒泡排序,先来看一段动画,记录了冒泡的执行过程。
耐心观看完后,我们可以总结出冒泡排序的规则:
1、比较两个数字
2、如果左边的数字大,则交换两个数字的位置
3、向右移动一个位置,比较下两个数字
沿着这个顺序比较下去,一直比较到最右端,虽然没有把所有的数字排好序,但是,数字中最大的那一个已经排放在最右边了,这个是一轮比较下来 可以确定的结果。
下一轮,我们继续从最左边开始。
这也是这个算法被称为冒泡排序的原因:因为在算法执行的时候,最大的数据项 总是 “冒泡”到数组的顶端【数组的排列规则,从左到右0-N】。
j= 0
i= 0: [6, 17, 26, 18, 19, 39, 1, 6, 14, 3, 40]
i= 1: [6, 17, 26, 18, 19, 39, 1, 6, 14, 3, 40]
i= 2: [6, 17, 18(), 26(), 19, 39, 1, 6, 14, 3, 40]
i= 3: [6, 17, 18, 19, 26, 39, 1, 6, 14, 3, 40]
i= 4: [6, 17, 18, 19, 26, 39, 1, 6, 14, 3, 40]
i= 5: [6, 17, 18, 19, 26, 1, 39, 6, 14, 3, 40]
i= 6: [6, 17, 18, 19, 26, 1, 6, 39, 14, 3, 40]
i= 7: [6, 17, 18, 19, 26, 1, 6, 14, 39, 3, 40]
i= 8: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
i= 9: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
看代码吧。
OC
main.m
#import <Foundation/Foundation.h>
#import "Sort.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray * arr = [NSMutableArray arrayWithArray:@[@6,@17,@26,@18,@19,@39,@1,@6,@14,@3,@40]];
Sort * sort = [[Sort alloc] init];
//冒泡排序
NSArray * bubble = [sort bubblingSortWithMutableArray:arr];
NSLog(@"冒泡: %@",bubble);
}
return 0;
}
Sort.h
#import <Foundation/Foundation.h>
@interface Sort : NSObject
/**
冒泡排序
@param array 排序前的数组
@return 排序后的数组
*/
- (NSArray *)bubblingSortWithMutableArray:(NSMutableArray *)array;
@end
Sort.m
#import "Sort.h"
@implementation Sort
//oc 冒泡排序
- (NSArray *)bubblingSortWithMutableArray:(NSMutableArray *)array {
NSMutableArray * arr = [array mutableCopy];
for ( int j=0; j<arr.count-1; j++) {
for (int i=0; i<(arr.count-1)-j; i++) {
if (arr[i]>arr[i+1]) {
[arr exchangeObjectAtIndex:i withObjectAtIndex:i+1];
}
}
}
return arr;
}
@end
Swift语言
main.swift
import Foundation
let arr: [Int] = [6,17,26,18,19,39,1,6,14,3,40]
let mySort: Sort = Sort()
let sortArr: [Int] = mySort.bubblingSort(array: arr)
print("冒泡排序: \(sortArr) ")
Sort.swift
import Cocoa
class Sort: NSObject {
/// 冒泡排序
///
/// - Parameter array: 排序前的数组
/// - Returns: 排序后的数组
func bubblingSort(array: [Int]) -> [Int] {
var arr: [Int] = array
for j in 0..<arr.count {
for i in 0..<arr.count-j-1 {
if arr[i] > arr[i+1] {
arr.swapAt(i, i+1)
}
}
}
return arr
}
}
C语言
#include <stdio.h>
void bubblingArray(int a[],int size);
int main(int argc, const char * argv[]) {
printf("Hello, World!\n");
int a[] = {6,17,26,18,19,39,1,6,14,3,40};
int size = sizeof(a)/sizeof(a[0]);
bubblingArray(a, size);
//经过排序函数后,这里打印的数组也是拍过顺序的,引用传递
for(int i=0; i<size; i++){
printf(" %d ",a[i]);
}
printf("\n");
return 0;
}
/**
冒泡排序
@param a 待排序的数组
@param size 数组的大小
*/
void bubblingArray(int a[],int size) {
for (int j = 0; j< size-1; j++) {
for (int i=0; i< size-1-j; i++) {
if (a[i]>a[i+1]) {
int tmp = a[i]+a[i+1];
a[i] = tmp - a[i];
a[i+1] = tmp - a[i];
}
}
}
//打印
for(int i=0; i<size; i++){
printf(" %d ",a[i]);
}
printf("\n");
}
打印程序执行过程:
j= 0
i= 0: [6, 17, 26, 18, 19, 39, 1, 6, 14, 3, 40]
i= 1: [6, 17, 26, 18, 19, 39, 1, 6, 14, 3, 40]
i= 2: [6, 17, 18, 26, 19, 39, 1, 6, 14, 3, 40]
i= 3: [6, 17, 18, 19, 26, 39, 1, 6, 14, 3, 40]
i= 4: [6, 17, 18, 19, 26, 39, 1, 6, 14, 3, 40]
i= 5: [6, 17, 18, 19, 26, 1, 39, 6, 14, 3, 40]
i= 6: [6, 17, 18, 19, 26, 1, 6, 39, 14, 3, 40]
i= 7: [6, 17, 18, 19, 26, 1, 6, 14, 39, 3, 40]
i= 8: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
i= 9: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
j= 1
i= 0: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
i= 1: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
i= 2: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
i= 3: [6, 17, 18, 19, 26, 1, 6, 14, 3, 39, 40]
i= 4: [6, 17, 18, 19, 1, 26, 6, 14, 3, 39, 40]
i= 5: [6, 17, 18, 19, 1, 6, 26, 14, 3, 39, 40]
i= 6: [6, 17, 18, 19, 1, 6, 14, 26, 3, 39, 40]
i= 7: [6, 17, 18, 19, 1, 6, 14, 3, 26, 39, 40]
i= 8: [6, 17, 18, 19, 1, 6, 14, 3, 26, 39, 40]
j= 2
i= 0: [6, 17, 18, 19, 1, 6, 14, 3, 26, 39, 40]
i= 1: [6, 17, 18, 19, 1, 6, 14, 3, 26, 39, 40]
i= 2: [6, 17, 18, 19, 1, 6, 14, 3, 26, 39, 40]
i= 3: [6, 17, 18, 1, 19, 6, 14, 3, 26, 39, 40]
i= 4: [6, 17, 18, 1, 6, 19, 14, 3, 26, 39, 40]
i= 5: [6, 17, 18, 1, 6, 14, 19, 3, 26, 39, 40]
i= 6: [6, 17, 18, 1, 6, 14, 3, 19, 26, 39, 40]
i= 7: [6, 17, 18, 1, 6, 14, 3, 19, 26, 39, 40]
j= 3
i= 0: [6, 17, 18, 1, 6, 14, 3, 19, 26, 39, 40]
i= 1: [6, 17, 18, 1, 6, 14, 3, 19, 26, 39, 40]
i= 2: [6, 17, 1, 18, 6, 14, 3, 19, 26, 39, 40]
i= 3: [6, 17, 1, 6, 18, 14, 3, 19, 26, 39, 40]
i= 4: [6, 17, 1, 6, 14, 18, 3, 19, 26, 39, 40]
i= 5: [6, 17, 1, 6, 14, 3, 18, 19, 26, 39, 40]
i= 6: [6, 17, 1, 6, 14, 3, 18, 19, 26, 39, 40]
j= 4
i= 0: [6, 17, 1, 6, 14, 3, 18, 19, 26, 39, 40]
i= 1: [6, 1, 17, 6, 14, 3, 18, 19, 26, 39, 40]
i= 2: [6, 1, 6, 17, 14, 3, 18, 19, 26, 39, 40]
i= 3: [6, 1, 6, 14, 17, 3, 18, 19, 26, 39, 40]
i= 4: [6, 1, 6, 14, 3, 17, 18, 19, 26, 39, 40]
i= 5: [6, 1, 6, 14, 3, 17, 18, 19, 26, 39, 40]
j= 5
i= 0: [1, 6, 6, 14, 3, 17, 18, 19, 26, 39, 40]
i= 1: [1, 6, 6, 14, 3, 17, 18, 19, 26, 39, 40]
i= 2: [1, 6, 6, 14, 3, 17, 18, 19, 26, 39, 40]
i= 3: [1, 6, 6, 3, 14, 17, 18, 19, 26, 39, 40]
i= 4: [1, 6, 6, 3, 14, 17, 18, 19, 26, 39, 40]
j= 6
i= 0: [1, 6, 6, 3, 14, 17, 18, 19, 26, 39, 40]
i= 1: [1, 6, 6, 3, 14, 17, 18, 19, 26, 39, 40]
i= 2: [1, 6, 3, 6, 14, 17, 18, 19, 26, 39, 40]
i= 3: [1, 6, 3, 6, 14, 17, 18, 19, 26, 39, 40]
j= 7
i= 0: [1, 6, 3, 6, 14, 17, 18, 19, 26, 39, 40]
i= 1: [1, 3, 6, 6, 14, 17, 18, 19, 26, 39, 40]
i= 2: [1, 3, 6, 6, 14, 17, 18, 19, 26, 39, 40]
j= 8
i= 0: [1, 3, 6, 6, 14, 17, 18, 19, 26, 39, 40]
i= 1: [1, 3, 6, 6, 14, 17, 18, 19, 26, 39, 40]
j= 9
i= 0: [1, 3, 6, 6, 14, 17, 18, 19, 26, 39, 40]
j= 10
冒泡排序: [1, 3, 6, 6, 14, 17, 18, 19, 26, 39, 40]