This week I was exposed to various sorting algorithms. I learned about three basic sorting algorithms namely: the Insertion sort, the bubble sort and the selection sort. These algorithms are generally slow because especially where large numbers are involved. This is the reason why they have a time complexity of O(n2). I also learned about some more advanced algorithms such as : Ā· Shellsort Ā· Mergesort Ā· Quicksort Ā· Heapsort Ā· Binsort and Radix Sort These algorithms are very good at sorting where a large data set is involved. In my written assignment, I chose to implement the merge sort in my algorithm. I chose this divide and conquer algorithm because it is considered as one of the most efficient algorithms. I also found it really easy to understand and implement. Basically, this algorithm works by recursively breaking down a list into smaller lists until it is atomic, then reunites these smaller lists into one fully sorted list. The merge sort algorithm also requires less exchanges as compared to the basic algorithms. Understanding how the merge sort algorithm works was the easy part. I ran into a few problems when I tried to implement it. I needed to determine the mid-point of a given array, then create two temporary arrays. I would then copy half of the elements of the original array in the first temporary array and the other half into the second temporary array. This process required carefully analysing the variables needed and also the looping constructs. I made a lot of errors along the way, but fortunately there are a lot of online resources I referred to which helped. In particular, the geek for geeks website really helped not only by explaining but by also providing a well-documented sample of the merge sort algorithm. They also provided a pictorial explanation of the merge sort algorithm, which really helped me understand. The assignment question also required me to determine the number of exchanges after inputting twenty one given numbers in the algorithm. This was the easy part since I only needed to declare a static variable (I called it "number of swaps"). For each swap done by the algorithm, the variable was to increment itself by one. In the end, I discovered that my algorithm only performed 68 exchanges,
unlike the given insertion sort algorithm, which managed to perform 117 exchanges. This exciting discovery proved the superiority of the merge sort algorithm. Although I failed to submit my discussion assignment due to technical issues beyond my control, I managed to read about it. The discussion assignment required an explanation about the quick sort algorithm. The quick sort algorithm simply picks one element in the array and chooses it to be the pivot(Actually, the middle element or the last element in the array are usually used as pivots). All elements smaller than the pivot element goes in one sub array and all elements larger than the pivot element go into another. Whilst this quick sort algorithm works much faster