How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the CLU programming language
How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the CLU programming language
Table of Contents
Problem Statement
A bubble sort is generally considered to be the simplest sorting algorithm.
A bubble sort is also known as a sinking sort.
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
Because of its abysmal O(n2) performance, it is not used often for large (or even medium-sized) datasets.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the CLU programming language
Source code in the clu programming language
% Bubble-sort an array in place.
bubble_sort = proc [T: type] (a: array[T])
where T has lt: proctype (T,T) returns (bool)
bound_lo: int := array[T]$low(a)
bound_hi: int := array[T]$high(a)
for hi: int in int$from_to_by(bound_hi, bound_lo, -1) do
for i: int in int$from_to(bound_lo, hi-1) do
if a[hi] < a[i] then
temp: T := a[i]
a[i] := a[hi]
a[hi] := temp
end
end
end
end bubble_sort
% Print an array
print_arr = proc [T: type] (a: array[T], w: int, s: stream)
where T has unparse: proctype (T) returns (string)
for el: T in array[T]$elements(a) do
stream$putright(s, T$unparse(el), w)
end
stream$putc(s, '\n')
end print_arr
start_up = proc ()
ai = array[int]
po: stream := stream$primary_output()
test: ai := ai$[7, -5, 0, 2, 99, 16, 4, 20, 47, 19]
stream$puts(po, "Before: ") print_arr[int](test, 3, po)
bubble_sort[int](test)
stream$puts(po, "After: ") print_arr[int](test, 3, po)
end start_up
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the CLU programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Elixir programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Toka programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the Python programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Efene programming language