Friday, 28 March 2014

Sorting and efficiency! These two are the topics I learned for the past few weeks. I find it a little bit complecated. The code seems to be very complex at first and it took me few days to finally understand the code.

The type of sorting that is the most complicated for me is the merge sort because it divides each item in the list recursively, then sort it and finally merge it again. It turns out that, though merge sort seems to be a little bit complicated, this type of sorting is pretty fast compared to other types of sorting such as bubble sort and selection sort. The worse case of merge sort is nlogn, which means if there is a list with length n, the list will take at most nlogn to be sorted. Worse case is the time that it takes the most to implement a program. The opposite of worse case, called the best case scenario, is the quickest time that a program can run.

Here is an example of merge sort. Let L = [6, 3, 7, 2, 9, 1]. When merge sort is implemented, list L is broken down into two parts and each part will be broken down into two smaller pieces until there is only a single piece. Then, the term in the even position will be compared to the term next to it (for example, term at index 0 with index 1, index 2 with index 3, etc). The two terms will be merged together and the term with lower value will be placed before the term with larger value. After this is done, each two-merged term will be compared to the one next to it and, similar to the previous merging step, the term wil lower value will be placed before the larger one. This step is done when the list is already sorted correctly.

There is more than one sorting type that is discussed in the lecture notes and class, such as bubble sort, selection sort, insertion sort, shell sort, quick sort, tim's sort and count sort. However, I only explained the merge sort in detail because this is the type of sort that challenges me the most becaus it takes more time to understand it compared to the other sorting type. Also, this sorting type is very efficient as the worse case scenario can be presented in nlogn compared to, say, bubble sort, selectiom sort, and insertion sort that has a worse case scenario of n^2, which is considered to be slower than merge sort.

No comments:

Post a Comment