void _quickSort( int* array, int start, int end, unsigned int* cost ) { if (start >= end) return; int left = start, right = end, val = array[start]; while (left < right) { while(left < right && array[right] >= val) --right; if(left < right) { (*cost)++; array[left++] = array[right]; } while(left < right && array[left] < val) ++left; if(left < right) { (*cost)++; array[right--] = array[left]; } } array[right] = val; (*cost)++; _quickSort(array, start, left-1, cost); _quickSort(array, left + 1, end, cost); return; }
/* A recursive implementation of quicksort for linked list */ void _quickSort(struct Node* l, struct Node *h) { if (h != NULL && l != h && l != h->next) { struct Node *p = partition(l, h); _quickSort(l, p->prev); _quickSort(p->next, h); } }
// The main function to sort a linked list. It mainly calls _quickSort() void quickSort(struct Node *head) { // Find last node struct Node *h = lastNode(head); // Call the recursive QuickSort _quickSort(head, h); }
unsigned int quickSort( int* array, int arraySize ) { if (arraySize < 0) return 0; unsigned int cost = 0; _quickSort(array, 0, arraySize-1, &cost); return cost; }
int32 Vector::sort(Comparator* comp) { Comparator* cUse = NULL; if (!m_uCount) return FAILURE; if (!comp && !m_comp) return FAILURE; if (comp) cUse = comp; else cUse = m_comp; _quickSort(0, m_uCount - 1, cUse); if (!m_comp) m_comp = comp; m_bSorted = true; return SUCCESS; }
void Vector::_quickSort(int32 s, int32 d, Comparator* c) { div_t pos = div(s + d, 2); int32 ret = 0; int32 i = s; int32 j = d; VectorSlot* vsX = (VectorSlot*) m_pvDB[pos.quot]; if (!vsX || (vsX && vsX->bEmpty)) return; char* strX = vsX->dbData.toString(); uint32 x_sz = vsX->dbData.getCount(); do { VectorSlot* vsI = NULL; VectorSlot* vsJ = NULL; char* strI = NULL; uint32 i_sz = 0; char* strJ = NULL; uint32 j_sz = 0; while (1) { vsI = (VectorSlot *) m_pvDB[i]; if (vsI && !vsI->bEmpty) { strI = vsI->dbData.toString(); i_sz = vsI->dbData.getCount(); ret = c->compare(strI, i_sz, strX, x_sz); if (ret >= 0) break; } i++; } while (1) { vsJ = (VectorSlot *) m_pvDB[j]; if (vsJ && !vsJ->bEmpty) { strJ = vsJ->dbData.toString(); j_sz = vsJ->dbData.getCount(); ret = c->compare(strX, x_sz, strJ, j_sz); if (ret >= 0) break; } j--; } if (i <= j) { m_pvDB.set(i, vsJ); m_pvDB.set(j, vsI); i++; j--; } } while (i <= j); if (s < j) _quickSort(s, j, c); if (i < d) _quickSort(i, d, c); }