首页 > 编程语言 >常见排序算法

常见排序算法

时间:2023-01-02 23:22:37浏览次数:44  
标签:tmp string nums int 常见 ret 算法 ans 排序

1、冒泡排序

class Solution {
public:
    string minNumber(vector<int>& nums) {
        int len = nums.size();
        for (int i = 0; i < len  - 1; i++){
            bool isSort = true;
            for (int j = 0; j < len - 1 - i ; j++){
                string s1 = to_string (nums[j]) + to_string (nums[j + 1]);
                string s2 = to_string (nums[j + 1]) + to_string (nums[j]);
                if (s1 > s2){
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                    isSort = false;
                }
            }

            if (isSort) break;
        }

        string ret = "";
        for (int i = 0; i < len; i++){
            ret += to_string (nums[i]);
        }
        
        return ret;
    }
};

2、选择排序

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 选择排序
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums){
            ans.push_back(to_string(num));
        }

        for (int i = 0; i < len; i++){
            int minIndex = i;
            for (int j = i + 1; j < len; j++){
                if (ans[minIndex] + ans[j] > ans[j] + ans[minIndex]){
                    minIndex = j;
                }
            }

            string temp = ans[minIndex];
            ans[minIndex] = ans[i];
            ans[i] = temp;
        }

        string ret = "";
        for (auto str : ans){
            ret += str;
        }
        
        return ret;
    }
};

3、插入排序

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 插入排序
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums) ans.push_back(to_string(num));

        for (int i = 1; i < len; i++){
            for (int j = i; j > 0 && ans[j - 1] + ans[j] > ans[j] + ans[j - 1]; j--){
                string tmp = ans[j - 1];
                ans[j - 1] = ans[j];
                ans[j] = tmp;
            }
        }

        string ret = "";
        for (auto str : ans) ret += str;
        return ret;
    }
};

4、希尔排序

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 希尔排序
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums) ans.push_back(to_string(num));

        int h = 1;
        while (h < len / 3) h = 3 * h + 1;

        while (h >= 1){
            for (int i = h; i < len; i++){
                for (int j = i; j >= h && ans[j - h] + ans[j] > ans[j] + ans[j - h]; j -= h){
                    string tmp = ans[j - h];
                    ans[j - h] = ans[j];
                    ans[j] = tmp;
                }
            }
            h /= 3;
        }
        
        string ret = "";
        for (auto str : ans) ret += str;
        return ret;
    }
};

5、归并排序-自顶向下

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 归并排序,自顶向下
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums) ans.push_back(to_string(num));
        vector<string> tmp(len);
        Sort(ans, tmp, 0, len - 1);
        string ret = "";
        for (auto str : ans) ret += str;
        return ret;
    }

    void Sort(vector<string>& ans, vector<string> tmp, int left, int right){
        if (right <= left) return;
        int mid = left + (right - left) / 2;
        Sort(ans, tmp, left, mid);
        Sort(ans, tmp, mid + 1, right);
        Merge(ans, tmp, left, mid, right);
    }

    void Merge(vector<string>& ans, vector<string> tmp, int left, int mid, int right){
        int i = left, j = mid + 1;
        for (int k = left; k <= right; k++){
            tmp[k] = ans[k];
        }
        for (int k = left; k <= right; k++){
            if (i > mid) ans[k] = tmp[j++];
            else if (j > right) ans[k] = tmp[i++];
            else if (tmp[i] + tmp[j] > tmp[j] + tmp[i]) ans[k] = tmp[j++];
            else ans[k] = tmp[i++];
        }
    }
};

6、归并排序-自底向上

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 归并排序,自底向上
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums) ans.push_back(to_string(num));
        vector<string> tmp(len);

        for (int i = 1; i < len; i = i + i){
            for (int j = 0; j < len - i; j += i + i){
                Merge(ans, tmp, j, j + i - 1, min(j + i + i - 1, len - 1));
            }
        }
        
        string ret = "";
        for (auto str : ans) ret += str;
        return ret;
    }


    void Merge(vector<string>& ans, vector<string> tmp, int left, int mid, int right){
        int i = left, j = mid + 1;
        for (int k = left; k <= right; k++){
            tmp[k] = ans[k];
        }
        for (int k = left; k <= right; k++){
            if (i > mid) ans[k] = tmp[j++];
            else if (j > right) ans[k] = tmp[i++];
            else if (tmp[i] + tmp[j] > tmp[j] + tmp[i]) ans[k] = tmp[j++];
            else ans[k] = tmp[i++];
        }
    }
};

