Chapter Two Int 2 Algorithm
Chapter Two Int 2 Algorithm
Merge Sort
Binary Search
Quick Sort
MERGE SORT
Merge sort is one of the most efficient sorting algorithms. It is based on the divide-and-conquer
strategy. Merge sort continuously cuts down a list into multiple sub-lists until each has only one
item, and then merges those sub-lists into a sorted list.
Merge sort algorithm divides the array into two halves and applies merge sort algorithm to each
half individually after which the two sorted halves are merged together. Thus its method of
sorting is called merging.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
Again, divide each subpart recursively into two halves until you get individual elements.
3. Now, combine the individual elements in a sorted manner. Here, conquer and combine steps
go side by side.
Example 0ne of Divide and Conquer algorithm
A classic example of Divide and Conquer is Merge Sort demonstrated below.
In Merge Sort, we divide array into two halves, sort the two halves recursively, and then merge
the sorted halves.
We know that merge sort first divides the whole array iteratively into equal halves unless the
atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size
4.
This does not change the sequence of appearance of items in the original. Now we divide these
two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the
color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target
list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and
44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and merge
them into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Quick Sort
Quicksort is a highly efficient sorting technique that divides a large data array into smaller ones.
Quick sort is capable of sorting a list of data elements significantly faster (twice or thrice faster)
than any of the common sorting algorithms.
Example one
Consider the following list of elements and the element to be searched...
Example Two
Step 2: Compare 38 with the middle element. 38 > 25 so, discard the first half.
Step 3: Select the send half of the array. For the second half, low = middle+1 as shown:
Step 4: Find the middle element of this smaller array which comes out to 32. Compare 38 with
32.
38 > 32 Thus, discard the first half of this array.
Step 5: Select the remaining half of the array by doing low = middle+1 as shown:
Finally, we have found 38 and thus the algorithm will halt here.
Output: 8.
Example three
The above image illustrates the following:
Quick Sort algorithm doesn’t require any extra Merge sort requires extra storage space to store
storage the auxiliary array
It can be said as an unstable algorithm yet it It is stable as the elements are divided into two
can be made stable by modifying some equal parts and the value is the same in sorted
changes in the code itself array as it was in unsorted array before sorting