Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Sorting Array
Author Message
CyCLoBoT

Posted: 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

Posted: 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

CyCLoBoT

Posted: 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.

Posted: 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

Posted: 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

Posted: 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

Posted: 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

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

who did what for bits?!

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

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

Posted: 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

Posted: 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

Posted: 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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 13 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: