Week 4 –
CST370 – Design and Analysis of Algorithms
This week we continued
learning about the divide and conquer technique. We specifically learned how to
apply it to the Mergesort algorithm. Mergesort sorts an array by dividing it
into two halves and sorting those two halves recursively. Lastly, you merge the
two sorted arrays into a single sorted array. The algorithm merges the two
sorted arrays with two pointers that are initialized to point at the first elements
of each array being merged. The two elements are compared and the element with smaller
value is added to the new sorted array. The next step is to increment the index
of the smaller element and the two elements are compared again. This is
repeated until the end of an array is reached. At that point, the remaining
elements of the other array are simply copied to the new array. One of the main
advantages of Mergesort is that it always has a time complexity of O(n log n)
regardless of the input data distribution which makes it well suited for large
data sets.
Comments
Post a Comment