7、快速排序

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 快速排序
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums) ans.push_back(to_string(num));
        sort(ans, 0, len - 1);
        string ret = "";
        for (auto str : ans) ret += str;
        return ret;
    }

    void sort(vector<string>& ans, int left, int right){
        if (right <= left) return;
        int j = partition(ans, left, right);
        sort(ans, left, j - 1);
        sort(ans, j + 1, right);
    }
    
    // 切分
    int partition(vector<string>& ans, int left, int right){
        int i = left, j = right;
        string pivot = ans[left];
        while (true){
            while (ans[i] + pivot <= pivot + ans[i]){
                if (++i > j) break;
            }
            while (pivot + ans[j] < ans[j] + pivot){
                if (--j < i) break;
            }
            if (i >= j) break;
            string tmp = ans[i];
            ans[i] = ans[j];
            ans[j] = tmp;
        }
        string tmp = ans[left];
        ans[left] = ans[j];
        ans[j] = tmp;
        return j;
    }
};

8.快速排序-三向切分

class Solution {
public:
    string minNumber(vector<int>& nums) {
        // 快速排序-三向切分
        int len = nums.size();
        vector<string> ans;
        for (auto num : nums) ans.push_back(to_string(num));
        sort(ans, 0, len - 1);
        string ret = "";
        for (auto str : ans) ret += str;
        return ret;
    }

    void sort(vector<string>& ans, int left, int right){
        if (right <= left) return;
        int lt = left, i = left + 1, gt = right;
        string tmp = ans[left];
        while(i <= gt){

            if (ans[i] + tmp == tmp + ans[i]){
                i++;
            }
            else if (ans[i] + tmp < tmp + ans[i]){
                exchange(ans, lt++, i++);
            }
            else{
                exchange(ans, i, gt--);
            }
        }

        sort(ans, left, lt - 1);

 

标签:tmp,string,nums,int,常见,ret,算法,ans,排序
From: https://www.cnblogs.com/jerry-autumn/p/17020837.html

相关文章

  • [Phoenix基础]-- 常见问题解答
    常问问题​​我想开始 有没有凤凰HelloWorld?​​​​凤凰城有没有办法批量加载?​​​​如何将Phoenix表映射到现有的HBase表?​​​​有没有任何提示来优化凤凰?​​​​如......
  • 使用lambda表达式实现sort的自定义排序
    使用lambda表达式实现sort的自定义排序(C++andJava)首先大致讲一下什么是lambda表达式你也可以将它就当做是匿名函数,lambda表达式其实就是匿名函数演化出的一种语法系统......
  • 代码随想录算法训练营第五天LeetCode242,349,202,1
    代码随想录算法训练营第五天|LeetCode242,349,202,1LeetCode242有效的字母异位词题目链接:https://leetcode.cn/problems/valid-anagram/description///开了两个哈希数组,......
  • [Hive排序]--4种排序方式介绍
    一、官方文档​​Home-ApacheHive-ApacheSoftwareFoundation​​​​LanguageManual-ApacheHive-ApacheSoftwareFoundation​​​​LanguageManualSortBy-......
  • Java Map实现按value排序
    JavaMap实现按value排序如果想按照key来排序,用TreeMap就可以;如果想实现按value排序,可以采用下面这种方式publicstaticvoidmain(String[]args){Map<St......
  • 开源!《AI 算法工程师手册》中文教程正式发布!
    最近红色石头在浏览网页的时候,偶然发现一份非常不错的AI资源,就是这本《AI算法工程师手册》。本文将给大家推荐这本优秀教材,并作详细的介绍。这本《AI算法工程师手册》......
  • 遗传算法解决旅行商问题(TSP)
    遗传算法解决旅行商问题作者:Cukor丘克环境:MatlabR2020a+vscode问题描述旅行商问题(TSP).一个商人欲从自己所在的城市出发,到若干个城市推销商品,然后回到其所在的城市......
  • 海康威视常见道闸控制盒接线图及外部接口说明
    类型一,适用型号DS-TMG51X/52X道闸、守蔚系列道闸道闸及控制盒接线示意图:外部接口说明:1、抓拍机(或出入口终端)继电器输出信号接入道闸:控制开+、控制开-,控制关+、控制关-2、......
  • 代码随想录算法训练营第六天
    今日刷题4道:先复习了哈希表理论基础,然后刷了4道题。242.有效的字母异位词,349.两个数组的交集,202.快乐数,1.两数之和。为什么没有第5天呢,因为周日休息!ps:什么时候想到用......
  • m基于VDLL的矢量型GPS信号跟踪算法matlab仿真
    1.算法概述载波跟踪环是传统独立式GPS接收机最脆弱的环节,针对弱信号环境下其比伪码跟踪环路更容易失锁的问题,给出一种基于矢量频率锁定环(vector-frequencylockloop,VFLL......