其他分享
首页 > 其他分享> > 备考研究生之几种排序

备考研究生之几种排序

作者:互联网

排序

冒泡排序

void BubbleSort(vector<int>& a){
    int n = a.size();
    for (int i = 0; i < n; ++i) {
        for (int j = n-1; j > i; --j) {
            if (a[j] < a[j-1])
                swap(a[j],a[j-1]);
        }
    }
}

选择排序

void SelectionSort(vector<int>& a){
    int n = a.size();
    for (int i = 0; i < n; ++i) {
        int min = a[i];
        int minindex = i;
        for (int j = i+1; j < n; ++j) {
            if (a[j] < min){
                min = a[j];
                minindex = j;
            }
        }
        swap(a[i],a[minindex]);
    }
}

插入排序

void InsertionSort(vector<int>& a){
    int n = a.size();
    for (int i = 1; i < n; ++i) {
        int now = a[i];
        int j = i - 1;
        while(j >= 0 && a[j] > now){
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = now;
    }
}

希尔排序

void ShellSort(vector<int>& a){
    int n = a.size();
    for (int gap = n / 2; gap > 0; gap /= 2) {
        for (int i = gap; i < n; ++i) {
            int j = i;
            int current = a[i];
            while(j - gap >= 0 && current < a[j - gap]){
                a[j] = a[j - gap];
                j -= gap;
            }
            a[j] = current;
        }
    }
}

归并排序

void MergeSort(vector<int>& a,int left,int right){
    if (left < right){
        int mid = (left + right) / 2;
        MergeSort1(a,left,mid);
        MergeSort1(a,mid+1,right);
        Merge1(a,left,mid,right);
    }
}

void Merge(vector<int>& a,int left,int mid,int right){
    vector<int> temp;
    int l = left,r = mid + 1;
    while (l <= mid && r <= right){
        if (a[l] < a[r]){
            temp.push_back(a[l]);
            l++;
        }
        else{
            temp.push_back(a[r]);
            r++;
        }
    }
    while (l <= mid)
        temp.push_back(a[l++]);
    while (r <= right)
        temp.push_back(a[r++]);
    int i = 0,j = left;
    while (j <= right)
        a[j++] = temp[i++];
}

快速排序

void QuickSort(vector<int>& a,int left,int right){
    if (left<right){
        int mid = Partition(a,left,right);
        QuickSort(a,left,mid-1);
        QuickSort(a,mid+1,right);
    }
}

int Partition(vector<int>& a,int left,int right){
    int current = a[right];
    int i = left,j = right;
    while(i < j){
        while (i < j && a[i] < current)
            i++;
        a[j] = a[i];
        while (i < j &&a[j] > current)
            j--;
        a[i] = a[j];
    }
    a[i] = current;
    return i;
}

堆排序

(升序用大根堆,降序用小根堆)

void HeapSort(vector<int>& a){
    BuildMaxHeap(a);

    int n = a.size();
    for (int j = 0; j < n; ++j) {
        cout<<a[j]<<" ";
    }
    cout<<endl;

    int k = n;
    for (int i = n - 1; i > 0; --i) {
        swap(a[i],a[0]);
        k--;
        heapify(a,0,k);
    }
}

void BuildMaxHeap(vector<int>& a){
    int n = a.size();
    for (int i = n / 2; i >= 0 ; --i) {
        heapify(a,i,n);
    }
}

void heapify(vector<int>& a,int i,int n){
    int left = 2 * i + 1;
    int right = 2 * i + 2;
    int largest = i;
    if (left < n && a[left] > a[largest]){
        largest = left;
    }
    if (right < n && a[right] > a[largest]){
        largest = right;
    }
    if (largest != i){
        swap(a[i],a[largest]);
        heapify(a,largest,n);
    }
}

标签:研究生,right,int,void,vector,备考,largest,排序,left
来源: https://blog.csdn.net/qq_41143484/article/details/121079506