AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Quicksort and copy (https://forums.alliedmods.net/showthread.php?t=194601)

Liverwiz 08-29-2012 14:33

Quicksort and copy
 
I'm trying to sort a dynamic array into a temporary dynamic array. So i can then print the sorted one without messing up the original array.

Because amxmodx uses quicksort (which is the best way) I'm also using quicksort. Now....i have as far as putting all the changed cells into the array, but the ones that don't move i'm just having a brain fart as to how i can tell if they've been moved or not. Can someone hint me towards how to tell?

This is the code that i have.
PHP Code:

quickSort(Array:inputleftright ,Array:output)
{
    new 
cell[PowersCell], tmpCell[PowersCell]
    new 
leftright
    
new pivot
    
    ArrayGetArray
(input, (j) / 2cell)
    
pivot cell[LEVEL]
    
        
// partition inupt
    
while(<= j)
    {
        
ArrayGetArray(inputicell)
        while(
cell[LEVEL] < pivot)
            
ArrayGetArray(input, ++icell)
            
        
ArrayGetArray(inputjcell)
        while(
cell[LEVEL] > pivot)
            
ArrayGetArray(input, --jcell)
            
        if(
<= j)
        {
            
ArrayGetArray(inputicell)
            clone(
celltmpCellPowersCell)
            
ArrayGetArray(inputjcell)
                
// input[i] = input[j]
            
ArraySetArray(outputicell)
            
ArraySetArray(outputjtmpCell)
            
i++
            
j++
        }
    }
    
    if(
left j)
        
quickSort(inputleftj)
    if(
right)
        
quickSort(inputiright)
}
clone(
inputoutputsize)
{
    for(new 
0sizei++)
        
output[i] = input[i]


clone is just a pretty way of copying an array into another.

Also...if i got anything wrong, please let me know.

ConnorMcLeod 08-29-2012 17:33

Re: Quicksort and copy
 
Dynamic arrays have their own sorting natives, so create a temp array, copy the default aray in it, use the sort native in the temp array.

Liverwiz 08-29-2012 18:22

Re: Quicksort and copy
 
That's what i ended up doing in the mean time till i figured this out.

I wanted to skip the first iteration of creating a temp array by sorting directly into the temp.

Backstabnoob 08-30-2012 03:31

Re: Quicksort and copy
 
Quote:

Originally Posted by maximac (Post 1786727)
FUNCTION CALLS My implementation avoids function calls. Calling (and returning from) a function is time-consuming, and not because the content of the function takes time to execute — just getting into the function, and back out of it, uses processor time.

Is it just me or are the bots getting more and more retarded?


All times are GMT -4. The time now is 05:43.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.