Link to home
Start Free TrialLog in
Avatar of k_f_v
k_f_v

asked on

SORTING

HOW DO I SORT A LIST OF ELEMENTS USING BUBBLE SORT.
ASKER CERTIFIED SOLUTION
Avatar of shlomoy
shlomoy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vikiing
vikiing

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

Correction: the second For must be

         For J := i to n do

(not to "n-1")

Opps !!!!

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

ok ok :-)

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

Hmmm... that's pretty much what my code does :-)
OUI... :) but a little explain wouldn't hurt, would it...
:-) sure.
Though, I believe k_f_v isn't reading this at all :-(
Another DEAD_Q, Noooooooo.... :)
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...
Oh, but there is... i don't know exactly who much time will pass 'till this is autograde, but it will...
where can i find information about this autograding?

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

https://www.experts-exchange.com/Customer_Service/Experts_Exchange/Q.10151692
Avatar of k_f_v

ASKER

thankx a lot  shlomoy .i didn't disappear its just that i read my after 2-3 days. thanx again
for taking the trouble .
Thank you too.
have fun with Pascal.