代码随想录--排序算法

912.排序数组

快速排序

思路:

复制代码
		1.  设置一个pivot
		2. 将小于nums[pivot]的值 放在左边
		3. 将 大于nums[pivot]的值  放在 右边
		4. 递归调用

注意:必须先比较numshigh 与pivot

代码:

cpp 复制代码
class Solution {

    int partition(vector<int>&nums, int low, int high){
        int pivot = nums[low];
        while(low<high){
            while(low <high && nums[high]>=pivot){
                high--;
            }nums[low] = nums[high];

            while(low<high && nums[low]<=pivot){
                low++;
            }nums[high] = nums[low];

        }
        nums[low] = pivot;
        return low;
    }

    void quiksort(vector<int>&nums, int low, int high){
        if(low<high){
            int pos = partition(nums, low, high);
            quiksort(nums, low, pos-1);
            quiksort(nums, pos+1, high);
        }
    }

public:
    vector<int> sortArray(vector<int>& nums) {
        quiksort(nums, 0, (int)nums.size()-1);
        return nums;
    }
};

堆排序

思路:

复制代码
1.构造大顶堆   
2. 取出堆顶元素
3. 构造大顶堆
4. 取出堆顶元素	

将堆顶元素放入末尾,下次构造大顶堆时不考虑

注意:

代码:

cpp 复制代码
class Solution {
    void buildMaxHeap(vector<int>& nums) {
        int n = nums.size();
        for (int i = (n - 1) / 2; i >= 0; --i) {
            maxHeapify(nums, i, n);
        }
    }

    void maxHeapify(vector<int>& nums, int i, int n) {
        while (i * 2 + 1 < n) {
            // 代表当前 i 节点的左右儿子;
            // 超出数组大小则代表当前 i 节点为叶子节点,不需要移位
            int lSon = 2 * i + 1;
            int rSon = 2 * i + 2;
            int large = i;
            if (lSon < n && nums[lSon] > nums[i]) large = lSon;
            if (rSon < n && nums[rSon] > nums[large]) large = rSon;

            if (large != i) {
                swap(nums[i], nums[large]);
                // 迭代判断对应子节点及其儿子节点的大小关系
                i = large;
            } else {
                break;
            }
        }
    }

public:
    vector<int> sortArray(vector<int>& nums) {
        // heapSort 堆排序
        int n = nums.size();
        // 将数组整理成大根堆
        buildMaxHeap(nums);
        for (int i = n - 1; i >= 1; --i) {
            // 依次弹出最大元素,放到数组最后,当前排序对应数组大小 - 1
            swap(nums[0], nums[i]);
            --n;
            maxHeapify(nums, 0, n);
        }
        return nums;
    }
};

归并排序

思路: 将有序序列 合并成一个

注意:

代码:

cpp 复制代码
class Solution {
    vector<int> tmp;
    void mergeSort(vector<int>& nums, int l, int r) {
        if (l >= r) return;
        int mid = (l + r) >> 1;
        mergeSort(nums, l, mid);
        mergeSort(nums, mid + 1, r);
        int i = l, j = mid + 1;
        int cnt = 0;
        while (i <= mid && j <= r) {
            if (nums[i] <= nums[j]) {
                tmp[cnt++] = nums[i++];
            }
            else {
                tmp[cnt++] = nums[j++];
            }
        }
        while (i <= mid) {
            tmp[cnt++] = nums[i++];
        }
        while (j <= r) {
            tmp[cnt++] = nums[j++];
        }
        for (int i = 0; i < r - l + 1; ++i) {
            nums[i + l] = tmp[i];
        }
    }
public:
    vector<int> sortArray(vector<int>& nums) {
        tmp.resize((int)nums.size(), 0);
        mergeSort(nums, 0, (int)nums.size() - 1);
        return nums;
    }
};
相关推荐
To_OC10 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
金銀銅鐵13 小时前
[Python] 扩展欧几里得算法
python·数学·算法
To_OC16 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
To_OC1 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
刘马想放假2 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
05Kevin2 天前
lk每日冒险题--数据结构6.27
算法
To_OC2 天前
从一次栈溢出报错说起,我把递归彻底扒明白了
javascript·算法·程序员
千纸鹤安安3 天前
千问Qwen-AgentWorld来了:一个语言模型搞定七大Agent场景,GPT-5.4都输了
算法