Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problems With Bubblesort

Posted on 2003-03-31
10
Medium Priority
?
168 Views
Last Modified: 2010-04-16
Hello i have a problem with my Bubblesort program.
I'm a Newbie and i hope you can help me ...
here is my pascal code

program bubble;
uses crt;
const
 anz=5;

 var
  i,j,min : integer;
  puffer :integer;
  Zahlenfeld: array [1..anz] of integer;

begin
 clrscr;

  randomize;
   for i:= 1 to anz do
    begin
     Zahlenfeld[i] :=random(99)
    end;

   begin
      writeln ('das sind die zu sortierenden Zahlen');

     for i:= 1 to Anz do
      begin
       writeln (zahlenfeld[i]);
      end;
      readln;
    end;


    for j:= 1 to anz  do
    begin

       for i:=1 to anz do
       begin
        if zahlenfeld[i+1] < zahlenfeld[i] then
        end;
        puffer    := Zahlenfeld[i+1]   ;
        zahlenfeld[i+1]:= zahlenfeld[i];
        zahlenfeld[i]:= puffer;

    end;
   for i:= 1 to anz do
    begin
     writeln(zahlenfeld[i]);
    end;
    readln;
end.




0
Comment
Question by:Matzekocher
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 50

Accepted Solution

by:
dbrunton earned 80 total points
ID: 8240395
The problem is in here.

for j:= 1 to anz  do
  begin
    for i:=1 to anz do
      begin
         if zahlenfeld[i+1] < zahlenfeld[i] then
       end;
     puffer    := Zahlenfeld[i+1]   ;
     zahlenfeld[i+1]:= zahlenfeld[i];
     zahlenfeld[i]:= puffer;
  end;

might be better as

for j:= 1 to anz -1 do
  begin
    for i := j to anz - 1 do
      begin
         if zahlenfeld[i+1] < zahlenfeld[i] then
           begin
               puffer    := Zahlenfeld[i+1]   ;
               zahlenfeld[i+1]:= zahlenfeld[i];
               zahlenfeld[i]:= puffer;
         end;  
  end;

No guarantee this will work
0
 

Author Comment

by:Matzekocher
ID: 8240765
he Thanks, it works now
0
 
LVL 10

Expert Comment

by:substand
ID: 8243830
perhaps you should acces that answer then.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 15

Expert Comment

by:VGR
ID: 8349891
listening...
0
 
LVL 1

Expert Comment

by:Okey
ID: 8486849
Hey you won't get points back for a posted Question none the less if it isn't satisfying you!

So close this question by accepting an answer!

All is done or what's wrong ???

0
 
LVL 1

Expert Comment

by:Okey
ID: 8681315
Come on and look at this solution!:

program bubble;
uses crt;
const
anz=5;

var
 i,j,min : integer;
 puffer :integer;
 Zahlenfeld: array [1..anz] of integer;

begin
clrscr;

 randomize;
  for i:= 1 to anz do
   begin
    Zahlenfeld[i] :=random(99)
   end;

  begin
     writeln ('das sind die zu sortierenden Zahlen');

    for i:= 1 to Anz do
     begin
      writeln (zahlenfeld[i]);
     end;
     readln;
   end;

{Vergleiche den ersten mit den folgenden bis zum letzten und nicht weiter
 der letzte muß nicht mit nachfolgenden verglichen werden deshalb:
 (
  'J' indiziert die bisher sortierten und zeigt auf den ersten nicht sortierten
 deshalb brauch 'i' auch nicht kleiner wie 'J' sein!
 )
}

   for j:= 1 to anz-1  
    do  for i:=(J+1) to anz
           do  begin
                  if zahlenfeld[J] > zahlenfeld[i]
                   then Begin;
                              puffer          := Zahlenfeld[J];
                              zahlenfeld[J]:= zahlenfeld[i];
                              zahlenfeld[i]:= puffer;      
                          end;
  for i:= 1 to anz do
   begin
    writeln(zahlenfeld[i]);
   end;
   readln;
end.

0
 

Expert Comment

by:CleanupPing
ID: 9314353
Matzekocher:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 50

Expert Comment

by:dbrunton
ID: 9316877
I'll stick my hand up for points
0
 
LVL 15

Expert Comment

by:VGR
ID: 9324066
(always agrees with dbrunton)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
A Case Study of using the Windows API to provide RS232 communications capability in Access without the use of Active-X controls.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

581 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