Computer Science Canada

Sorting Array

Author:  CyCLoBoT [ 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

Author:  Blade [ Mon Mar 24, 2003 8:29 pm ]
Post 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

Author:  CyCLoBoT [ Mon Mar 24, 2003 9:48 pm ]
Post subject: 

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

Author:  Blade [ Mon Mar 24, 2003 9:51 pm ]
Post subject: 

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

Author:  Homer_simpson [ Thu May 29, 2003 9:31 pm ]
Post 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

Author:  Andy [ Fri May 30, 2003 8:16 am ]
Post subject: 

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

Author:  JayLo [ Fri May 30, 2003 2:57 pm ]
Post subject: 

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

Author:  Homer_simpson [ Fri May 30, 2003 3:38 pm ]
Post subject: 

who did what for bits?!

Author:  Blade [ Fri May 30, 2003 11:38 pm ]
Post subject: 

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

Author:  Homer_simpson [ Sat May 31, 2003 12:05 am ]
Post subject: 

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

Author:  vlovich [ Tue Jun 03, 2003 8:28 am ]
Post 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.

Author:  frankscott39 [ 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 !

Author:  Beastinonyou [ 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.


: