### Author Topic: Bubblesort  (Read 2906 times)

#### Beachtante

• New member
• Posts: 6
##### Bubblesort
« on: June 20, 2018, 07:02:41 pm »
Code: Pascal  [Select]
1. procedure  Tauschen(var a,b: integer );
2.
3.  Var   Ztemp : Integer;
4.
5.  begin                                              //Tausch Prozedur für den Sortieralgorithmus
6.        ztemp :=  a;
7.        a     :=  b;
8.        b     := ztemp;
9.
10.  end;
11. procedure TFrm_Bubblesort.Btn_SortierenClick(Sender: TObject);
12.
13. var       neul,i,limit : integer;
14.
15.
16. begin
17.      Mmo_sort.lines.clear;
18.        limit:= high( zahlen );
19.
20.          repeat
21.          neul:=0;
22.          for i := 1 to limit do
23.          begin
24.            if zahlen[ i-1 ] < zahlen[i] then
25.               begin
26.                    Tauschen(zahlen[i-1], zahlen[i]);
27.                    neul := i;
28.
29.               end;
30.
31.            end;
33.          limit:=Neul;
34.          until limit=0 ;
35.
36. end;
37.
Hey there !
Ive got the fowllowing problem. I have to make a programm( in school ) wich sorts numbers in the algorithm of Bubblesort.
I already got almost everything done. But in the output it doesnt show mostly the first and the last number.
My teacher said that the problem is hanging somewhere in the index. But now im at the point that i cant do it by myself.
Because i am sitting at this already about like 4-6 houers to get this done.
So if somebody have time to help me .I would be very Thankfull.

ps the code is attached and the
names are in german so if dont know something then ask.
Thats because im from germany so that would excuse my english  .
« Last Edit: June 20, 2018, 07:43:44 pm by Beachtante »

#### Bart

• Hero Member
• Posts: 3308
##### Re: Bubblesort
« Reply #1 on: June 20, 2018, 07:21:56 pm »
Please simply put the code inside [ code ] [ /code ] tags (without the spaces, I put them there to fool de forum software). You can use the button with # on it, right above the field where you type your message.

Bart

#### Lainz

• Hero Member
• Posts: 2976
##### Re: Bubblesort
« Reply #2 on: June 20, 2018, 07:28:35 pm »
Too much time from the first bubblesort I wrote =)

If there's a problem about a lost item, check the boundaries or indexes..

#### Munair

• Sr. Member
• Posts: 368
• Keep it simple.
##### Re: Bubblesort
« Reply #3 on: June 20, 2018, 07:29:42 pm »
In your first condition it should be i - 1 instead of i + 1. Same goes for the swap. Have a look at this wiki for a good example: http://wiki.freepascal.org/Bubble_sort It seems you're almost there.
« Last Edit: June 20, 2018, 07:32:02 pm by Munair »
Lazarus 1.8.4; Manjaro Linux; Windows 10 x64; Windows XP; PC-DOS 2000

#### Beachtante

• New member
• Posts: 6
##### Re: Bubblesort
« Reply #4 on: June 20, 2018, 07:34:16 pm »
In your first condition it should be i - 1 instead of i + 1. Same goes for the swap. Have a look at this wiki for a good example: http://wiki.freepascal.org/Bubble_sort It seems you're almost there.

I changed now but i didnt fixed my problem at all, it didnt show the last to numbers.
« Last Edit: June 20, 2018, 07:37:00 pm by Beachtante »

#### Beachtante

• New member
• Posts: 6
##### Re: Bubblesort
« Reply #5 on: June 20, 2018, 07:35:48 pm »
Too much time from the first bubblesort I wrote =)

If there's a problem about a lost item, check the boundaries or indexes..

i checked them but i dont know if its right or false xD because I am not an expert im a beginner i only know basics

#### Beachtante

