To implement a program to sort a list / array using merge sort algorithm.
The program takes an input list of integers 'arr', and first prints the original list using the 'printList()' function. Then, the program calls the 'mergeSort()' function to sort the input list. After the sorting is completed, the program prints the sorted list again using the 'printList()' function.
The 'mergeSort()' function first checks if the length of the input list is greater than 1. If the length is greater than 1, it calculates the middle index of the list and splits the input list into two sub-lists: 'L' and 'R', where 'L' contains the left half of the list and 'R' contains the right half. The function then recursively calls itself with 'L' and 'R' as input, until the length of both 'L' and 'R' becomes 1.
After the recursive calls return, the function merges the two sub-lists by comparing the elements of 'L' and 'R', and adding the smaller element to the final sorted list 'arr'. This process continues until all elements of 'L' and 'R' have been added to 'arr'.
Finally, the 'printList()' function takes a list as input and prints each element of the list, separated by a space.
The divide and conquer strategy is a problem-solving approach in computer science and mathematics that involves breaking down a complex problem into smaller subproblems, solving them independently, and then combining the solutions to solve the original problem. The name comes from the three main steps involved:
The divide and conquer strategy is often used to solve problems that are difficult to solve directly, such as sorting algorithms, matrix multiplication, and finding the closest pair of points in a set.
The main advantage of the divide and conquer strategy is that it allows for parallel processing of the subproblems, which can result in faster processing times. Additionally, it can be easier to reason about and debug smaller subproblems rather than the original, larger problem.
Some common algorithms that use the Divide and Conquer strategy include:
By learning the Divide and Conquer algorithm to find the maximum value of an array, you can achieve several learning outcomes, including: