算法-冒泡排序


算法-冒泡排序

简单定义:大数下沉,小数起泡
代码实现如下:

1
void funcScort(int arr[],int len){  
2
    for (int i = 0; i< len - 1; i++) {  
3
        for(int j = 0 ;j<len -1-i;j++ ){  
4
            if (arr[j]>arr[j+1]) {  
5
                int temp;  
6
                temp = arr[j];  
7
                arr[j] = arr[j+1];  
8
                arr[j+1] = temp;  
9
            }  
10
        }  
11
    }  
12
}  
13
14
int main(int argc, const char * argv[]) {  
15
    int a[10] = {1,23,5,89,53,33,98,75,64,48};  
16
    //排序  
17
    funcScort(a, 10);  
18
    //打印  
19
    for (int i = 0; i<10; i++) {  
20
        printf("%d\t",a[i]);  
21
    }  
22
    return 0;  
23
}  
24
25
//输出结果:1 5 23 33 48 53 64 75 89 98

算法-选择排序

简单定义:每次把未查找部分的最小的数放在前面
代码实现如下:

1
void selectSort(int arr[],int len){  
2
    for (int i=0; i<len-1; i++) {  
3
        for (int j = i+1; j<len; j++) {  
4
            if (arr[i]>arr[j]) {  
5
                int temp;  
6
                temp = arr[i];  
7
                arr[i] = arr[j];  
8
                arr[j] = temp;  
9
            }  
10
        }  
11
    }  
12
}  
13
14
int main(int argc, const char * argv[]) {  
15
    // insert code here...  
16
    int a[10] = {1,23,5,89,53,33,98,75,64,48};  
17
    //排序  
18
    selectSort(a, 10);  
19
    //打印  
20
    for (int i = 0; i<10; i++) {  
21
        printf("%d\t",a[i]);  
22
    }  
23
    return 0;  
24
}
25
26
//输出结果:1 5 23 33 48 53 64 75 89 98
#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×