SORTING

HOW DO I SORT A LIST OF ELEMENTS USING BUBBLE SORT.
k_f_vAsked:
Who is Participating?
 
shlomoyConnect With a Mentor Commented:

                     For I := 1 to n - 1 do
                     Begin
                        For J := i to n - 1 do
                           If X[j] > X[j+1] then
                              Begin
                                 T := X[j];
                                 X[j] := X[j+1];
                                 X[j+1] := T
                              end
                     end

of course, you'll need to declare all the variables here.

0
 
vikiingCommented:
>>       For J := i to n - 1 do

Correction: the second For must be

         For J := i to n do

(not to "n-1")

0
 
vikiingCommented:
Opps !!!!

I'm now seeing the algorithm is slightly different from the one I use. Sorry; I said nothing...

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
shlomoyCommented:
ok ok :-)

0
 
My name is MudSystems EngineerCommented:
BUBBLE SORT is the MOST-SLOW sort procedure there is... it compares each and every number to the next one and goes up to the end, then starts again but this time doesn't start at the begining, insted starts at the BEGIN+1 .. BEGIN+2 an so on...

Like Victor said, it's the way you do the code...

First you have to had two index, to point to the two numbers of the array [a bunch of numbers that you wanna sort] like...

N1 .. N2 .. N3 .. N4 .. N5 .. N6 .. N7 .. N8

Then lets do it like this... INDEX1 will go from MINNUM+1 to MAXNUM and INDEX2 will go from MAXNUM downto INDEX1, so you use INDEX1 to remeber from where to where you allready did the comparations of the numbers and INDEX2 to do the comparations numbers

For Index1:=MinNum+1 To MaxNum Do
  For Index2:=MaxNum DownTo Index1 Do
    If MyArray[Index2-1] > MyArray[Index2] Then
      ChangePlaces(MyArray[Index2],MyArray[Index2-1]);

So for the sorting of the numbers up there [N1 .. N8]: Index1 points to N2 and Index2 points to N8…

Index1     Index2   Comparation
  N2            N8        If N7 > N8 then change
  N2            N7        If N6 > N7 then change
  N2            N6        If N5 > N6 then change
  N2            N5        If N4 > N5 then change
  N2            N4        If N3 > N4 then change
  N2            N3        If N2 > N3 then change
  N2            N2        If N1 > N2 then change
At this moment we reach the "bottom" set it at MINNUM+1 or N2 or Index1, you see that every number had been compared with the next closest one from up-to-down-comparation… then we set the bottom to N3 or we just increment Index1, and Ïndex2 we set it to the top

  N3            N8        If N7 > N8 then change
  N3            N7        If N6 > N7 then change
  N3            N6        If N5 > N6 then change
  N3            N5        If N4 > N5 then change
  N3            N4        If N3 > N4 then change
  N3            N3        If N2 > N3 then change
Again at this moment we reach the bottom set it at N3 and we did the comparations and again we set the Index1 to the what it was + 1 (we increase the bottom) and again we set the Index2 to the top…

  N4            N8        If N7 > N8 then change
    And so on until we reach
  N8            N8        If N7 > N8 then change

0
 
shlomoyCommented:
Hmmm... that's pretty much what my code does :-)
0
 
My name is MudSystems EngineerCommented:
OUI... :) but a little explain wouldn't hurt, would it...
0
 
shlomoyCommented:
:-) sure.
Though, I believe k_f_v isn't reading this at all :-(
0
 
My name is MudSystems EngineerCommented:
Another DEAD_Q, Noooooooo.... :)
0
 
shlomoyCommented:
I wonder why isn't there an automatic point-granter thread to grant the Experts the points if the question asker disappeares after some time...
0
 
My name is MudSystems EngineerCommented:
Oh, but there is... i don't know exactly who much time will pass 'till this is autograde, but it will...
0
 
shlomoyCommented:
where can i find information about this autograding?

0
 
My name is MudSystems EngineerCommented:
You can post a question in the CS section, or you can take a look at a question i did in there about the same thing...

http://www.experts-exchange.com/Customer_Service/Experts_Exchange/Q.10151692
0
 
k_f_vAuthor Commented:
thankx a lot  shlomoy .i didn't disappear its just that i read my after 2-3 days. thanx again
for taking the trouble .
0
 
shlomoyCommented:
Thank you too.
have fun with Pascal.
0
All Courses

From novice to tech pro — start learning today.