• New member
• Posts: 6
##### Re: Bubblesort
« Reply #6 on: June 20, 2018, 07:36:13 pm »
Please simply put the code inside [ code ] [ /code ] tags (without the spaces, I put them there to fool de forum software). You can use the button with # on it, right above the field where you type your message.

Bart

Thanks for the information

#### Bart

• Hero Member
• Posts: 3308
##### Re: Bubblesort
« Reply #7 on: June 20, 2018, 07:37:24 pm »
Code: Pascal  [Select]
1.   if zahlen[ i+1 ] < zahlen[i]

What happens if i = limit?
You then access zahlen[limit+1].
I don't think you want to do that.

Bart

#### Beachtante

• New member
• Posts: 6
##### Re: Bubblesort
« Reply #8 on: June 20, 2018, 07:42:43 pm »
Code: Pascal  [Select]
1.   if zahlen[ i+1 ] < zahlen[i]

What happens if i = limit?
You then access zahlen[limit+1].
I don't think you want to do that.

Bart

idk what you mean, ive got it already changed

#### Bart

• Hero Member
• Posts: 3308
##### Re: Bubblesort
« Reply #9 on: June 20, 2018, 07:43:50 pm »
Give an array with boundaries Low..High you basically do

Code: Pascal  [Select]
1. repeat
2.   for i := Low to High-1 do
3.   begin
4.     set some flag that indicates a swap was needed to false
5.     if array[i] > array[i+1] then swap these and set the flag that a swap was needed to true
6.   end;
7. until no swap was needed

Unrelated:
I would suggest to make the procedure that swaps a nested procedure of the sorting procedure and make the parameter indices to the array.
That will make your teacher happy.

Bart

#### Munair

• Sr. Member
• Posts: 368
• Keep it simple.
##### Re: Bubblesort
« Reply #10 on: June 20, 2018, 07:48:43 pm »
Take a good look at the example I gave you. There is another error in your code.
Lazarus 1.8.4; Manjaro Linux; Windows 10 x64; Windows XP; PC-DOS 2000

• Hero Member
• Posts: 7420
##### Re: Bubblesort
« Reply #11 on: June 20, 2018, 08:28:46 pm »
Take a good look at the example I gave you. There is another error in your code.
Maybe this helps to obfuscate < >
Code: Pascal  [Select]
1. program champagnesort;
2. {\$mode objfpc}
3. function BubblesSort(var a: array of integer ):boolean;
4. var
5.   finished, i:integer;
6. begin
7.   Result := High( a ) <> 0;
8.   repeat
9.     finished := 0;
10.     for i := Succ(low(a)) to High(a) do
11.       begin
12.         if a[ Pred(i) ] > a[ i ] then // xor swap
13.           begin
14.             a[Pred(i)] := a[Pred(i)] xor a[i];
15.             a[i] := a[Pred(i)] xor a[i];
16.             a[Pred(i)] := a[Pred(i)] xor a[i];
17.             finished := i ;
18.           end;
19.       end ;
20.   until Result = (finished = 0);
21. end;
22.
23. var
24.   a:array of integer;
25.   v:integer;
26. begin
27.   a := [123123,12,1,55,6686,5,1555,666];
28.   Bubblessort(a);
29.   for v in a do writeln(v);
30. end.
Modern pascal. Adapted from our wiki. Programming with xor swap, saves temp..
« Last Edit: June 20, 2018, 08:45:18 pm by Thaddy »

#### Bart

• Hero Member
• Posts: 3308
##### Re: Bubblesort
« Reply #12 on: June 20, 2018, 08:35:06 pm »
Maybe this helps to obfuscate < >

LOL

Bart

• Hero Member
• Posts: 7420
##### Re: Bubblesort
« Reply #13 on: June 20, 2018, 08:36:45 pm »
I removed another temp. We just crossed... And made it slightly more useful in a thread (by making it a Boolean function). Rather proud of this...
If this was homework, nobody is going to believe Beachtante..... Oh, and of course, it works....
« Last Edit: June 20, 2018, 08:59:38 pm by Thaddy »