`

冒泡排序,选择排序,插入排序

阅读更多

冒泡排序:

int* bubbleSort(int* A, int n) {
        // write code here by Kean
        int len = n-1;
        for(int i = 0; i < n-1; (len--, i++)){
            for(int j = 0; j < len; j++){
                if(A[j] > A[j+1]){
                    int t = A[j];
                    A[j] = A[j+1];
                    A[j+1] = t;
                }
            }
        }
        return A;
    }

 选择排序

 

int* selectionSort(int* A, int n) {
        // write code here written by Kean
        for(int i = 0; i < n-1; i++){
            for(int j = i+1; j < n; j++){
                if(A[i] > A[j]){
                    int t = A[i];
                    A[i] = A[j];
                    A[j] = t;
                }
            }
        }
        return A;
    }

 插入排序:

 

int* insertionSort(int* A, int n) {
        // write code here
        for(int i = 1; i < n; i++){
            for(int j = i; j >= 0; j--){
                if(A[j] < A[j-1]){
                    int t = A[j-1];
                    A[j-1] = A[j];
                    A[j] = t;
                }
            }
        }
            return A;
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics