
-----------------------------------
CyCLoBoT
Mon Mar 24, 2003 7:12 pm

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

-----------------------------------
Blade
Mon Mar 24, 2003 8:29 pm


-----------------------------------
you would use bubble sort... it would look something like this

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
Mon Mar 24, 2003 9:48 pm


-----------------------------------
Thanks Blade this is exactly what I was looking for. I really appreciate it.  :)

-----------------------------------
Blade
Mon Mar 24, 2003 9:51 pm


-----------------------------------
no problem.. if you have any more questions... i'll try to help

-----------------------------------
Homer_simpson
Thu May 29, 2003 9:31 pm


-----------------------------------
here's a complete working function :
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
Fri May 30, 2003 8:16 am


-----------------------------------
suck ups, its sad what ppl will do for a couple of bits...

-----------------------------------
JayLo
Fri May 30, 2003 2:57 pm


-----------------------------------
err... that's why it's called a help forum eh dodge?

-----------------------------------
Homer_simpson
Fri May 30, 2003 3:38 pm


-----------------------------------
who did what for bits?!

-----------------------------------
Blade
Fri May 30, 2003 11:38 pm


-----------------------------------
this was fixed in march man... before you was born :)

-----------------------------------
Homer_simpson
Sat May 31, 2003 12:05 am


-----------------------------------
but yer sort has problems and gives wrong info if 2 numbers are the same in the array =/

-----------------------------------
vlovich
Tue Jun 03, 2003 8:28 am


-----------------------------------
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
Mon Jan 16, 2012 6:04 pm

Re: Sorting Array
-----------------------------------

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
Tue Jan 17, 2012 7:45 am

Re: Sorting Array
-----------------------------------

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.
