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
Medium Priority
168 Views
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;
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;
end.

0
Question by:Matzekocher
• 2
• 2
• 2
• +3

LVL 50

Accepted Solution

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

ID: 8240765
he Thanks, it works now
0

LVL 10

Expert Comment

ID: 8243830
perhaps you should acces that answer then.
0

LVL 15

Expert Comment

ID: 8349891
listening...
0

LVL 1

Expert Comment

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

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

0

Expert Comment

ID: 9314353
Matzekocher:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 50

Expert Comment

ID: 9316877
I'll stick my hand up for points
0

LVL 15

Expert Comment

ID: 9324066
(always agrees with dbrunton)
0

## Featured Post

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
Course of the Month13 days, 13 hours left to enroll