输入、输出:有穷,确定,可行
算法效率的度量方法:
以最高阶 去除其余加项,去除系数
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2^) < O(n^3^) < O(2^n^)<O(n!)<O(n^n^)

线性表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| #include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 typedef int ElemType; ElemType *newbase;
typedef struct{ ElemType *elem; int length; int ListSize; }SqList;
int InitSqList(SqList &L){ L.elem=(ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE); if(!L.elem) exit(OVERFLOW); L.length=0; L.ListSize=LIST_INIT_SIZE; return OK; }
void CreatSqList(SqList &L){ int i,n; cout<<"请输入顺序表的元素个数:"; cin>>n; for(i=0;i<n;i++){ cout<<"请输入第 "<<(i+1)<<" 个元素:"; cin>>L.elem[i]; L.length++; } }
void ShowSqList(SqList &L){ cout<<endl; for(int i=0;i<L.length;i++) cout<<L.elem[i]<<" "; cout<<endl; }
int InsertSqList(SqList &L,int pos,ElemType elem){ if((pos-1)<0&&pos>L.length+1){ cout<<"您所插入的位置不合法!"<<endl; return ERROR; } if(L.length>=L.ListSize){
newbase=(ElemType *)realloc(L.elem,(L.ListSize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); L.elem=newbase; L.ListSize+=LISTINCREMENT; } for(int i=L.length-1;i>=pos-1;i--){ L.elem[i+1]=L.elem[i]; } L.elem[pos-1]=elem; L.length++; return OK; }
int DeleteSqList(SqList &L,int pos){ if(pos<1||pos>L.length){ cout<<"删除的位置不合法!"<<endl; return ERROR; } for(int i=pos-1;i<L.length;i++){ L.elem[i]=L.elem[i+1]; } L.length--; return OK; }
int UpdateSqList(SqList &L,int pos,ElemType elem){ if(pos<1&&pos>L.length){ cout<<"修改的位置不合法!"<<endl; return ERROR; } L.elem[pos-1]=elem; return 0; }
void CombineSqList(SqList &La,SqList &Lb){ int i,j; for(i=0;i<Lb.length;i++){ int cout=0; for(j=0;j<La.length;j++){ if(La.elem[j]==Lb.elem[j]) cout++; } if(cout==0) La.elem[La.length++]=Lb.elem[i]; } }
void CombineSq(SqList &LA,SqList &LB,SqList &LC){ ElemType *pa,*pb,*pc,*pa_last,*pb_last; LC.length=LA.length+LB.length; LC.elem=new ElemType[LC.length]; pc=LC.elem; pa=LA.elem; pb=LB.elem; pa_last=LA.elem+LA.length-1; pb_last=LB.elem+LB.length-1; while((pa<=pa_last)&&(pb<=pb_last)){ if(*pa<=*pb) *pc++=*pa++; else *pc++=*pb++; } while(pa<=pa_last) *pc++=*pa++; while(pb<=pb_last) *pc++=*pb++; } int main(){ SqList L; InitSqList(L); CreatSqList(L); ShowSqList(L); int num=0; cout<<endl<<"1、插入"<<endl; cout<<"2、删除"<<endl; cout<<"3、修改"<<endl; cout<<"4、顺序表普通合并"<<endl; cout<<"5、顺序表有序合并"<<endl<<endl; cout<<"请选择需要进行的操作:"; cin>>num; if(num<1||num>5){ cout<<"你选择的操作不存在,请重新输入:"; cin>>num; } switch(num){ case 1:{ int m,n; cout<<"请输入你要插入的位置:"; cin>>m; cout<<"请输入你要插入的元素:"; cin>>n; InsertSqList(L,m,n); ShowSqList(L); break; } case 2:{ int m; cout<<"请输入你要删除的位置:"; cin>>m; DeleteSqList(L,m); ShowSqList(L); break; } case 3:{ int m,n; cout<<"请输入你要修改的位置:"; cin>>m; cout<<"请输入你要修改的元素:"; cin>>n; UpdateSqList(L,m,n); ShowSqList(L); break; } case 4:{ SqList Lb; InitSqList(Lb); cout<<"请创建你要合并的顺序表Lb:"<<endl; int n; cout<<"请输入Lb的元素个数:"; cin>>n; cout<<"你所输入的"<<n<<"个元素分别为:"; for(int i=0;i<n;i++){ cin>>Lb.elem[i]; Lb.length++; } CombineSqList(L,Lb); cout<<"合并后:"; ShowSqList(L); break; } case 5:{ SqList Lb,Lc; InitSqList(Lb); InitSqList(Lc); cout<<"请创建你要合并的顺序表Lb:"<<endl; int n; cout<<"请输入Lb的元素个数:"; cin>>n; cout<<"你所输入的"<<n<<"个元素分别为:"; for(int i=0;i<n;i++){ cin>>Lb.elem[i]; Lb.length++; } CombineSq(L,Lb,Lc); cout<<"合并后:"; ShowSqList(Lc); break; } } return 0; }
|