SA-MP Forums Archive
Arrangement. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Arrangement. (/showthread.php?tid=340972)



Arrangement. - nuriel8833 - 09.05.2012

How can I make an arrangement?
For ex. I have this numbers:

1 5 34 6 4 7 23 7 1 5 23

How can I arrange them from the lowest to the biggest?


AW: Arrangement. - EnzoMortelli - 09.05.2012

http://en.wikipedia.org/wiki/Quicksort


Re: AW: Arrangement. - nuriel8833 - 09.05.2012

Quote:
Originally Posted by EnzoMortelli
Посмотреть сообщение
Can I make it with loops? I didnt understand a single word in there ><


Re: Arrangement. - aco_SRBIJA - 09.05.2012

U need loop into loop

pawn Код:
stock SortInArray(array[])
{
new p=strlen(array);
for (new i=1;i!=p;i++)
    for (new j=i+1;j!=p+1;j++)
    if (array[i] > array[j])
    {
         new help;
         help = array[i];
         array[i] = array[i];
         array[j] = help;  
    }
}



Re: Arrangement. - [ABK]Antonio - 09.05.2012

You could try this out...took me a minute to write it out (with testing and all lol)

pawn Код:
stock BubbleSort(Array[], size) //at least similar
{
    new tmpslot=0, bool:swapped;
   
    redo:
    swapped = false;
    for(new index=1; index < size; index++)
    {
        if(Array[index-1] > Array[index])
        {
            tmpslot = Array[index];
            Array[index] = Array[index-1];
            Array[index-1] = tmpslot;
            swapped = true;
        }
    }
    if(swapped) goto redo;
   
    for(new index=0; index < size; index++) printf("Number %d: %d", index+1, Array[index]);
}
EDIT: this is what i used to test it

pawn Код:
new TestArray[10] = {4,3,6,2,7,1,5,9,8,10};
main()
{
    BubbleSort(TestArray, 10);
}



Re: Arrangement. - Vince - 09.05.2012

Don't ever use goto! Use do-while in this case.
pawn Код:
do
{
    // ..
}
while(swapped);



Re: Arrangement. - [ABK]Antonio - 09.05.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
Don't ever use goto! Use do-while in this case.
pawn Код:
do
{
    // ..
}
while(swapped);
lol facepalm i just realized why it wasn't working when i was using the do while


pawn Код:
stock BubbleSort(Array[], size) //at least similar
{
    new tmpslot=0, bool:swapped;


    do
    {
        swapped = false;
        for(new index=1; index < size; index++)
        {
            if(Array[index-1] > Array[index])
            {
                tmpslot = Array[index];
                Array[index] = Array[index-1];
                Array[index-1] = tmpslot;
                swapped = true;
            }
        }
    } while(swapped);
   
    for(new index=0; index < size; index++) printf("Number %d: %d", index+1, Array[index]);
}
The version with the do while instead of goto...though I don't really see a difference between the two.


Re: Arrangement. - nuriel8833 - 09.05.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
Or quicksort, an implementation of which is here:

http://y-less.pastebin.ca/1000201
Does it work with more than 2 numbers aswell?


Re: Arrangement. - [ABK]Antonio - 09.05.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
Or quicksort, an implementation of which is here:

http://y-less.pastebin.ca/1000201
Hmm, I have a question...Why does it run QSort 11 times?

Was running times just to see what I read a long time ago about Bubblesort being faster for smaller arrays


Yes, both what ****** and I posted do more than 2 numbers


Re: Arrangement. - [ABK]Antonio - 09.05.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
It is what is called a recursive algorithm. It sorts the array by sorting the low half then the high half, and for each of those halves it first sorts the low half (quarter) and the high half (quarter) etc. There are ways to explicitly code the stack and avoid function recursion, and also ways to optimise for small arrays - I've just not implemented those things yet.
Oh ok - it's an interesting function :P (at least for me to read)