数据结构中的排序

#include <bits/stdc++.h>
using namespace std;
int n,a[1000],b[1000];
void QuickSort(int a[],int l,int r) {
    if (l < r) {
        int x = a[l], i = l, j = r;
        while (i < j) {
            while (i < j && a[j] >= x) j--;
            if (i < j) a[i++] = a[j];
            while (i < j && a[i] <= x) i++;
            if (i < j) a[j--] = a[i];
        }
        a[i] = x;
        QuickSort(a, l, i - 1);
        QuickSort(a, i + 1, r);
    }
}

void merge_sort(int l,int r) {
    if (l == r) {
        return;
    }
    int mid = l + r >> 1;
    merge_sort(l, mid);
    merge_sort(mid + 1, r);
    int i = l, j = mid + 1, k = l;
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) {
            b[k++] = a[i++];
        } else {
            //ans += mid - i + 1;
            b[k++] = a[j++];
        }
    }
    while (i <= mid) {
        b[k++] = a[i++];
    }
    while (j <= r) {
        b[k++] = a[j++];
    }
    for (int i = l; i <= r; i++) {
        a[i] = b[i];
    }
}

void insert_sort(int a[],int n) {
    for (int i = 2,j; i <=n; i++) {
        for (j = 1; j < i; j++) {
            if (a[i] < a[j])
                break;
        }
        if (i != j) {
            int key = a[i];
            for (int k = i; k > j; k--)
                a[k] = a[k - 1];
            a[j] = key;
        }
    }
}

void bobbleSort(int a[],int n) {
    for (int i = 1; i <=n; i++) {
        for (int j = 1; j <=n- i; j++) {
            if (a[j] > a[j + 1]) {
                int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}
void down(int a[],int n, int i) {
    while (i*2 <= n) {
        i=i*2;
        if (i + 1 <= n && a[i] < a[i + 1]) {
            i++;
        }
        if (a[i] >= a[i / 2]) {
            swap(a[i], a[i / 2]);
        } else break;
    }
}
void heapsort(int a[],int n) {
    for (int i = n / 2; i >= 1; i--) {
        down(a,n, i);
    }
    for (int i = n; i >= 1; i--) {
        swap(a[i], a[1]);
        down(a,i - 1, 1);
    }
}
void selectSort(int a[],int n) {
    for (int i = 1; i < n; i++) {
        int mi = i;
        for (int j = i + 1; j <= n; j++) {
            if (a[j] < a[mi]) {
                mi = j;
            }
        }
        if (mi != i) {
            swap(a[i], a[mi]);
        }
    }
}

void shellSort(int a[],int n) {
    for (int step = n >> 1; step > 0; step >>= 1) {
        for (int i = step; i <=n; i += step) {
            int j = i - step;
            int temp = a[i];
            while (j >= 1 && a[j] > temp) {
                a[j + step] = a[j];
                j -= step;
            }
            a[j + step] = temp;
        }
    }
}
void print(int a[],int n) {
    for (int i = 1; i <= n; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}
int main() {
    srand((int) time(NULL));
    int n = rand() % 40;
    for (int i = 1; i <= n; i++) {
        a[i] = rand() % 1000;
    }

    random_shuffle(a + 1, a + n + 1);
    QuickSort(a, 1, n);
    print(a, n);

    random_shuffle(a + 1, a + n + 1);
    merge_sort(1, n);
    print(a, n);

    random_shuffle(a + 1, a + n + 1);
    insert_sort(a, n);
    print(a, n);

    random_shuffle(a + 1, a + n + 1);
    bobbleSort(a, n);
    print(a, n);

    random_shuffle(a + 1, a + n + 1);
    selectSort(a, n);
    print(a, n);

    random_shuffle(a + 1, a + n + 1);
    heapsort(a, n);
    print(a, n);

    random_shuffle(a + 1, a + n + 1);
    shellSort(a, n);
    print(a, n);
}
posted @ 2020-06-10 17:46  Snow_in_winer  阅读(167)  评论(0编辑  收藏  举报