Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sorting Array
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CyCLoBoT




PostPosted: Mon Mar 24, 2003 7:12 pm   Post subject: Sorting Array

I have created an array that stores in x and y cordinate and it is set initially to zero. I would like to sort the array so that any cells in the array that is not zero is brought to top and the zeros come after it.

Like for example the unsorted array would be like this

Array for x-cordinate
0
0
256
0
0

Array for y-cordinate
0
0
125
0
0

I want that after sorting the array becomes like this

Array for x-cordinate
256
0
0
0
0


Array for y-cordinate
125
0
0
0
0

Any suggestions would be appreciated
Sponsor
Sponsor
Sponsor
sponsor
Blade




PostPosted: Mon Mar 24, 2003 8:29 pm   Post subject: (No subject)

you would use bubble sort... it would look something like this

code:
for k : 1 .. 10
        for i : 1 .. 10
            if (coordinate1 (k) > coordinate2(i)) then
                temp := coordinate1 (k)
                coordinate1 (k) := coordinate2 (i)
                coordinate2(i):=temp
            end if
        end for
end for


the idea is if one variable is larger than the other, they swap places

MOD Edit: Thx for helpful reply, some bonus bits your way - Tony
CyCLoBoT




PostPosted: Mon Mar 24, 2003 9:48 pm   Post subject: (No subject)

Thanks Blade this is exactly what I was looking for. I really appreciate it. Smile
Blade




PostPosted: Mon Mar 24, 2003 9:51 pm   Post subject: (No subject)

no problem.. if you have any more questions... i'll try to help
Homer_simpson




PostPosted: Thu May 29, 2003 9:31 pm   Post subject: (No subject)

here's a complete working function :
code:
procedure sort (var a : array 1 .. * of int)
    var N : int := upper (a);
    var tt := 0
    var minIndex : int
    for i : 1 .. (N - 1);
        minIndex := i;
        for j : i .. N

            if (a (j) < a (minIndex)) then
                minIndex := j;
            end if
        end for
        if (minIndex > i) then

            tt := a (i)
            a (i) := a (minIndex)
            a (minIndex) := tt
        end if
    end for
end sort
Andy




PostPosted: Fri May 30, 2003 8:16 am   Post subject: (No subject)

suck ups, its sad what ppl will do for a couple of bits...
JayLo




PostPosted: Fri May 30, 2003 2:57 pm   Post subject: (No subject)

err... that's why it's called a help forum eh dodge?
Homer_simpson




PostPosted: Fri May 30, 2003 3:38 pm   Post subject: (No subject)

who did what for bits?!
Blade




PostPosted: Fri May 30, 2003 11:38 pm   Post subject: (No subject)

this was fixed in march man... before you was born Smile
Homer_simpson




PostPosted: Sat May 31, 2003 12:05 am   Post subject: (No subject)

but yer sort has problems and gives wrong info if 2 numbers are the same in the array =/
vlovich




PostPosted: Tue Jun 03, 2003 8:28 am   Post subject: (No subject)

if your a beginner programmer, i would recommend insertion sort rather than bubble. Its pretty easy to implement and a hell of a lot faster. also if your feeling more confident, try implementing quick sort.
frankscott39




PostPosted: Mon Jan 16, 2012 6:04 pm   Post subject: Re: Sorting Array

vlovich wrote:

if your a beginner programmer, i would recommend insertion sort rather than bubble. Its pretty easy to implement and a hell of a lot faster. also if your feeling more confident, try implementing quick sort.


I would definitely accept your recommendation & suggestions !
Beastinonyou




PostPosted: Tue Jan 17, 2012 7:45 am   Post subject: Re: Sorting Array

frankscott39 @ Mon Jan 16, 2012 6:04 pm wrote:
vlovich wrote:

if your a beginner programmer, i would recommend insertion sort rather than bubble. Its pretty easy to implement and a hell of a lot faster. also if your feeling more confident, try implementing quick sort.


I would definitely accept your recommendation & suggestions !


Ok, maybe if you hop in a Time-Machine, travel back in time 8.5 years and then ask that question, and I'm sure you would get an answer.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: