c++ 排序 冒泡 插入 选择 快速
//冒泡
#include <iostream>
using namespace std; void bubbleSort(int* list,int index)
{
for(int i=index;i>;i--) //index 最大的那个索引
{
for(int j=;j<i;j++)
{
if(list[j]>list[j+])
{
int temp;
temp=list[j];
list[j]=list[j+];
list[j+]=temp;
}
}
}
} void main()
{
int list[]={,,,,,,,,}; for(int i=;i<;i++)
cout<<list[i]<<" | ";
cout<<endl; bubbleSort(list,);//index:= 9-1 for(int i=;i<;i++)
cout<<list[i]<<" | ";
} //插入排序法
#include <iostream>
using namespace std; void insertSort(int* list,int index)
{
//在排序之前我们需要搞清一个思路,新插入一个数据的时候,排序过后的数组都是
//从小到大排列好的,所以我们需要从后往前查找,直到找到比我们要插入的数字还小的
//值。这个时候我们需要一个变量j作为标识 //从1开始
for(int i=;i<=index;i++) //index 最大的那个索引
{
int insertNode=list[i];
int j;
for(j=i-;j>=;j--)
{
if(insertNode<list[j])
list[j+]=list[j];
else
break; //因为前面的已经排序好,所以找到位置后 就可以退出了
}
list[j+]=insertNode;
} } void main()
{
int list[]={,,,,,,,,}; for(int i=;i<;i++)
cout<<list[i]<<" | ";
cout<<endl; insertSort(list,);//index:= 9-1 for(int i=;i<;i++)
cout<<list[i]<<" | ";
} //选择排序法
#include <iostream>
using namespace std; void selectSort(int* list,int index)
{
for(int i=;i<=index;i++) //index 最大的那个索引
{
int minValue=list[i];
int minIndex=i; for(int j=i;j<=index;j++)
{
if(minValue>list[j])
{
minValue=list[j];
minIndex=j;
}
} int temp;
temp=list[i];
list[i]=list[minIndex];
list[minIndex]=temp;
} } void main()
{
int list[]={,,,,,,,,}; for(int i=;i<;i++)
cout<<list[i]<<" | ";
cout<<endl; selectSort(list,);//index:= 9-1 for(int i=;i<;i++)
cout<<list[i]<<" | ";
} //快速排序法
#include <iostream>
using namespace std; int Partition(int a[], int low, int high)
{
int x = a[high];//将输入数组的最后一个数作为主元,用它来对数组进行划分
int i = low - ;//i是最后一个小于主元的数的下标
for (int j = low; j < high; j++)//遍历下标由low到high-1的数
{
if (a[j] < x)//如果数小于主元的话就将i向前挪动一个位置,并且交换j和i所分别指向的数
{
int temp;
i++;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
//经历上面的循环之后下标为从low到i(包括i)的数就均为小于x的数了,现在将主元和i+1位置上面的数进行交换
a[high] = a[i + ];
a[i + ] = x;
return i + ;
} void QuickSort(int a[], int low, int high)
{
if (low < high)
{
int q = Partition(a, low, high);
QuickSort(a, low, q - );
QuickSort(a, q + , high);
}
} void main(){ int arry[] = {,,,,,,,,}; for (int i = ; i < ; i++)
{
cout << arry[i] <<" | ";
} cout<<endl;
QuickSort(arry, , );
for (int i = ; i < ; i++)
{
cout << arry[i] <<" | ";
}
}
c++ 排序 冒泡 插入 选择 快速的更多相关文章
- 《java数据结构与算法》系列之“简单排序"-冒泡,选择,插入
好几天又没写,因为这几天很闲,平时忙的时候自己再累都不会睡着,但是呢这没事了,照理说应该是不瞌睡了,结果还睡着了. 所以说,人很贱.也验证了一句话,没有目标的人其实最无聊.人一定要有自己的工作,这工作 ...
- Java数据结构和算法(三)--三大排序--冒泡、选择、插入排序
三大排序在我们刚开始学习编程的时候就接触过,也是刚开始工作笔试会遇到的,后续也会学习希尔.快速排序,这里顺便复习一下 冒泡排序: 步骤: 1.从首位开始,比较首位和右边的索引 2.如果当前位置比右边的 ...
- java面试准备之基础排序——冒泡与选择排序
选择排序: [java] public void select(int[] arr){ for(int i=0;i<arr.length;i++){ ...
- php六种基础算法:冒泡,选择,插入,快速,归并和希尔排序法
$arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序法 * 思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. * 比 ...
- 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)
排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...
- Java四种排序:冒泡,选择,插入,二分(折半插入)
四种排序:冒泡,选择,插入,二分(折半插入) public class Test{ // public static void main(String[] args) { // Test t=new ...
- python排序算法实现(冒泡、选择、插入)
python排序算法实现(冒泡.选择.插入) python 从小到大排序 1.冒泡排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)) ...
- python 数据结构与算法之排序(冒泡,选择,插入)
目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...
- 左神算法第一节课:复杂度、排序(冒泡、选择、插入、归并)、小和问题和逆序对问题、对数器和递归(Master公式)
第一节课 复杂度 排序(冒泡.选择.插入.归并) 小和问题和逆序对问题 对数器 递归 1. 复杂度 认识时间复杂度常数时间的操作:一个操作如果和数据量没有关系,每次都是固定时间内完成的操作,叫做常数 ...
随机推荐
- pig安装配置及实例
一.前提 1. hadoop集群环境配置好(本人hadoop版本:hadoop-2.7.3) 2. windows基础环境准备: jdk环境配置.esclipse环境配置 二.搭建pig环境 1.下载 ...
- DCGAN增强图片数据集
DCGAN增强图片数据集 1.Dependencies Python 3.6+ PyTorch 0.4.0 numpy 1.14.1, matplotlib 2.2.2, scipy 1.1.0 im ...
- 17 Resources AssetBundle资源打包
Resources在Unity中可以使用www类加载远程文件或本地文件,或是在脚本中定义字段或数组从外部拖入. 在Unity中提供了Resources类读取资源要通过Resources类读取的文件必须 ...
- DIV 透明度 设置
filter:alpha(opacity=70); -moz-opacity:0.70;-khtml-opacity: 0.70; opacity: 0.70;
- Web基础之Spring AOP与事务
Spring之AOP AOP 全程Aspect Oriented Programming,直译就是面向切面编程.和POP.OOP相似,它也是一种编程思想.OOP强调的是封装.继承.多态,也就是功能的模 ...
- 循环指令 LOOP
循环程序: 如果需要重复执行若干次同样任务.用循环执行 循环指令: LOOP <跳转标号> 用累加器的低字做循环计数器 每次执行LOOP 指令的时候,累加器的低字减去1 若减去后 非零 , ...
- 基于高德开放平台Map Lab的数据可视化
在Map Lab上创建可视化项目,首先需要添加数据.添加数据有4种方式,分别是: 上传CSV文件添加数据 上传Excel文件添加数据 连接在线数据库添加数据 提供在线数据API添加数据 一.数据上传说 ...
- 英语语法 - the + 形容词 的意义
1,表示一类人 (复数) the young 青年 the old 老年the poor 穷人 the rich 富人the sick 病人 The old need care more than ...
- 安装npm install时,长时间停留在fetchMetadata: sill 解决方法——换npm的源
安装npm install时,长时间停留在fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch处, ...
- Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in
原语句: db.carMongoDTO.aggregate({}}}, {}}}) 报错: Exceeded memory limit for $group, but didn't allow ext ...