#include <iostream>
#include <time.h>
using namespace std;
#define max 1000
//论文Bidirectional Conditional Insertion Sort algorithm; An efficient
//progress on the classical insertion sort提到的算法
//减去了作者优化代码的部分
void SWAP(int* A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
int ISEQUAL(int* A, int SL, int SR) {
for (int k = SL + 1; k <= SR - 1; k++) {
if (A[k] != A[SL]) {
SWAP(A, k, SL);
return k;
}
}
return -1;
}
void INSRIGHT(int* A, int CurrItem, int SR, int right) {
int j = SR;
while (j <= right && CurrItem > A[j]) {
A[j - 1] = A[j];
j++;
}
A[j - 1] = CurrItem;
}
void INSLEFT(int* A, int CurrItem, int SL, int left) {
int j = SL;
while (j >= left && CurrItem < A[j]) {
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = CurrItem;
}
int main() {
clock_t start, end;
int A[max] = { 5,4,2,1,0,9,3,8,7,6 };
int n = 10;
//int n;
//cout << "请输入要排序的数据规模:" << endl;
//cin >> n;
start = clock();
int SL = 0;
int SR = n - 1;
int i, LC, RC, CurrItem;
for (int p = 0; p < 1000000000; p++) {
while (SL < SR) {
if (A[SL] == A[SR]) {
if (ISEQUAL(A, SL, SR) == -1) {
return 0;
}
}
if (A[SL] > A[SR]) {
SWAP(A, SL, SR);
}
i = SL + 1;
LC = A[SL];
RC = A[SR];
while (i < SR) {
CurrItem = A[i];
if (CurrItem >= RC) {
A[i] = A[SR - 1];
INSRIGHT(A, CurrItem, SR, n - 1);
SR = SR - 1;
}
else if (CurrItem <= LC) {
A[i] = A[SL + 1];
INSLEFT(A, CurrItem, SL, 0);
SL = SL + 1;
i = i + 1;
}
else {
i = i + 1;
}
}
SL = SL + 1;
SR = SR - 1;
}
}
end = clock();
for (int i = 0; i < 10; i++) {
cout << A[i] << endl;
}
cout << double(double(end) - double(start)) / CLOCKS_PER_SEC << "s" << endl;
}
BCIS算法
最新推荐文章于 2024-07-20 22:49:13 发布