Author Topic: SelectionSort  (Read 1200 times)

0 Members and 1 Guest are viewing this topic.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
SelectionSort
« on: February 19, 2009, 04:47:09 pm »
Script Name: SelectionSort
Script Description Sorts alphabetic & "integer" strings
Author: Dual

Hosted by: Soldat Central - http://soldatcentral.com/
Core Version: 2.6.3
Compile Test: Passed

SelectionSort() allows you to arrange a string array representing alphabetic characters (including special characters) in alphabetical order,
as well as a string array representing integer literals. Integer values will be sorted by greatness.

Code: [Select]
procedure ZeroFill(var S: string; Count: integer);
var
  i: integer;
begin
  for i:= 1 to Count do
  begin
    S:= '0' + S;
  end;
end;

function SelectionSort(var AOS: array of string; Index: integer; Ival: boolean): integer;
var
  b, c1, c2: string;
  i, j, m: integer;
begin
  for i:= 0 to Index do
  begin
    if (length(AOS[i]) > m) then
    begin
      m:= length(AOS[i]);
    end;
  end;
  i:= 1;
  while (j < Index) do
  begin
    c1:= AOS[j];
    c2:= AOS[j + i];
    if (Ival) then
    begin
      ZeroFill(c1, m - length(c1));
      ZeroFill(c2, m - length(c2));
    end;
    if (c1 < c2) then
    begin
      b:= AOS[j];
      AOS[j]:= AOS[j + i];
      AOS[j + i]:= b;
      Inc(Result, 1);
    end;
    if (j + i < Index) then
    begin
      Inc(i, 1);
    end else
    begin
      i:= 1;
      Inc(j, 1);
    end;
  end;
end;

Parameters:

AOS: array of string
String array to be sorted.

Index: integer
Assigns an array index which determines the sort extent.
If you want to sort the whole array pass the highest array index on it.

Ival: boolean
Only enable this if you want to sort an array representing integer values.

Return Value:

SelectionSort() returns the amount of interchanges which were required to sort the array.
It also returns the sorted array in terms of a reference.

Note:

Always disable Ival if you sort strings by alphabetic characters otherwise SelectionSort() won\'t work properly.



(Size 755 B)
- http://soldatcentral.com/index.php?page=script&f=103 -


** Script hosted by Soldat Central! Please visit the author's script page and Rate this script **
Soldat Global Account System: #soldat.sgas @ quakenet