Advertisement
mixster

mixster

Oct 28th, 2008
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.68 KB | None | 0 0
  1. program Alphabetizer;
  2. const
  3.   wordList = 'The,quick,brown,dog,jumped,over,lazy,foxes,dagger';
  4.  
  5. var
  6.   strWords, strSorted: TStringArray;
  7.   intWords: T2DIntArray;
  8.   i, s, h, l: Integer;
  9.  
  10. function Explode(str, del: string): TStringArray;
  11. var
  12.   i, l: Integer;
  13. begin
  14.   i := 0;
  15.   l := -1;
  16.   repeat
  17.     Inc(l);
  18.     SetLength(Result, l + 1);
  19.     i := Pos(del, str);
  20.     if i <= 0 then
  21.       Break;
  22.     Result[l] := Copy(str, 1, i - 1);
  23.     Delete(str, 1, i + Length(del) - 1);
  24.   until false
  25.   Result[l] := Copy(str, 1, Length(str));
  26. end;
  27.  
  28. begin
  29.   Writeln('Sorting out "' + wordList + '" into alphabetical order!');
  30.   strWords := Explode(Lowercase(wordList), ',');
  31.   SetArrayLength(intWords, High(strWords) + 1);
  32.  
  33.   for i := 0 to High(strWords) do
  34.   begin
  35.     SetArrayLength(intWords[i], Length(strWords[i]));
  36.     for s := 0 to High(intWords[i]) do
  37.       intWords[i][s] := Ord(strWords[i][s + 1]);
  38.   end;
  39.  
  40.   while High(strSorted) < High(strWords) do
  41.   begin
  42.     h := 0;
  43.     for i := 1 to High(intWords) do
  44.     begin
  45.       l := High(intWords[h]);
  46.       if l > High(intWords[i]) then
  47.         l := High(intWords[i]);
  48.       for s := 0 to l do
  49.         if intWords[i][s] < intWords[h][s] then
  50.         begin
  51.           h := i;
  52.           Break;
  53.         end
  54.         else if intWords[i][s] > intWords[h][s] then
  55.           Break;
  56.     end;
  57.     l := High(strSorted) + 1;
  58.     SetArrayLength(strSorted, l + 1);
  59.     for s := 0 to High(intWords[h]) do
  60.       strSorted[l] := strSorted[l] + Chr(intWords[h][s]);
  61.     Swap(intWords[h], intWords[High(intWords)]);
  62.     SetArrayLength(intWords, High(intWords));
  63.   end;
  64.   for i := 0 to High(strSorted) do
  65.     Writeln(strSorted[i]);
  66. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement