?
Solved

Problems With Bubblesort

Posted on 2003-03-31
10
Medium Priority
?
167 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
[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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 49

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
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
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 49

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The Summer 2017 Scholarship Winners have been announced!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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