首页 > 编程语言 >c++ 五种排序方式

c++ 五种排序方式

时间:2023-03-03 15:23:56浏览次数:30  
标签:std cout nums int auto c++ temp1 五种 排序

第一种:冒泡排序

冒泡排序遍历两次数组,时间复杂度On2,每一次用一个元素和后续其他所有元素比较,若是大小不符合预期则反转位置,具体实现代码如下:

#include <iostream>
#include <vector>
void Printf(const std::vector<int>&nums)
{
    for(auto num:nums)
    {
        std::cout<<"num:"<<num<<std::endl;
    }
}
void BubbleSort(std::vector<int>&nums)
{
    for(int i = 0 ; i < nums.size() ; ++i)
    {
        for(int j = 0; j < nums.size()-1; ++j)
        {
            if(nums[j]<nums[j+1])
            {
                auto temp = nums[j];
                nums[j] = nums[j+1];
                nums[j+1] = temp;
            }
        }
    }
}
int main()
{
    std::vector<int> nums = {1,5,2,9,6,4,8,3,11,5};
    auto temp1 = nums;
    Printf(temp1);
    BubbleSort(temp1); std::cout<<"after BubbleSort"<<std::endl; Printf(temp1);

}

第二种:选择排序

选择排序是按照从大到小或者从小到大的顺序,每次选择后续元素最大最小的值放在当前应该输入位置上,具体实现代码如下:

#include <iostream>
#include <vector>
void Printf(const std::vector<int>&nums)
{
    for(auto num:nums)
    {
        std::cout<<"num:"<<num<<std::endl;
    }
}
void SelectSort(std::vector<int>&nums)
{
    auto max = 0; 
    int pos = 0;
    for(int i = 0; i < nums.size()-1; ++i)
    {
        max = nums[i];
        pos = i;
        for(int j = i+1; j < nums.size(); ++j)
        {
            if(nums[j]>max)
            {
                max = nums[j];
                pos = j;
            }
        }
        nums[pos] = nums[i];
        nums[i] = max;
    }
}
int main()
{
    std::vector<int> nums = {1,5,2,9,6,4,8,3,11,5};
    // auto temp1 = nums;
    // Printf(temp1);
    // BubbleSort(temp1); std::cout<<"after BubbleSort"<<std::endl; Printf(temp1);

    std::cout<<"---------------------------------------------------"<<std::endl;
    auto temp2 = nums;
    Printf(temp2);
    SelectSort(temp2); std::cout<<"after SelectSort"<<std::endl; Printf(temp2);

}

三:插入排序

插入排序,一般也被称为直接插入排序。对于少量元素的排序,它是一个有效的算法 。插入排序是一种最简单的排序方法,它的基本思想是将一个记录插入到已经排好序的有序表中,从而一个新的、记录数增1的有序表。在其实现过程使用双层循环,外层循环对除了第一个元素之外的所有元素,内层循环对当前元素前面有序表进行待插入位置查找,并进行移动。

具体代码实现如下:

 

#include <iostream>
#include <vector>
void Printf(const std::vector<int>&nums)
{
    for(auto num:nums)
    {
        std::cout<<"num:"<<num<<std::endl;
    }
}
void InsertSort(std::vector<int>&nums)
{
    int insertIndex,insertVal;
    insertIndex= insertVal = 0;
    for(int i = 1; i < nums.size(); ++i)
    {
        insertVal = nums[i];
        insertIndex = i-1;
        while(insertIndex>=0&&nums[insertIndex]<insertVal)
        {
            nums[insertIndex+1] = nums[insertIndex];
            insertIndex--;
        }
        if(insertIndex + 1 != i)
        {
            nums[insertIndex + 1] = insertVal;
        }
    }
}

int main()
{
    std::vector<int> nums = {1,5,2,9,6,4,8,3,11,5};
    // auto temp1 = nums;
    // Printf(temp1);
    // BubbleSort(temp1); std::cout<<"after BubbleSort"<<std::endl; Printf(temp1);

    std::cout<<"---------------------------------------------------"<<std::endl;
    // auto temp2 = nums;
    // Printf(temp2);
    // SelectSort(temp2); std::cout<<"after SelectSort"<<std::endl; Printf(temp2);

    auto temp3 = nums;
    Printf(temp3);
    InsertSort(temp3); std::cout<<"after InsertSort"<<std::endl; Printf(temp3);


}

第四种:快速排序

标签:std,cout,nums,int,auto,c++,temp1,五种,排序
From: https://www.cnblogs.com/52ld/p/17175717.html

相关文章

  • Linux开发C++
    首先在windows上安装linux系统。VM简介VMwareWorkstation中文版是一个“虚拟PC”软件。它使你可以在一台机器上同时运行二个或更多Windows、DOS、LINUX系统。与“多......
  • 堆排序
    【经典算法】:堆排序 1.概述堆排序(HeapSort)就是利用堆(假设利用大顶堆)进行排序的方法。原理:将待排序的序列构造成一个大顶堆,此时,整个序列的最大值就是堆顶的根结点,将......
  • 【大型软件开发】开发日志(五).net框架与C++的融合:CLR——C++调用C#的DLL
    做什么?先说一下场景,现在正在开发一个QtActiveServer,也就是用一个应用程序去向其他的组件暴露接口,以达到提供服务的目的。然后新版的框架要提供大部分功能,也就是要重做大......
  • mysql对一二三四五排序
    SELECTbuildingFloorsFROMConstructionCaseORDERBYFIELD(SUBSTRING(buildingFloors,1,1),'一','二','三','四','五','六','七','八','九'); ......
  • C/C++学生考勤系统[2023-03-03]
    C/C++学生考勤系统[2023-03-03]功能:学生考勤系统应包含各班学生的全部信息。每个学生是一条记录,包括姓名、性别、学号、出勤情况等。本系统可模拟考勤过程,记录考勤结果,并......
  • C/C++ 数据结构使用数组实现队列的基本操作
    //使用数组实现队列#include<iostream>#include<Windows.h>usingnamespacestd;#defineMAXSIZE5//队列的最大容量typedefintDataType;//队列中的元素类型......
  • c++注释
    注释分为单行注释和界定符对注释单行注释://界定符对注释:/*      */(用于注释多行)注意:当界定符对注释多行时更规范的做写法是每一行前面都要有个星号  ......
  • c/c++ 随机数
    #include<stdio.h>#include<math.h>#include<stdlib.h>#include<algorithm>#include<string.h>#include<time.h>//头文件#include<iostream>usingnamesp......
  • UEC++学习(1)
    第三章流程控制第一节C++和蓝图循环ForLoop蓝图节点相当于C++中的for循环,ForeLoopWithBreak节点相当于for循环体中加了break语句,当触发某个条件时直接结束。for(){ ......
  • UEC++学习(2)
    第三章断言第一节简单的断言断言让程序的进程中断,方便程序员发现在哪里发生了问题。AGameBase*GameBase=nullptr;check(false);check(GameBase);//断言给false......