// SortTest.cpp : Defines the entry point for the console application. // #include #include #include #include #include #include "string.h" #define bufferSize 20000 #define MAX_ELEMENTS 400 #define LONG_MAX 2147483647 struct myPair { short a; short b; }; long myBestResult = LONG_MAX; std::vector tempResult; std::vector bestResult; void checkResult(long currentResult) { if (currentResult myData[j1]) { totalSwaps++; a = myData[j]; b = myData[j1]; myData[j] = b; myData[j1] = a; price += CostPosition[j*N+j1]; if (CostValueType) { price += CostValue[(a-1)*N+b-1]; } myPair pair; pair.a = j+1; pair.b = j1+1; tempResult.push_back(pair); printf("%d %d\n",j+1, j1+1); } } } checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); //bubblesort optimized price = 0; totalSwaps = 0; memcpy(myData,myDataOrg, N*sizeof(short)); for (i = 0; i < N-1; i++) { for (j = 0; j < N-i-1; j++) { if(myData[j] == j+1) continue; //place ok short j1 = j; do{ j1++; }while(myData[j1] == j1+1); //first out of order if (myData[j] > myData[j1]) { totalSwaps++; a = myData[j]; b = myData[j1]; myData[j] = b; myData[j1] = a; price += CostPosition[j*N+j1]; if (CostValueType) { price += CostValue[(a-1)*N+b-1]; } myPair pair; pair.a = j+1; pair.b = j1+1; tempResult.push_back(pair); printf("%d %d\n",j+1, j1+1); } } } checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); //bubblesort reversed price = 0; totalSwaps = 0; memcpy(myData,myDataOrg, N*sizeof(short)); for (i = 0; i < N-1; i++) { for (j = N-1; j > i ; j--) { short j1 = j-1; if (myData[j] < myData[j1]) { totalSwaps++; a = myData[j]; b = myData[j1]; myData[j] = b; myData[j1] = a; price += CostPosition[j*N+j1]; if (CostValueType) { price += CostValue[(a-1)*N+b-1]; } myPair pair; pair.a = j+1; pair.b = j1+1; tempResult.push_back(pair); printf("%d %d\n",j+1, j1+1); } } } checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); //bubblesort reversed optimized price = 0; totalSwaps = 0; memcpy(myData,myDataOrg, N*sizeof(short)); for (i = 0; i < N-1; i++) { for (j = N-1; j > i ; j--) { if(myData[j] == j+1) continue; //place ok short j1 = j; do{ j1--; }while(myData[j1] == j1+1); //first out of order if (myData[j] < myData[j1]) { totalSwaps++; a = myData[j]; b = myData[j1]; myData[j] = b; myData[j1] = a; price += CostPosition[j*N+j1]; if (CostValueType) { price += CostValue[(a-1)*N+b-1]; } myPair pair; pair.a = j+1; pair.b = j1+1; tempResult.push_back(pair); printf("%d %d\n",j+1, j1+1); } } } checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); //best move price = 0; totalSwaps = 0; memcpy(myData,myDataOrg, N*sizeof(short)); bool mixed = false; do { mixed = false; int bestIdx1 = -1; int bestIdx2 = -1; long tmpBestMoveCost = LONG_MAX; for (i = 0; i < N; i++) { a = i+1; if (myData[i] == a) continue; //ok mixed = true; for(j=0; j d2) { bestIdx1 = i; bestIdx2 = j; } else if (d1 == d2) { if (bestIdx1+bestIdx2 > i+j) { bestIdx1 = i; bestIdx2 = j; } } } } } } if (mixed) { totalSwaps++; price+=tmpBestMoveCost; a = myData[bestIdx1]; b = myData[bestIdx2]; myData[bestIdx1] = b; myData[bestIdx2] = a; myPair pair; pair.a = bestIdx1+1; pair.b = bestIdx2+1; tempResult.push_back(pair); printf("%d %d\n",bestIdx1+1, bestIdx2+1); } }while(mixed); checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); //best move by min distance price = 0; totalSwaps = 0; memcpy(myData,myDataOrg, N*sizeof(short)); mixed = false; do { mixed = false; int bestIdx1 = -1; int bestIdx2 = -1; long tmpBestMoveCost = LONG_MAX; for (i = 0; i < N; i++) { a = i+1; if (myData[i] == a) continue; //ok mixed = true; for(j=0; j i+j) { bestIdx1 = i; bestIdx2 = j; } } } } } if (mixed) { totalSwaps++; a = myData[bestIdx1]; b = myData[bestIdx2]; tmpBestMoveCost = myData[bestIdx1] = b; myData[bestIdx2] = a; tmpBestMoveCost = CostPosition[bestIdx1*N+bestIdx2]; if (CostValueType) { tmpBestMoveCost += CostValue[(a-1)*N+b-1]; } price+=tmpBestMoveCost; myPair pair; pair.a = bestIdx1+1; pair.b = bestIdx2+1; tempResult.push_back(pair); printf("%d %d\n",bestIdx1+1, bestIdx2+1); } }while(mixed); checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); //best move by max distance price = 0; totalSwaps = 0; memcpy(myData,myDataOrg, N*sizeof(short)); mixed = false; do { mixed = false; int bestIdx1 = -1; int bestIdx2 = -1; long tmpBestMoveCost = 0; for (i = 0; i < N; i++) { a = i+1; if (myData[i] == a) continue; //ok mixed = true; for(j=0; jtmpBestMoveCost) { tmpBestMoveCost = moveDistance; bestIdx1 = i; bestIdx2 = j; } else if (moveDistance==tmpBestMoveCost) { if (bestIdx1+bestIdx2 < i+j) { bestIdx1 = i; bestIdx2 = j; } } } } } if (mixed) { totalSwaps++; a = myData[bestIdx1]; b = myData[bestIdx2]; tmpBestMoveCost = myData[bestIdx1] = b; myData[bestIdx2] = a; tmpBestMoveCost = CostPosition[bestIdx1*N+bestIdx2]; if (CostValueType) { tmpBestMoveCost += CostValue[(a-1)*N+b-1]; } price+=tmpBestMoveCost; myPair pair; pair.a = bestIdx1+1; pair.b = bestIdx2+1; tempResult.push_back(pair); printf("%d %d\n",bestIdx1+1, bestIdx2+1); } }while(mixed); checkResult(price); printf("Price: %d(%d)\n",price, totalSwaps); saveResult(); return true; } int _tmain(int argc, _TCHAR* argv[]) { loadInput("sorting.in"); return 0; }