Advertisement
mixster

mixster

Oct 28th, 2008
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.09 KB | None | 0 0
  1. program Alphabetizer;
  2. const
  3.   wordList = 'The,quick,brown,dog,jumped,over,lazy,foxes,dagger';
  4.   // Above is the words to alphabetize separated by commas
  5.  
  6. var
  7.   strWords, strSorted: TStringArray;
  8.   intWords: T2DIntArray;
  9.   i, s, h, l: Integer;
  10.  
  11. function Explode(str, del: string): TStringArray; // Will separate a string based on del
  12. var
  13.   i, l: Integer;
  14. begin
  15.   i := 0;
  16.   l := -1;
  17.   repeat
  18.     Inc(l);
  19.     SetLength(Result, l + 1);
  20.     i := Pos(del, str);
  21.     if i <= 0 then
  22.       Break;
  23.     Result[l] := Copy(str, 1, i - 1);
  24.     Delete(str, 1, i + Length(del) - 1);
  25.   until false
  26.   Result[l] := Copy(str, 1, Length(str));
  27. end;
  28.  
  29. begin
  30.   Writeln('Sorting out "' + wordList + '" into alphabetical order!');
  31.   strWords := Explode(Lowercase(wordList), ','); // Splits the word list up into individual words
  32.   SetArrayLength(intWords, High(strWords) + 1); // Sets the first dimension of intWords to the number of words
  33.  
  34.   for i := 0 to High(strWords) do // Go through all the words
  35.   begin
  36.     SetArrayLength(intWords[i], Length(strWords[i])); // Set the 2nd dimension of intWords to the same as that of the word
  37.     for s := 0 to High(intWords[i]) do // Loop through all the values in the array
  38.       intWords[i][s] := Ord(strWords[i][s + 1]); // and assign them to the code for the word at the same position - it is +1 as string arrays start on 1 rather than 0
  39.   end;
  40.  
  41.   while High(strSorted) < High(strWords) do // While the sorted word array is smaller than actual word array, ie not all sorted yet
  42.   begin
  43.     h := 0; // Reset h - will hold the current lowest alphabetically word index
  44.     for i := 1 to High(intWords) do // Loop through all of the other words
  45.     begin
  46.       l := High(intWords[h]); // l holds shortest length
  47.       if l > High(intWords[i]) then // if the current word is shorter,
  48.         l := High(intWords[i]); // then use l as this
  49.       for s := 0 to l do // loop through the characters up
  50.         if intWords[i][s] < intWords[h][s] then // if the current words character code is smaller
  51.         begin
  52.           h := i; // Set h to this index as this is newest lowest alphabetically
  53.           Break; // Break out loop of outer loop as no need to continue comparing
  54.         end
  55.         else if intWords[i][s] > intWords[h][s] then // if word[h] is lower alphabetically
  56.           Break; // then break as further comparison unneeded
  57.     end;
  58.     l := High(strSorted) + 1; // Used to improve readability - length of strSorted
  59.     SetArrayLength(strSorted, l + 1); // Make strSorted 1 array longer as about to add new string
  60.     for s := 0 to High(intWords[h]) do // loop through all values in the lowest alphabetically word
  61.       strSorted[l] := strSorted[l] + Chr(intWords[h][s]); // and convert the character code to a char and add on
  62.     Swap(intWords[h], intWords[High(intWords)]); // Put the current high alphabetically to the end of the array
  63.     SetArrayLength(intWords, High(intWords)); // and set the array one shorter so that it is removed
  64.   end;
  65.   for i := 0 to High(strSorted) do // loop through the sorted array
  66.     Writeln(strSorted[i]); // and write it out
  67. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement