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
| #include<iostream> #include<climits> #include<string> using namespace std; #define inp(i,x,y) for(i=x;i<=y;i++) #define dep(i,x,y) for(i=x;i>=y;i--)
#define Status bool #define MAXSIZE 100 #define Max_time_num 4 #define KeyType int #define InfoType string #define LT(x,y) (x < y ? 1:0) typedef struct{ KeyType key; InfoType info; }ElemType; typedef struct{ ElemType r[MAXSIZE+1]; int length; }SqList;
typedef struct{ KeyType key; int next; }SLNode; typedef struct{ SLNode r[MAXSIZE+1]; int length; }SLinkList;
Status InsertSort(SqList L);
Status BInsertSort(SqList L);
Status ListInsertSort(SLinkList &SL); Status Arrange(SLinkList &SL);
Status ShellInsert(SqList &L, int dk); Status ShellSort(SqList L,int* dlta,int t); int main() { int i = 0, j = 0;
SqList L; cin >> L.length; inp(i,1,L.length) cin >> L.r[i].key;
InsertSort(L); BInsertSort(L); int* dlta = new int[Max_time_num]; inp(i, 0, Max_time_num) if(i<=2) dlta[i] = i+1; else if(i==3) dlta[i] = 5; else dlta[i] = 9; ShellSort(L, dlta, 3); SLinkList SL; cin >> SL.length; inp(i,1,SL.length){ cin >> SL.r[i].key; } SL.r[i].key = INT_MAX; if(ListInsertSort(SL)){ cout << "List InsertSort Result:\n"; inp(i,0,SL.length){ cout << "<" << SL.r[i].key << ", " << SL.r[i].next << "> "; } cout << endl; } cout << "Rearrange Records Result:\n"; if(Arrange(SL)){ inp(i,1,SL.length){ cout << "<" << SL.r[i].key << ", " << SL.r[i].next << "> "; } cout << endl; } delete[] dlta; return 0; } Status InsertSort(SqList L) { int i = 0, j = 0; inp(i, 2, L.length){ if(LT(L.r[i].key, L.r[i-1].key)){ L.r[0] = L.r[i]; L.r[i] = L.r[i-1]; for(j=i-2;LT(L.r[0].key, L.r[j].key);j--) L.r[j+1] = L.r[j]; L.r[j+1] = L.r[0]; } } cout << "InsertSort Result:\n"; inp(i,1,L.length) cout << L.r[i].key << " "; cout << endl; return true; } Status BInsertSort(SqList L) { int i = 0, j = 0; inp(i, 2, L.length){ L.r[0] = L.r[i]; int low = 1, high = i - 1; while(low <= high){ int m = (low + high) / 2; if(LT(L.r[0].key, L.r[m].key)) high = m-1; else low = m + 1; } dep(j,i-1,high+1) L.r[j+1] = L.r[j]; L.r[high + 1] = L.r[0];
} cout << "Binary InsertSort Result:\n"; inp(i,1,L.length) cout << L.r[i].key << " "; cout << endl; return true; } Status ListInsertSort(SLinkList &SL) { SL.r[0].next = 1;SL.r[1].next = 0; int i=0,j=0,k=0; inp(i, 2, SL.length){ j = 0; k = SL.r[j].next; while(k > 0 && LT(SL.r[k].key, SL.r[i].key)){ j = k;k = SL.r[j].next; } SL.r[i].next = SL.r[j].next; SL.r[j].next = i; } return true; } Status Arrange(SLinkList &SL) { int i=0, j=0; int p = SL.r[0].next; inp(i, 1, SL.length-1){ while(p < i) p = SL.r[p].next; int q = SL.r[p].next; if(p != i){ SLNode temp = SL.r[p]; SL.r[p] = SL.r[i]; SL.r[i] = temp; SL.r[i].next = p; } p = q; } return true; } Status ShellSort(SqList L, int* dlta, int t) { int i = 0; inp(i,0,t-1){ ShellInsert(L, dlta[i]); } cout << "Shell InsertSort Result:\n"; inp(i,1,L.length) cout << L.r[i].key << " "; cout << endl; return true; } Status ShellInsert(SqList &L, int dk) { int i = 0, j = 0; inp(i,dk+1,L.length){ if(LT(L.r[i].key, L.r[i-dk].key)){ L.r[0] = L.r[i]; for(j=i-dk;j>0 && LT(L.r[0].key, L.r[j].key); j-=dk) L.r[j+dk] = L.r[j]; L.r[j+dk] = L.r[0]; } } return true; }
|