49 void Abc_SortMerge(
int * p1Beg,
int * p1End,
int * p2Beg,
int * p2End,
int * pOut )
51 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
53 while ( p1Beg < p1End && p2Beg < p2End )
55 if ( *p1Beg == *p2Beg )
56 *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
57 else if ( *p1Beg < *p2Beg )
62 while ( p1Beg < p1End )
64 while ( p2Beg < p2End )
66 assert( pOut - pOutBeg == nEntries );
82 int nSize = pInEnd - pInBeg;
88 if ( pInBeg[0] > pInBeg[1] )
90 pInBeg[0] ^= pInBeg[1];
91 pInBeg[1] ^= pInBeg[0];
92 pInBeg[0] ^= pInBeg[1];
97 int temp, i, j, best_i;
98 for ( i = 0; i < nSize-1; i++ )
101 for ( j = i+1; j < nSize; j++ )
102 if ( pInBeg[j] < pInBeg[best_i] )
105 pInBeg[i] = pInBeg[best_i];
106 pInBeg[best_i] = temp;
112 Abc_Sort_rec( pInBeg + nSize/2, pInEnd, pOutBeg + nSize/2 );
113 Abc_SortMerge( pInBeg, pInBeg + nSize/2, pInBeg + nSize/2, pInEnd, pOutBeg );
114 memcpy( pInBeg, pOutBeg,
sizeof(
int) * nSize );
134 pOutput = (
int *)
malloc(
sizeof(
int) * nSize );
154 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
155 int * pOutBeg = pOut;
156 while ( p1Beg < p1End && p2Beg < p2End )
158 if ( p1Beg[1] == p2Beg[1] )
159 *pOut++ = *p1Beg++, *pOut++ = *p1Beg++, *pOut++ = *p2Beg++, *pOut++ = *p2Beg++;
160 else if ( p1Beg[1] < p2Beg[1] )
161 *pOut++ = *p1Beg++, *pOut++ = *p1Beg++;
163 *pOut++ = *p2Beg++, *pOut++ = *p2Beg++;
165 while ( p1Beg < p1End )
166 *pOut++ = *p1Beg++, *pOut++ = *p1Beg++;
167 while ( p2Beg < p2End )
168 *pOut++ = *p2Beg++, *pOut++ = *p2Beg++;
169 assert( pOut - pOutBeg == nEntries );
185 int nSize = (pInEnd - pInBeg)/2;
191 if ( pInBeg[1] > pInBeg[3] )
193 pInBeg[1] ^= pInBeg[3];
194 pInBeg[3] ^= pInBeg[1];
195 pInBeg[1] ^= pInBeg[3];
196 pInBeg[0] ^= pInBeg[2];
197 pInBeg[2] ^= pInBeg[0];
198 pInBeg[0] ^= pInBeg[2];
201 else if ( nSize < 8 )
203 int temp, i, j, best_i;
204 for ( i = 0; i < nSize-1; i++ )
207 for ( j = i+1; j < nSize; j++ )
208 if ( pInBeg[2*j+1] < pInBeg[2*best_i+1] )
211 pInBeg[2*i] = pInBeg[2*best_i];
212 pInBeg[2*best_i] = temp;
213 temp = pInBeg[2*i+1];
214 pInBeg[2*i+1] = pInBeg[2*best_i+1];
215 pInBeg[2*best_i+1] = temp;
223 memcpy( pInBeg, pOutBeg,
sizeof(
int) * 2 * nSize );
240 int i, * pResult, * pInput, * pOutput;
241 pResult = (
int *)
calloc(
sizeof(
int), nSize );
244 pInput = (
int *)
malloc(
sizeof(
int) * 2 * nSize );
245 pOutput = (
int *)
malloc(
sizeof(
int) * 2 * nSize );
246 for ( i = 0; i < nSize; i++ )
247 pInput[2*i] = i, pInput[2*i+1] = pCosts[i];
249 for ( i = 0; i < nSize; i++ )
250 pResult[i] = pInput[2*i];
272 void Abc_MergeSortCostMerge(
int * p1Beg,
int * p1End,
int * p2Beg,
int * p2End,
int * pOut,
int * pCost )
274 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
275 int * pOutBeg = pOut;
276 while ( p1Beg < p1End && p2Beg < p2End )
278 if ( pCost[*p1Beg] == pCost[*p2Beg] )
279 *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
280 else if ( pCost[*p1Beg] < pCost[*p2Beg] )
285 while ( p1Beg < p1End )
287 while ( p2Beg < p2End )
289 assert( pOut - pOutBeg == nEntries );
305 int nSize = pInEnd - pInBeg;
311 if ( pCost[pInBeg[0]] > pCost[pInBeg[1]] )
313 pInBeg[0] ^= pInBeg[1];
314 pInBeg[1] ^= pInBeg[0];
315 pInBeg[0] ^= pInBeg[1];
318 else if ( nSize < 8 )
320 int temp, i, j, best_i;
321 for ( i = 0; i < nSize-1; i++ )
324 for ( j = i+1; j < nSize; j++ )
325 if ( pCost[pInBeg[j]] < pCost[pInBeg[best_i]] )
328 pInBeg[i] = pInBeg[best_i];
329 pInBeg[best_i] = temp;
337 memcpy( pInBeg, pOutBeg,
sizeof(
int) * nSize );
354 int i, * pInput, * pOutput;
355 pInput = (
int *)
malloc(
sizeof(
int) * nSize );
356 for ( i = 0; i < nSize; i++ )
360 pOutput = (
int *)
malloc(
sizeof(
int) * nSize );
384 return *pNum1 - *pNum2;
401 int i, nSize = 50000000;
402 int * pArray = (
int *)
malloc(
sizeof(
int) * nSize );
407 for ( i = 0; i < nSize; i++ )
420 for ( i = 1; i < nSize; i++ )
421 assert( pArray[pPerm[i-1]] <= pArray[pPerm[i]] );
430 for ( i = 1; i < nSize; i++ )
431 assert( pArray[i-1] <= pArray[i] );
437 qsort( (
void *)pArray, nSize,
sizeof(
int), (
int (*)(
const void *,
const void *))
Abc_SortNumCompare );
440 for ( i = 1; i < nSize; i++ )
441 assert( pArray[i-1] <= pArray[i] );
463 if ( (
unsigned)(*p1) < (
unsigned)(*p2) )
465 if ( (
unsigned)(*p1) > (
unsigned)(*p2) )
471 if ( (
unsigned)(*p1) > (
unsigned)(*p2) )
473 if ( (
unsigned)(*p1) < (
unsigned)(*p2) )
484 for ( i = 1; i < nSize; i++ )
485 assert( (
unsigned)pData[i-1] >= (
unsigned)pData[i] );
491 for ( i = 1; i < nSize; i++ )
492 assert( (
unsigned)pData[i-1] <= (
unsigned)pData[i] );
516 for ( i = 0; i < nSize-1; i++ )
519 for ( j = i+1; j < nSize; j++ )
520 if ( (
unsigned)pData[j] < (
unsigned)pData[best_i] )
528 for ( i = 0; i < nSize-1; i++ )
531 for ( j = i+1; j < nSize; j++ )
532 if ( (
unsigned)pData[j] > (
unsigned)pData[best_i] )
552 while ( (
unsigned)pData[++i] < (
unsigned)v );
553 while ( (
unsigned)v < (
unsigned)pData[--j] )
578 while ( (
unsigned)pData[++i] > (
unsigned)v );
579 while ( (
unsigned)v > (
unsigned)pData[--j] )
594 int k, i = l-1, j = r,
p = l-1, q = r;
605 while ( (
unsigned)pData[++i] < (
unsigned)v );
606 while ( (
unsigned)v < (
unsigned)pData[--j] )
612 if ( (
unsigned)pData[i] == (
unsigned)v )
614 if ( (
unsigned)v == (
unsigned)pData[j] )
619 for ( k = l; k <
p; k++, j-- )
621 for ( k = r-1; k > q; k--, i++ )
629 int k, i = l-1, j = r,
p = l-1, q = r;
640 while ( (
unsigned)pData[++i] > (
unsigned)v );
641 while ( (
unsigned)v > (
unsigned)pData[--j] )
647 if ( (
unsigned)pData[i] == (
unsigned)v )
649 if ( (
unsigned)v == (
unsigned)pData[j] )
654 for ( k = l; k <
p; k++, j-- )
656 for ( k = r-1; k > q; k--, i++ )
669 for ( i = 1; i < nSize; i++ )
670 assert( (
unsigned)pData[i-1] >= (
unsigned)pData[i] );
676 for ( i = 1; i < nSize; i++ )
677 assert( (
unsigned)pData[i-1] <= (
unsigned)pData[i] );
687 for ( i = 1; i < nSize; i++ )
688 assert( (
unsigned)pData[i-1] >= (
unsigned)pData[i] );
694 for ( i = 1; i < nSize; i++ )
695 assert( (
unsigned)pData[i-1] <= (
unsigned)pData[i] );
713 for ( i = 0; i < nSize; i++ )
714 pData[i] = ((
word)i << 32) | pCosts[i];
716 for ( i = 0; i < nSize; i++ )
717 pResult[i] = (
int)(pData[i] >> 32);
746 word * pData1, * pData2;
753 for ( i = 0; i < nSize; i++ )
754 pData2[i] = pData1[i] = ((
word)i << 32) | rand();
763 for ( i = 0; i < nSize; i++ )
764 printf(
"(%d,%d) ", (
int)(pData1[i] >> 32), (
int)pData1[i] );
774 for ( i = 0; i < nSize; i++ )
775 printf(
"(%d,%d) ", (
int)(pData2[i] >> 32), (
int)pData2[i] );
void Abc_QuickSortCostData(int *pCosts, int nSize, int fDecrease, word *pData, int *pResult)
static void Abc_SelectSortDec(word *pData, int nSize)
int * Abc_QuickSortCost(int *pCosts, int nSize, int fDecrease)
void Abc_Sort_rec(int *pInBeg, int *pInEnd, int *pOutBeg)
void Abc_QuickSort3Dec_rec(word *pData, int l, int r)
void Abc_MergeSortCost_rec(int *pInBeg, int *pInEnd, int *pOutBeg)
void Abc_QuickSort2Inc_rec(word *pData, int l, int r)
void Abc_MergeSortCostMerge(int *p1Beg, int *p1End, int *p2Beg, int *p2End, int *pOut)
void Abc_MergeSort(int *pInput, int nSize)
int Abc_SortNumCompare(int *pNum1, int *pNum2)
#define ABC_ALLOC(type, num)
static abctime Abc_Clock()
int Abc_QuickSort1CompareInc(word *p1, word *p2)
#define ABC_SWAP(Type, a, b)
static void Abc_SelectSortInc(word *pData, int nSize)
static void Abc_PrintTime(int level, const char *pStr, abctime time)
int * Abc_MergeSortCost(int *pCosts, int nSize)
ABC_NAMESPACE_IMPL_START void Abc_SortMerge(int *p1Beg, int *p1End, int *p2Beg, int *p2End, int *pOut)
DECLARATIONS ///.
void Abc_QuickSort2Dec_rec(word *pData, int l, int r)
int Abc_QuickSort1CompareDec(word *p1, word *p2)
unsigned __int64 word
DECLARATIONS ///.
#define ABC_NAMESPACE_IMPL_END
void Abc_QuickSort3Inc_rec(word *pData, int l, int r)
#define ABC_NAMESPACE_IMPL_START
void Abc_QuickSort2(word *pData, int nSize, int fDecrease)
void Abc_QuickSort3(word *pData, int nSize, int fDecrease)
void Abc_QuickSort1(word *pData, int nSize, int fDecrease)