Java教程

常见的排序算法

本文主要是介绍常见的排序算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

快排

int partition(vector<int>& nums, int start, int end) {
    int tmp = nums[start];
    int i = start, j = end;
    while (i < j) {
        while (i < j && nums[j] >= tmp) j--;
        nums[i] = nums[j];

        while (i < j && nums[i] <= tmp) i++;
        nums[j] = nums[i];
    }

    nums[i] = tmp;
    return i;
}

void quickSort(vector<int>& nums, int start, int end) {
    if (start < end) {
        int mid = partition(nums, start, end);
        quickSort(nums, start, mid-1);
        quickSort(nums, mid+1, end);
    }
}

void quickSort(vector<int>& nums) {
    quickSort(nums, 0, nums.size() - 1);
}

快排随机化改进

void random(vector<int>& nums, int l, int r){
    int n = rand()%(r-l+1);
    int tmp = nums[l];
    nums[l] = nums[l+n];
    nums[l+n] = tmp;
}

int pivot(vector<int>& nums, int l, int r){
    random(nums, l, r);
    int tmp = nums[l];
    int i = l, j = r;
    while(i < j){
        while(i < j && nums[j] >= tmp) j--;
        nums[i] = nums[j];
        while(i < j && nums[i] <= tmp) i++;
        nums[j] = nums[i];
    }
    nums[i] = tmp;

    return i;
}

void quickSort(vector<int>& nums, int l, int r){
    if(l < r){
        int mid = pivot(nums, l, r);
        quickSort(nums, l, mid-1);
        quickSort(nums, mid+1, r);
    }
}

vector<int> quickSort(vector<int>& nums) {
    quickSort(nums, 0, nums.size()-1);
    return nums;
}

堆排序

void adjustDownMaxHeap(vector<int>& nums, int i, int n) {//n表示数组nums的长度
    int parent = i, child = 2*i+1, root = nums[i];

    while (child < n) {
        if (child + 1 < n && nums[child + 1] > nums[child]) child++;
        if (root < nums[child]) {
            nums[parent] = nums[child];
            parent = child;
            child = 2 * parent + 1;
        }
        else break;
    }

    nums[parent] = root;
}

void adjustDownMinHeap(vector<int>& nums, int i, int n) {//n表示数组nums的长度
    int parent = i, child = 2 * i + 1, root = nums[i];

    while (child < n) {
        if (child + 1 < n && nums[child + 1] < nums[child]) child++;
        if (root > nums[child]) {
            nums[parent] = nums[child];
            parent = child;
            child = 2 * parent + 1;
        }
        else break;
    }

    nums[parent] = root;
}

void buildMaxHeap(vector<int>& nums) {
    for (int i = nums.size() / 2 - 1; i >= 0; i--) adjustDownMaxHeap(nums, i, nums.size());
}
void buildMinHeap(vector<int>& nums) {
    for (int i = nums.size() / 2 - 1; i >= 0; i--) adjustDownMinHeap(nums, i, nums.size());
}


void heapSort(vector<int>& nums, int tag=0) {
    if (tag == 0) {
        //升序排列
        buildMaxHeap(nums);
        for (int i = nums.size() - 1; i >= 0; i--) {
            swap(nums[0], nums[i]);
            adjustDownMaxHeap(nums, 0, i);
        }
    }
    else if(tag == 1){
        //降序排列
        buildMinHeap(nums);
        for (int i = nums.size() - 1; i >= 0; i--) {
            swap(nums[0], nums[i]);
            adjustDownMinHeap(nums, 0, i);
        }
    }
}

归并排序

void merge(vector<int>& nums, int start, int mid, int end) {
    int* tmp = new int[end - start + 1] {0};
    int i = start, j = mid + 1, k = 0;
    while (i <= mid && j <= end) tmp[k++] = (nums[i] <= nums[j]) ? nums[i++] : nums[j++];
    while (i <= mid) tmp[k++] = nums[i++];
    while (j <= end) tmp[k++] = nums[j++];
    for (int i = 0; i < k; i++) nums[start + i] = tmp[i];
    delete[] tmp;
}

void mergeSort(vector<int>& nums, int start, int end) {
    if (start < end) {
        int mid = start + ((end - start) >> 1);
        mergeSort(nums, start, mid);
        mergeSort(nums, mid + 1, end);
        merge(nums, start, mid, end);
    }
}

void mergeSort(vector<int>& nums) {
    mergeSort(nums, 0, nums.size()-1);
}

插入排序

void insertSort(vector<int>& nums) {
    int size = nums.size();
    for (int i = 1; i < size; i++) {
        int j = i, tmp = nums[j];
        while (j > 0 && tmp < nums[j - 1]) {
            nums[j] = nums[j - 1];
            j--;
        }

        nums[j] = tmp;
    }
}

选择排序

void selectSort(vector<int>& nums) {
    int max, max_idx,size = nums.size();
    for (int i = 1; i < nums.size(); i++) {
        max = nums[0]; max_idx = 0;
        for (int j = 0; j < size - i + 1; j++) {
            if (nums[j] > max) {
                max = nums[j];
                max_idx = j;
            }
        }

        int tmp = nums[size - i];
        nums[size - i] = max;
        nums[max_idx] = tmp;
    }
}

冒泡排序

void bubbleSort(vector<int>& nums) {
    bool tag = false;
    for (int i = 1; i < nums.size(); i++) {
        tag = false;
        for (int j = 0; j < nums.size() - i; j++) {
            if (nums[j] > nums[j + 1]) {
                tag = true;
                int tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
            }
        }

        if (!tag) break;
    }
}
这篇关于常见的排序算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!