Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Bubble sorting arrays

Name: Bubble Sorting Arrays ! Description:This example is documented ! and shows how to sort array elements eg ! in assending/dessending order. rate my c ! ode ! By: Jason Myerscough ! ! Returns:if returns numbers in assendin ! g order ! !This code is copyrighted and has ! limited warranties.Please see http://w ! ww.1DelphiStreet.com/vb/scripts/ShowCode ! .asp?txtCodeId=724&lngWId=7 !for details. !************************************** //////////////////////////////////////////////////////////////////////////////// ///////////////////(* Bubble Sorting Arrays - By Jason M. *)//////////////////// //////////////////////////////////////////////////////////////////////////////// (* This example was written at as a console app in delphi 6 if you want to use it in Turbo Pascal All you have to do is delete {$APPTYPE CONSOLE} and underneath Uses change SysUtils to Crt *) program Bubble; {$APPTYPE CONSOLE} uses SysUtils; Var Ary : array[1..10] of byte; InnerLoop : integer; Outerloop : integer; temp : integer; {The reason I use this temp is so I can swap the value in the array over. If I had the following: x := y; y := x; y will just wont get y because you assigned y to x and variables can only hold one Value :) So by using the Temp variable I can swap them over temp := x; x := y; y := temp;} begin randomize; for innerloop := 1 to 10 do begin ary[innerloop] := Random(100); writeln(ary[innerloop]); end; {assign some numbers to the array} {start sorting the array} for outerloop := 1 to 10 do begin for innerloop := outerloop to 10 do begin if ary[outerloop] > ary[innerloop] {To displa in decending order change > to <} then begin temp := ary[outerloop]; {enables me to swap the values over} ary[outerloop] := ary[innerloop]; {make the lowest value higher up in the the array} ary[innerloop] := temp; end;{end IF} end; {end inner loop} end;{end outer loop} writeln; writeln('press ENTER to view data in asscending order'); readln; for innerloop := 1 to 10 do begin writeln(ary[innerloop]); end; readln; end.