Lintcode - 整数排序

class Solution {
public:
    /*
     * @param A: an integer array
     * @return: 
     */
    void sortIntegers(vector &A) {
        // write your code here
        //SelectionSort(A);
        //bubleSort(A);
        //selectionSort1(A);
        //insertionSort(A);
        ComparisonCountingSort(A);
    }
    
private:
    /*==================Selection sort======================*/
    void SelectionSort(vector &a) {
        int n = a.size();
        for (int i=0; i &a) {
        int n = a.size();
        
        for (int i = 0; i < n-1; i++) { // a[i]和a[i                //+1]比较,范围到倒数第二个元素
            for (int j = 0; j < n - 1 - i; j++) {  // 末尾减去i个已经排列好的
                if (a[j] > a[j+1]) {
                    swap(a[j], a[j+1]);
                }
            }
        }    
    }
    
    /*==================Insertion Sort=====================*/
    void insertionSort(vector &a) {
        int n = a.size();
        
        for (int i=1; i=0 && a[j]>v) {
                a[j+1] = a[j]; // 后移
                j--;
            }
            a[j+1] = v;
        }
    }
    
    /*==================Selection Sort递归方式=====================*/
    int findSmallest(vector &a, int start, int end) {
        int min = start;
        for (int i = start + 1; i <= end; i++) {
            if (a[i] < a[min])
                min = i;
        }
        
        return min;
    }
    
    void selectionSort1(vector &a, int start, int end) {
        if (start < end - 1) {
            swap(a[start], a[findSmallest(a, start, end)]);
            selectionSort1(a, start + 1, end);
        }
    }
    
    void selectionSort1(vector &a) {
        if (a.size() <= 1)
            return;
    
        selectionSort1(a, 0, a.size() - 1);
    }
    
    /*===============ComparisonCountingSort======================*/
    // 计数排序
    void ComparisonCountingSort(vector &a) {
        int n = a.size();
        
        int Count[n];
        for (int i=0; i res;
        res.resize(n);
        for (int i=0; i

你可能感兴趣的:(数据结构与算法)