Selection sort is one of the simplest sorting algorithms, but it is only suitable for sorting small data sets. It locates the element with the smallest value and exchanges it with the element in the first place. It then moves to the second place, and scans the entire list for the next smallest element, and exchanges that with the value in the second place, and so on, until it reaches the end of the list. Because it scans the remaining list for each position in the list, it is to time consuming and inefficient to use as a sorting method for very large lists...
Using Selection Sort to sort
a Tstrings type using the 'Exchange' methodThe OpenDialog.Files 'Tstrings' property holds the file (or files) the user chooses when the dialog executes. It is a Read Only property, but you can move the order of the files around if you use the Tstrings 'Exchange' swap function. This 'swap function' mimics the behavior of the Selection Sort Algorithm, which is suitable for sorting short lists. The code moves through a list, and at each position in the list, it checks all the following positions to see if an item further down the list has a value less than the current position. If the value is less than the current item, then it belongs in the place occupied by the current item, and a simple swap takes place. Because selection sort searches to the end of the list for each element in the list, it is suitable only for shorter lists, and a more efficient search technique should be used for long lists. The Tstrings property 'Count' holds the number of elements in the string array, starting from '1' (it would be zero if the array was empty). However the strings in the array are accessed from '0' the first element, which means that when you want to move through each element, from first to last, the last element must be accessed as Tstrings[Tstrings.Count-1] to avoid generating an exception (string index out of bounds)....
Using Selection sort to arrange the elements
procedure TForm1.OpenDialogSortfiles;
in an OpenDialog.Files Tstrings element
var counter, look:integer; temp:Tstrings;
begin
if OpenDialog1.Files.Count<>1 then begin
temp := TStringList.Create;
{strings with mixed upper and lower case names will not sort alphabetically since strings are sorted based on the digital value of letters and not the letters themselves...therefore the sort will be based upon lowercase strings but the filenames themselves will not be changed to avoid the possiblity of error messages on systems set to be case sensitive ... The OpenDialog Tstrings property is Read Only so in order for the sort to work correctly a temporary Tstrings variable will be used to hold the actual indexing value for the sort}
for counter:=0 to OpenDialog1.Files.Count-1 do
temp.Append(LowerCase(OpenDialog1.Files.Strings[counter]));
for counter:=0 to temp.Count-1 do
for look:=counter+1 to temp.Count-1 do
if temp[look]<temp[counter] then begin
OpenDialog1.Files.Exchange(look, counter);
temp.Exchange(look,counter);
end;
temp.Free;
end; {if}
end;
Selection sort can also be used to sort arrays of strings or numbers, but, while the code works the same as it does when sorting a Tstrings list, as illustrated above, the Exchange function must be replaced with code that performs the same swapping function, and this results in the creation of a few extra variables, as well as some extra code...
Using Selection Sort to sort an array
In the following example I am assuming that the array is a 'global variable' since passing an array to a function and then returning an array from a function seems like a waste of resources, so the code is written as a procedure...You should determine that the array actually contains some values before calling this code, and is not empty, and then pass the value of the last element in the array as the variable 'Top' to avoid generating an error...In the following code the array is denoted by the variable name TheArray. If the array is a string array keep in mind that mixed lower and upper case entries will not be sorted properly for the reasons mentioned above...
Procedure TForm1.SelectionSortArray(Top:integer);
var
counter, min, look:integer;
temp:string; {if the array is of integer type then temp must be defined as 'integer' etc.}
begin
for counter:=0 to Top do begin
min:=counter;
for look:=counter+1 to top do
if TheArray[look]<TheArray[min] then min:=look;
temp:=TheArray[min]; TheArray[min]:=TheArray[counter];
TheArray[count]:=temp;
end;
end;
You can download the source code as a text file by right clicking on the following link -> selection_sort.txt
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

