排序算法.cpp
来自「数据结构上让我们编写的排序算法」· C++ 代码 · 共 55 行
CPP
55 行
// 排序算法.cpp: 主项目文件。
#include "stdafx.h"
#include<iostream>
#include<ctime>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;
int selectScroe(int *,int);
int main()
{
srand(time_t(NULL));
int length;
cout<<"请输入数列的数目"<<endl;
cin>>length;
int *p=new int[length];
for(int i=0;i<length;i++)
{
p[i]=rand()%100;
cout<<setw(4)<<p[i]<<endl;
}
selectScroe(p,length);
for(int i=0;i<length;i++)
{
cout<<setw(4)<<p[i]<<endl;
}
system("PAUSE");
return 0;
}
int selectScroe(int p[],int n)
{
double time,time1;
time=time_t(NULL);
for(int i=n-1;i>0;i--)
{
for(int j=0; j<i; j++)
{
if(p[j] > p[j+1])
{
int temp;
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
time1=time_t(NULL);
time=time1-time;
cout<<"花费的时间为"<<time<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?