Arrangement.
#1

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?
Reply
#2

http://en.wikipedia.org/wiki/Quicksort
Reply
#3

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

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;  
    }
}
Reply
#5

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);
}
Reply
#6

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

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.
Reply
#8

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?
Reply
#9

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
Reply
#10

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)
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)