排序专题2

2.1 插入排序

1:基本思想

把数插入到已排列好的数中。

 2:代码

#include <bits/stdc++.h>
using namespace std;
void input(int f,int e,int g[])
{
	for(int i=f;i<e;i++)
	cin>>g[i];
 } 
void output(int f,int e,int g[],string a)
{
	for(int i=f;i<e;i++)
	cout<<g[i]<<" ";
 } 
 void ins(int sort[],int n)
 {
 	int i,j,x;
 	for(i=1;i<n;i++)
 	{
 		x=sort[i];
 		for(j=0;j<i;j++)
 		if(x<sort[j])swap(sort[i],sort[j]);
	 }
 }
int main()
{
	int n;
	cin>>n;
	int a[n];
	input(0,n,a);
	ins(a,n);
	output(0,n,a," ");
}

2.2 快速排序

alogorithm函数库包含请使用万能头文件或者algorithm(c请使用alortithm.h)

sort函数





posted @ 2018-05-17 18:46  SMALLff  阅读(83)  评论(0编辑  收藏  举报