Skip to content

Document on sorting

This should be equivalent to taking a course in sorting. Learning about the different sorting algorithms and time complexity of each one. Compare them and note the differences. Implement all of the algorithms listed below

  • Insertion Sort ― get element in the right place. It has average case of O(n^{2}). But it's efficient for small input sizes. As in the subject, extreme test is 500. By pushing everything to stack B (except for three elements, which will be sorted in ascending order). Then by optimizing the cost of pushing an element e back to stack A (using pa), brute-force* on the group of available ranges, with priority to the open ranges, where e fits within stack A. Using this process, we have picked the element e with the smallest cost and the closest range.

    (*) The brute-force means iterating over all elements, counting the costs of the current range (starting from boundaries and going to the middle.

  • Merge Sort ― also divide and conquer using mid, but this time we shall keep going until we're left with either two or 1 element. repeat the same process backward, and merge every element in the right order. it's the ultimate sorting when it comes to complexity. It's has worst and best cases of O(nlog).

    NOTE: Merge Sort is very handy for sorting lists, since it does not require many movements back and forth, but instead, we can only re-order the next pointers when we satisfy the condition of splitting (either 1 or two elements).

  • Quick Sort ― divide and conquer using mid, send everything that's relatively ordered to stack B, sort highest part, then sort the lowest. Although it has worst case of O(n^2), it still a good algorithm since it's average is O(nlogn) Still need to document more.

Edited by Anas Rchid