A.brute force sort:
by repeatedly finding the smallest(largest) of the
unsorted sublist and swap it with the checking digit, let see a sample
of brute force sort:
I wanted to place the number in ascending order
original five number in array: 7 9 8 1 3,
after first searching,we find the smallest number
is 1,
then,we swap it with the first number:
after 1st search:
1 9 8 7 3
then continue with searching,
we find 3 is smallest in 4 arrays left, then:
after 2nd search:
1 3 8 7 9
then continue with searching,
we find 7 is smallest in 3 arrays left ,then
after 3nd search:
1 3 7 8 9,
sorting complete,the number is arranged!!,
here is the Brute force sort program:
sample 1:
var
data:array[1..5] of integer;
j,k,n:integer;{for searching counter}
index,small:integer;{for temp store the smallest
number}
found:boolean;
procedure Swap(var x,y:integer);
var temp:integer;
{in swaping,a empty/dummy "var" used for two number
interchange}
begin
temp:=x;
x:=y;
y:=temp;
end;
begin {main}
data[1]:7;data[2]:=9;data[3]:=8;data[4]:=1;data[5]:=3;
{sorting below\/}
for k:=1 to N - 1 do
begin
small :=a[k];
Found := false;
for j:= k + 1 to N
do
if A[J]<
small then
begin
Small:=a[J];
index:=J;
Found :=true;
end;
if Found
then
swap(A[index],A[K]);
end;
{output}
writeln(data[1],' ',data[2],' ',data[3],'
',data[4],' ',data[5]);
end.
B.Bubble sort:
sort by repeatedly comparing(searching)
data in arrays,
in each pass,the left most unchecked
digit will be go toward
then the smallest to the left(right),the
bigest to the right(left)
here is a number sample of bubble sort:
first past:
5 3 1 7 2 (after 1st searching,we
found that 5>3,then swap 5 and 3)
3 5 1 7 2 (continue searching,we
found that 5>1,then swap 5 and 1)
3 1 5 7 2 (continue after
searching,we found that no swap required)
3 1 5 2 7 (continue searching,we
found that 7>2,then swap them)
second pass
3 1 5 2 7 (after 1st searching,we
found that 3>1,then swap 3 and 1)
1 3 5 2 7 (continue searching,we
found that no swap required)
1 3 5 2 7 (continue searching,we
found that 5>2,then swap 5 and 2)
1 3 2 5 7 (this pass clear)
third pass
1 3 2 5 7 (after 1 st searching,we
found no swap required because 1 < 3)
1 3 2 5 7 (continue searching,we
found that 3>2,then swap 3 and 2)
1 2 3 5 7 (continue
searching,we found that no swap required)
fourth pass
1 2 3 5 7 (sorting complete!!)
sample program:
sample 1:
var
data:array[1..5] of integer;
j,k:integer;{for searching counter}
procedure Swap(var x,y:integer);
var temp:integer;
begin
temp:=x;
x:=y;
y:=temp;
end;
begin {main}
data[1]:7;data[2]:=9;data[3]:=8;data[4]:=1;data[5]:=3;
{sorting below\/}
for K:=1 to N -1 do
for J:= 1 to N - K do
if A[J]>A[J+1]
then
swap(A[J],A[J+1]);
{output}
writeln(data[1],' ',data[2],'
',data[3],' ',data[4],' ',data[5]);
end.
that's the end of this days:
next day ,we will talk about linear insertion sort and merging