I have a database with approximately 200,000 items, which is sorted by username. Now, when I add an element to the end of the array and call the quick sort function to sort this array, it takes about a second to sort, which is unacceptable. Of course, there are certain optimizations that can be done. For example, if I consistently compare each line from n-1 to 0, and then move the elements, then the performance is much greater.
Another idea is that I could do a binary search from 0 to n-1, well, not distort the search, but something similar to using my already sorted array. However, I was not able to write the correct function, which will return the index into which 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.
source share