I've got a database with approximately 200 000 items, which is sorted by username. Now when I add an item to end of array and call my quick sort function to sort that array it takes almost a second to sort, which is not acceptable. There are definitely quite some optimisations that can be done. For example if I sequentially compare each string from n-1 to 0, and then move items accordingly performance is much greater.
Other idea is that I could perform binary search from 0 to n-1, well not infact search, but something similar to take advantage of my already sorted array. However I've failed to write a proper function that would return an index where my new element should be placed.
void quick_sort(int left, int right)
{
int i = left, j = right;
if (left >= right) return;
char pivotC[128];
DataEntry *tmp;
strcpy_a(pivotC, sizeof pivotC, User[(left + right) / 2]->username);
while (i <= j)
{
while (StringCompare(User[i]->username, pivotC))
i++;
while (StringCompare(pivotC, User[j]->username))
j--;
if (i <= j)
{
tmp = User[i];
User[i] = User[j];
User[j] = tmp;
i++;
j--;
}
}
if (left < j)
quick_sort(left, j);
if (i < right)
quick_sort(i, right);
}
Any help is greatly appreciated.
std::sort()? - sashoalm