C++面试常见问题——06数组排序

数组排序

  1. 冒泡。最简单的冒泡,没啥好讲的

    #include<iostream>
    using namespace std;
    
    void BubbleSort(int a[],int len){
        int i,j;
        int tmp;
        for(i = 0;i < len;i++){
            for(j = i+1;j < len;j++){
                if(a[i] > a[j]){
                    tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                }
            }
        }
    }
    void Print(int a[],int len){
    	for(int i = 0; i < len;i++){
    		cout<<a[i]<<"  ";
    	}
    	cout<<endl;
    }
    int main(){
        int a[] = {2,4,5,4,77,3};
        BubbleSort(a,6);
        Print(a,6);
        return 0;
    }
    
posted @ 2020-02-23 14:42  MenciusYue  阅读(128)  评论(0)    收藏  举报