?
Solved

SORTING

Posted on 1999-07-24
15
Medium Priority
?
274 Views
Last Modified: 2010-04-16
HOW DO I SORT A LIST OF ELEMENTS USING BUBBLE SORT.
0
Comment
Question by:k_f_v
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 8

Accepted Solution

by:
shlomoy earned 300 total points
ID: 1218652

                     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
 
LVL 3

Expert Comment

by:vikiing
ID: 1218653
>>       For J := i to n - 1 do

Correction: the second For must be

         For J := i to n do

(not to "n-1")

0
 
LVL 3

Expert Comment

by:vikiing
ID: 1218654
Opps !!!!

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

0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
LVL 8

Expert Comment

by:shlomoy
ID: 1218655
ok ok :-)

0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 1218656
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
 
LVL 8

Expert Comment

by:shlomoy
ID: 1218657
Hmmm... that's pretty much what my code does :-)
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 1218658
OUI... :) but a little explain wouldn't hurt, would it...
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1218659
:-) sure.
Though, I believe k_f_v isn't reading this at all :-(
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 1218660
Another DEAD_Q, Noooooooo.... :)
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1218661
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
 
LVL 6

Expert Comment

by:My name is Mud
ID: 1218662
Oh, but there is... i don't know exactly who much time will pass 'till this is autograde, but it will...
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 1218663
where can i find information about this autograding?

0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 1218664
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
 

Author Comment

by:k_f_v
ID: 1218665
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
 
LVL 8

Expert Comment

by:shlomoy
ID: 1218666
Thank you too.
have fun with Pascal.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read on to get a few ideas on how to promote your next corporate event.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question