Question

In: Computer Science

Prove by argument that heapsort and merge sort are asymptotically optimal comparison sorts.

Prove by argument that heapsort and merge sort are asymptotically optimal comparison sorts.

Solutions

Expert Solution

HEAPSORT:

Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like selection sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heapsort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step.

More specifically, a heap stores 'n' elements in an array, 'a', at array locations a[0],......,a[n-1] with the smallest value stored at the root, a[0]. After transforming 'a'  into a BinaryHeap, the heap-sort algorithm repeatedly swaps a[0] and a[n-1], decrements n, and calls trickleDown[0] so that a[0],....,a[n-2]  once again are a valid heap representation. When this process ends (because n=0) the elements of 'a' are stored in decreasing order, so 'a'  is reversed to obtain the final sorted order.11.1Figure 11.4 shows an example of the execution of heapSort(a,c).

MERGE SORT:

Merge sort is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output.

The merge-sort algorithm is a classic example of recursive divide and conquer: If the length of 'a' is at most 1, then 'a' is already sorted, so we do nothing. Otherwise, we split 'a' into two halves, a0=a[0],.........,a[n/2-1] and a[1]=a[n/2],.....,a[n-1]. Then a0 and a1 are recursively sorte,  and then merge (the now sorted) a0 and a1 to get our fully sorted array 'a'.


Related Solutions

Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
1. Prove by induction on the column being sorted that RADIX-SORT correctly sorts its input. The...
1. Prove by induction on the column being sorted that RADIX-SORT correctly sorts its input. The Induction should include the following steps: Loop Invariant, Initialization, Maintenance, and Termination.
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Compare and Contrast Bubble Sort with Merge Sort. Discuss about the efficiency of both.
Compare and Contrast Bubble Sort with Merge Sort. Discuss about the efficiency of both.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a)...
This question has three parts: a) binary search, b) selection sort, and c) merge sort. a) Suppose we are performing a binary search on a sorted list called numbers initialized as follows: #index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 numbers = [-2, 0, 1, 7, 9, 16, 19, 28, 31, 40, 52, 68, 85, 99] #search for the value 5 index = binary_search(numbers, 5) Write the indexes of the elements that would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT