Forum > Beginners

Bubblesort

(1/5) > >>

Beachtante:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure  Tauschen(var a,b: integer );  Var   Ztemp : Integer;  begin                                              //Tausch Prozedur für den Sortieralgorithmus       ztemp :=  a;       a     :=  b;       b     := ztemp;  end;procedure TFrm_Bubblesort.Btn_SortierenClick(Sender: TObject); var       neul,i,limit : integer;  begin     Mmo_sort.lines.clear;       limit:= high( zahlen );          repeat         neul:=0;         for i := 1 to limit do         begin           if zahlen[ i-1 ] < zahlen[i] then              begin                   Tauschen(zahlen[i-1], zahlen[i]);                   neul := i;               end;            end;         Mmo_sort.lines.add(IntToStr(zahlen[i]));         limit:=Neul;         until limit=0 ; end; 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  :D.   

Bart:
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:
Too much time from the first bubblesort I wrote =)

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

munair:
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.

Beachtante:

--- Quote from: Munair 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.

--- End quote ---

I changed now but i didnt fixed my problem at all, it didnt show the last to numbers.

Navigation

[0] Message Index

[#] Next page

Go to full version