这一周复习了数据结构与算法的顺序表和链表,了解了顺序表和链表的区别
以下是一些总结和代码:
CreatList(SqList &L,int n) 参数:顺序表L,顺序表长度n 功能:创建长度为的顺序表 时间复杂度:O(n)
InitList(SqList &L) 参数:顺序表L 功能:初始化
InsertList(SqList &L,int i,ElemType e) 参数:顺序表L,位置i,元素e 功能:位置i处插入元素e
ListDelete(SqList &L,int i) 参数:顺序表L,位置i 功能:删除位置i处元素
LocateElem(SqList L,ElemType e) 参数:顺序表L,元素e 功能:返回第一个等于e的元素的位置
PrintList(SqList L) 参数:顺序表L 功能:遍历L,并输出
SplitSort(SqList &L) 参数:顺序表L 功能:分开奇偶,并分开排序
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define range 10000
void myrandom(int a[],int len)
{
srand((unsigned) time(NULL));
for(int i=0;i<len;i++)
a[i]=rand()%range+1;
}
void InsertionSort(int a[],int len)
{
int k,current;
for(int i=1;i<len;i++)
{
k=a[i];
current=i-1;
for(;current>=0&&a[current]>k;current--)
{
a[current+1]=a[current];
}
a[current+1]=k;
}
}
int main()
{
clock_t start,end;
start=clock();
freopen("out.txt","r",stdin);
freopen("out插入排序.txt","w",stdout);
int arr[range];
int n=range;
myrandom(arr,n);
InsertionSort(arr,n);
for(int i=0;i<n;i++)
{
printf("%5d ",arr[i]);
}
cout<<endl;
end=clock();
cout<<"插入排序共计用时:"<<(float)(end-start)*1000.0/CLOCKS_PER_SEC<<"ms"<<endl;
return 0;
}
原文:https://www.cnblogs.com/jiangyiying/p/13095583.html