Пример #1
0
// adopted from http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#C
static void QuickSortRecursive(void **data, int n, SeqItemComparator Compare, void *user_data, size_t maxterm)
{
    assert(maxterm < 1000);

    if (n < 2)
    {
        return;
    }

    void *pivot = data[n / 2];
    void **l = data;
    void **r = data + n - 1;

    while (l <= r)
    {
        while (Compare(*l, pivot, user_data) < 0)
        {
            ++l;
        }
        while (Compare(*r, pivot, user_data) > 0)
        {
            --r;
        }
        if (l <= r)
        {
            Swap(l, r);
            ++l;
            --r;
        }
    }

    QuickSortRecursive(data, r - data + 1, Compare, user_data, maxterm + 1);
    QuickSortRecursive(l, data + n - l, Compare, user_data, maxterm + 1);
}
Пример #2
0
void QuickSortRecursive(unsigned char * array, unsigned int nKey, int keyLen, int copyKeyLen) {
	unsigned char * lArray, * rArray, *bArray;	
	unsigned int nSmallerKey;
	//int i, j;
	if (nKey <= 2) {
		if (nKey == 2) {
			bArray = array + keyLen - copyKeyLen;
			rArray = bArray + keyLen;
			if (memcmp(bArray, rArray, copyKeyLen) > 0) {
				SwapMemory(bArray, rArray, copyKeyLen);
			}
		}
		return;
	}
	bArray = array + keyLen - copyKeyLen;
	lArray = bArray + keyLen ;
	rArray = bArray + keyLen * (nKey - 1);
	while (1) {
		while (memcmp(lArray, bArray, copyKeyLen) < 0) {
			lArray += keyLen;
			if (lArray > rArray) {
				break;
			}
		}
		while (memcmp(rArray, bArray, copyKeyLen) >= 0) {
			rArray -= keyLen;
			if (rArray <= bArray) {
				break;
			}
		}		
		if (lArray < rArray) {
			SwapMemory(lArray, rArray, copyKeyLen);
			lArray += keyLen;
			rArray -= keyLen;
		} else {
			break;
		}
	}

	SwapMemory(bArray, rArray, copyKeyLen);	
	nSmallerKey = (rArray - bArray)/keyLen;

	if (nSmallerKey > 1) {
		QuickSortRecursive(array, nSmallerKey, keyLen, copyKeyLen);
	}	
	if (nSmallerKey + 1 < nKey) {
		QuickSortRecursive(array + keyLen * (nSmallerKey + 1), nKey - nSmallerKey - 1, keyLen, copyKeyLen);
	}
}
Пример #3
0
void CIniEx::SortIniValues()
{
	for (int i=0;i<m_Sections.GetSize();i++)
	{
		if (!m_Keys[i]) 
			continue;

		// Quicksort
		QuickSortRecursive( i, 0, m_Keys[i]->GetUpperBound(), true );
	}
}
Пример #4
0
void CIniEx::QuickSortRecursive(int nSection, int iLow, int iHigh, bool bAscending)
{
	// Params renamed for easier comparison with literature
	int iLeft = iLow;
	int iRight = iHigh;

	// Important: Save Pivot-Element on Stack, instead of using GetAt(iPivot) in 
	// "while('compare')-Loop". Original implementation by Attila Hajdrik used 
	// GetAt(iPivot) in within the Loop
	// int iPivot = (iLow+iHigh) / 2;
	CString Pivot = m_Keys[nSection]->GetAt((iLow+iHigh) / 2); 

	do
	{
		if( bAscending )
		{
			while( CompareItems(m_Keys[nSection]->GetAt(iLeft), Pivot) < 0 ) iLeft++;
			while( CompareItems(Pivot, m_Keys[nSection]->GetAt(iRight)) < 0 ) iRight--;
		}
		else
		{
			while( CompareItems(m_Keys[nSection]->GetAt(iLeft), Pivot) > 0 ) iLeft++;
			while( CompareItems(Pivot, m_Keys[nSection]->GetAt(iRight)) > 0 ) iRight--;
		}

		if( iLeft <= iRight )
		{
			Swap(nSection, iLeft, iRight);

			iLeft++;
			iRight--;
		}
	}
	while( iLeft <= iRight );

	if( iLow < iRight )
		QuickSortRecursive(nSection, iLow, iRight, bAscending);

	if( iLeft < iHigh )
		QuickSortRecursive(nSection, iLeft, iHigh, bAscending);
}
Пример #5
0
void SeqSort(Seq *seq, SeqItemComparator Compare, void *user_data)
{
    QuickSortRecursive(seq->data, seq->length, Compare, user_data, 0);
}
Пример #6
0
void SequenceSort(Sequence *seq, SequenceItemComparator Compare)
{
    QuickSortRecursive(seq->data, seq->length, Compare, 0);
}
Пример #7
0
void QuickSort(unsigned char * array, unsigned int nKey, int keyLen) {
	QuickSortRecursive(array, nKey, keyLen, keyLen);
}