比较排序算法的时间复杂度 c语言,c语言四种排序算法时间复杂度比较(10页)-原创力文档...
1、方案設(shè)計(jì):
我這次實(shí)驗(yàn)通過隨機(jī)生成30000個隨機(jī)數(shù),把隨機(jī)數(shù)存到數(shù)組中,用這同一組隨機(jī)數(shù)據(jù)分別進(jìn)行四種排序,直接插入排序、直接選擇排序、冒泡排序和快速排序。還通過了調(diào)用txt文件把運(yùn)算所需時間導(dǎo)出,分別輸出各個算法所需用時并對用時時長再進(jìn)行冒泡排序算出用時最短的算法。
2、程序代碼:
#include <stdio.h>
#include
#include
#include
#include
#define N 30000
void Wrong() //輸入錯誤
{
printf("\n語法錯誤,請重新輸入!\n");
getchar();
}
void Disp(int a[]) //清屏
{
int i;
system("cls");
for(i=0; i
{
if((i-1)%10==9)
printf("\n");
printf("%-7d",a[i]);
}
}
void InsertSort(int a[],int p) //直接插入排序算法
{
int i,j,temp;
for(i=1; i
{
temp=a[i];
for(j=i; j>0&&a[j-1]>temp; j--)
a[j]=a[j-1];
a[j]=temp;
}
}
void SelectSort(int a[],int p) //選擇排序算法
{
int i,j,k;
for(i=0; i
{
k=i;
for(j=i+1; j<N; j++)
if(a[j]
k=j;
if(k!=i)
{
int temp;
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}
}
void BubbleSort(int a[],int p) //冒泡排序算法
{
int i,j,temp;
for (i=0; i
{
for (j=N-1; j>i; j--) //比較,找出本趟最小關(guān)鍵字的記錄
if (a[j]<a[j-1])
{
temp=a[j]; //進(jìn)行交換,將最小關(guān)鍵字記錄前移
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
void quicksort(int a[],int n,int p) //快速排序算法
{
int i,j,low,high,temp,top=-1;
struct node
{
int low,high;
} st[N];
top++;
st[top].low=0;
st[top].high=n-1;
while(top>-1)
{
low=st[top].low;
high=st[top].high;
top--;
i=low;
j=high;
if(low
{
temp=a[low];
while(i!=j)
{
while(itemp)j--;
if(i
{
a[i]=a[j];
i++;
}
while(i
if(i
{
總結(jié)
以上是生活随笔為你收集整理的比较排序算法的时间复杂度 c语言,c语言四种排序算法时间复杂度比较(10页)-原创力文档...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言中坐标xoy轴在哪里,用C语言在l
- 下一篇: 网络语言不c,哪些网络用语不文明?