BubbleSort - Release Notes


Copyright 2025 by The Software Samurai.
On the web: http://www.SoftwareSam.us/
Software released under GNU GPL3, and documentation released under FDL1.3
This document describes version 0.0.03 of BubbleSort.


Description

The BubbleSort (‘bubsort’) utility is a GNU/Linux console application which demonstrates the structure of the “bubble sort” sorting algorithm for either text or numeric data.

  1.  The "bubble sort" is the simplest (and slowest) of the common sorting algorithms.
        It's great flexibility is its strength, but it is recommend that it be used only if the total number of records is
        “relatively small”.
  2.  This application uses a bi-directional bubble sort, also known as the “cocktail sort”, presumably because its inventor was sipping a martini at the time.
  3.  Sorting record pointers is much faster than sorting the actual records, regardless of record content, and therefore, except for pure numeric data, pointer sorting is recommended whenever practical. This application demonstrates sorting both using record pointers, and direct sorting of the record data.
  4.  When sorting complex records such as class instances, a method must be devised to determine which record is greater or lesser than the other.
    1.  This application uses the author's ‘WinPos’ class to demonstrate this technique. WinPos is a simple class with only two data members (row and column), but with an assortment of constructors and assignment operators for positioning data within a window.
    2.  While the WinPos class defines "operator==" and "operator!=" it does not include greater-than or less-than operators, so one of the data members is used to perform these comparisons during the sort. The assignment operators are used to demonstrate direct swapping of the objects in the array being sorted. Example:
      WinPos wpA( 14, 56 ), wpB( 12, 56 ), wpSwap; if ( wpA.row > wpB.row ) { wpSwap = wpA; wpA = wpB; wpB = wpSwap; }
    3. Again, sorting pointers to objects is much faster that sorting the objects themselves, and should be used whenever practical; especially when the data are sorted and re-sorted under user control.  Hint: pointers are actually numeric data.
      int RECORDS = 250, src = 0, trg = RECORDS - 1; WinPos array[RECORDS] = { {14,56}, ... {78,22} }; WinPos* ptr[RECORDS] = { &array[0], &array[1], ... &array[RECORDS - 1] }; WinPos* swap; if ( ptr[src]->row > ptr[trg]->row ) { swap = ptr[src]; ptr[src] = ptr[trg]; ptr[trg] = swap; }
      Please see the author's FileMangler application for a real-world example of sorting complex data records (file stat records for an arbitrary number of files and directories).

This application was designed under Fedora Linux and is built using the GNU C compiler, version 13.3.1 or higher (C++17 support required). If you are not using GCC, then verify that your compiler fully supports the C++17 standard.


Building From Source



Operational Overview

BubbleSort is a very basic demonstration utility, bypassing user-friendliness in favor of simplicity.
The standard console app ‘− −help’ and ‘− −version’ options are available. A small number of command-line options provide access to all available functionality. See the next section for details.

This application relies heavily on our gString text-analysis tool which parses the source data, performs all text comparisons as well at moving text records (if necessary) during the sorting process.
Using the gString class for comparison and copying of text data encapsulates or bypasses all of the dangerous
C-language string manipulation functions. While C-language functions such as ‘wcsncasecmp’, ‘wcpncpy’, ‘wcwidth’, ‘mbsrtowcs’ ‘wcsrtombs’ and others are undeniably more efficient than the poorly-designed C++ ‘std::string’ etc.; they often lead to buffer overruns, memory leaks and other dangerous relics of a previous century. Using the gString class leverages the speed of these functions while minimizing the danger.
// String Comparison Examples: gString gsI( "Apples" ), gsL( "Bananas" ); // Case Sensitive: if ( (gsI.compare( gsL, true )) < ZERO ) { ... } if ( (gsI.compare( "Avocados", true )) < ZERO ) { ... } if ( (gsI.compare( L"D'Anjou pears", true )) < ZERO ) { ... } // Case Insensitive: if ( (gsI.compare( gsL, false )) < ZERO ) { ... } // Locale-aware comparison: if ( (gsI.compcoll( gsL )) < ZERO ) { ... } // String Copy Examples: const char* green_apple = "Eat green apples."; wchar_t wbuff[32]; // UTF-32 Target Buffer char ubuff[32]; // UTF-8 Target Buffer: gsI = green_apple ; // load the data gsI.copy( wbuff, 32 ); // write max. 32 characters gsI.copy( ubuff, 32 );) // write max. 32 bytes

For a full discussion of the gString class, please see the documentation for the author's NcDialog API package, or the stand-alone gString text-analysis-and-formatting package. gString Documentation


Command-line Options

Invocation:
   Usage: bubsort --dir==[a | d] [OPTIONAL ARGS]


Application “Locale”

On startup, the Bubblesort application sets the application locale (if any) from the environment. While this locale has no direct effect on the standard sorting processes used by this application, it does come into play for the
“– –compcoll” debugging option.
The author recommends that each non-trivial application should explicitly initialize the locale on startup. This is accomplished by creating an instance of the std::locale object as shown: #include std::locale* ioLocale = new std::locale ( "" ) ; ioLocale->global( *ioLocale ) ;
For information on performing a "collated" (locale-aware) sort, please see the standard library 'strcoll' and 'wcscoll' functions. For a simple example of this sort, see the 'SortCollated' method in BubbleSort.cpp, which in turn calls the 'compcoll' method of the gString class. The author has found this type of sort to be almost entirely worthless, but your mileage may vary.
Specifically, the C-library ‘wcscoll’ function seems to be unstable and may return an incorrect value for some comparisons. The ‘wcscoll’ documentation says:
Collation order is the dictionary order: the position of the letter in the national alphabet (its equivalence class)
has higher priority than its case or variant. Within an equivalence class, lowercase characters collate before
their uppercase equivalents and locale-specific order may apply to the characters with diacritics. In some locales,
groups of characters compare as single collation units. For example, “ch” in Czech follows “h” and precedes “i”,
and “dzs” in Hungarian follows “dz” and precedes “g”.     https://en.cppreference.com/w/c/string/wide/wcscoll
In the American English locale, however, the comparison seems to be case-insensitive.
This needs further research.