I have not done the copy from list to array code with this one because I did not have your list code which did enough elements. Probably won't bother at this point. I've already spent more time on this than I had planned. Yes, quickstort does not produce a stable sort.
Here's the quicksort code itself. I spent about 10 minutes testing this to see what using a very short recursive algorithm would do. This assumes the input is stored as "AX" ints on the module (for X = 0 ... N-1).
object oModule = GetModule();
int partition(int l, int r) {
int pivot, i, j, t;
pivot = GetLocalInt(oModule, "A" + IntToString(l));
i = l; j = r+1;
while( 1) {
do ++i; while( GetLocalInt(oModule, "A" + IntToString(i)) >= pivot && i <= r );
do --j; while( GetLocalInt(oModule, "A" + IntToString(j)) < pivot );
if( i >= j ) break;
t = GetLocalInt(oModule, "A" + IntToString(i));
SetLocalInt(oModule, "A" + IntToString(i), GetLocalInt(oModule, "A" + IntToString(j)));
SetLocalInt(oModule, "A" + IntToString(j),t);
}
t = GetLocalInt(oModule, "A" + IntToString(l));
SetLocalInt(oModule, "A" + IntToString(l), GetLocalInt(oModule, "A" + IntToString(j)));
SetLocalInt(oModule, "A" + IntToString(j), t);
return j;
}
void quickSort(int l, int r) {
int j;
if( l < r ) {
j = partition(l, r);
quickSort(l, j-1);
quickSort(j+1, r);
}
}
I thought you were using a basic insertion sort. If you got an NlogN variant then that explains a lot (isn't that a shell sort?). Plus you've tailored the algorithm to your data structure and vice-versa. How does your algorithm work if you do use the actual interfaces to the data structure instead of directly accessing the internal structure?
One thing you might try just for grins is using shifts instead of *2 and /2. Depending on how nwnscript implements those math operations it may be shorted to use <<1 and >>1 instead. Or it might not. With a real compiler with optimization it won't matter, but in this case it might.
Anyway, I concede ![]()





Retour en haut






