Efficient Bubble Sort Program
Bubble Sort is the one of the sorting algorithm to sort an array. The efficient bubble sort algorithm is given below as Pascal Program.
program Project1; {procedure to swap two values} Procedure swap(var x:integer;var y:integer) ; var temp: integer; begin temp:=x; x:=y; y:=temp; end; {procedure to print a given array} Procedure printArr(arr: Array of Integer) ; var i: integer; begin for i:=0 to Length(arr)-1 do begin writeln(arr[i]); end; end; {procedure to bubble sort the given array} Procedure BubbleSort(var arr: Array of Integer); var done: boolean; p,t,last:integer; begin done:=False; t:=Length(arr)-1; while ((done=False)) do begin P:=0; done:=True; last:=t; while (p <last) do begin if (arr[p] > arr[p+1]) then begin swap(arr[p],arr[p+1]); t:=p; done:=False; end; p:=p+1; end; end; end; {main Program} var n: array [0..10] of integer; begin n[0]:=1; n[1]:=2; n[2]:=3; n[3]:=10; n[4]:=20; n[5]:=25; n[6]:=50; n[7]:=60; n[8]:=70; n[9]:=80; n[10]:=100 ; BubbleSort(n); printArr(n); readln; end.
No comments:
Post a Comment