Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Alphabetizer;
- const
- wordList = 'The,quick,brown,dog,jumped,over,lazy,foxes,dagger';
- // Above is the words to alphabetize separated by commas
- var
- strWords, strSorted: TStringArray;
- intWords: T2DIntArray;
- i, s, h, l: Integer;
- function Explode(str, del: string): TStringArray; // Will separate a string based on del
- var
- i, l: Integer;
- begin
- i := 0;
- l := -1;
- repeat
- Inc(l);
- SetLength(Result, l + 1);
- i := Pos(del, str);
- if i <= 0 then
- Break;
- Result[l] := Copy(str, 1, i - 1);
- Delete(str, 1, i + Length(del) - 1);
- until false
- Result[l] := Copy(str, 1, Length(str));
- end;
- begin
- Writeln('Sorting out "' + wordList + '" into alphabetical order!');
- strWords := Explode(Lowercase(wordList), ','); // Splits the word list up into individual words
- SetArrayLength(intWords, High(strWords) + 1); // Sets the first dimension of intWords to the number of words
- for i := 0 to High(strWords) do // Go through all the words
- begin
- SetArrayLength(intWords[i], Length(strWords[i])); // Set the 2nd dimension of intWords to the same as that of the word
- for s := 0 to High(intWords[i]) do // Loop through all the values in the array
- 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
- end;
- while High(strSorted) < High(strWords) do // While the sorted word array is smaller than actual word array, ie not all sorted yet
- begin
- h := 0; // Reset h - will hold the current lowest alphabetically word index
- for i := 1 to High(intWords) do // Loop through all of the other words
- begin
- l := High(intWords[h]); // l holds shortest length
- if l > High(intWords[i]) then // if the current word is shorter,
- l := High(intWords[i]); // then use l as this
- for s := 0 to l do // loop through the characters up
- if intWords[i][s] < intWords[h][s] then // if the current words character code is smaller
- begin
- h := i; // Set h to this index as this is newest lowest alphabetically
- Break; // Break out loop of outer loop as no need to continue comparing
- end
- else if intWords[i][s] > intWords[h][s] then // if word[h] is lower alphabetically
- Break; // then break as further comparison unneeded
- end;
- l := High(strSorted) + 1; // Used to improve readability - length of strSorted
- SetArrayLength(strSorted, l + 1); // Make strSorted 1 array longer as about to add new string
- for s := 0 to High(intWords[h]) do // loop through all values in the lowest alphabetically word
- strSorted[l] := strSorted[l] + Chr(intWords[h][s]); // and convert the character code to a char and add on
- Swap(intWords[h], intWords[High(intWords)]); // Put the current high alphabetically to the end of the array
- SetArrayLength(intWords, High(intWords)); // and set the array one shorter so that it is removed
- end;
- for i := 0 to High(strSorted) do // loop through the sorted array
- Writeln(strSorted[i]); // and write it out
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement