How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Haxe programming language
How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Haxe 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 Haxe programming language
Source code in the haxe programming language
class BubbleSort {
public static function sort<T>(arr:Array<T>) {
var madeChanges = false;
var itemCount = arr.length;
do {
madeChanges = false;
itemCount--;
for (i in 0...itemCount) {
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
var temp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = temp;
madeChanges = true;
}
}
} while (madeChanges);
}
}
class Main {
static function main() {
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
3.5, 0.0, -4.1, -9.5];
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
'be', 'self-evident', 'that', 'all',
'men', 'are', 'created', 'equal'];
Sys.println('Unsorted Integers: ' + integerArray);
BubbleSort.sort(integerArray);
Sys.println('Sorted Integers: ' + integerArray);
Sys.println('Unsorted Floats: ' + floatArray);
BubbleSort.sort(floatArray);
Sys.println('Sorted Floats: ' + floatArray);
Sys.println('Unsorted Strings: ' + stringArray);
BubbleSort.sort(stringArray);
Sys.println('Sorted Strings: ' + stringArray);
}
}
You may also check:How to resolve the algorithm JSON step by step in the Raku programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the PL/I programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Phix programming language