Test Program 3 : Bubblesort (recursive)¶
This test program reads elements into an array and sorts them using the classic bubblesort algorithm. (recursive version)
Input¶
- Number of elements to be sorted from standard input.
- Elements to be sorted
Output¶
A sorted array of elements.
This program test the recursion, conditional and arrays.
decl
int n,arr[10],i,j,dup, BubbleSort(int first, int last);
enddecl
int BubbleSort(int first, int last)
{
decl
int temp;
enddecl
begin
if((first < last) AND (last > 0)) then
if(arr[first] > arr[first+1]) then
temp = arr[first];
arr[first] = arr[first+1];
arr[first+1] = temp;
endif;
dup = BubbleSort(first+1, last);
dup = BubbleSort(first, last-1);
endif;
return 0;
end
}
int main()
{
decl
int r;
enddecl
begin
read(n);
i=0;
while(i<n) do
read(arr[i]);
i = i+1;
endwhile;
r = BubbleSort(0,n-1);
i=0;
while(i<n) do
write(arr[i]);
i = i+1;
endwhile;
return 0;
end
}