Wednesday, April 2, 2014

Week of March.30.2013 - Sorting and Efficiency

One of the most important concepts in Computer Science is efficiency.  When it comes to any language, a general algorithm must take the least amount of steps in order to complete the task, otherwise you will be wasting precious resources, and tasks may take longer.

For example, we learnt about insertion sort, and quick sort.  Both algorithms are used to sort items, and sorting items is extremely important, especially when we want to search within the list of items.  Binary search belongs to big O of lgn, meaning the time it takes to accomplish the task of finding an item takes only a constant multiplied by lgn, n being the number of steps, and it grows at a rate of lgn when the lists increases.  But one important precondition for this algorithm to work is if the list is sorted.  Insertion sort belongs to big O of n^2 for the average runtime, and quick sort on the other hand belongs to big O of nlgn as the average runtime, showing significant better performance over the former.

That being said, quick sort is not the only super sort that is capable of nlgn runtime.  Other sorts such as merge sort, which splits the list in half continuously, and merges the multiple lists into a sorted list which essentially breaks the problem into halves, which is where the lgn comes from, multiply that with the actual length of the list, and you get nlgn runtime.