Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example.
Quick Sort is an Unstable sorting algorithm
Proof:
Stable Sorting algorithm is that maintains the relative position of element after sorting i.e. if we have following elements 1,9 8,8,9,2,4,5, we can represent as
a-1,b-9, c-8,d-8,e-9,f-2,g-4,h-5 the sorting order will be 1,2,4,5,8,8,9,9 which is clearly explained in the picture.
In the Stable sort elemets will be sorted and it also maintains order, in this example we can see 8 comes two times which is in the position c & d. In the stable sort the c comes before d even though the elements are same, Whereas in the Unstable sort order is not maintained, i.e., d comes before c.
why quick sort is unstable sorting ?
Let us take example 1,9 8,8,9,2,4,5 now we have to sort using quick sort.
first we will consider the pivotal element is last element i.e. 5
pivot=a[end]
now two indices i=start-1; j=start i.e. i=-1 and j=0 now from left of array keep seeing element if it is less then pivot i.e. 5 swap with left most element index by i and finally place the pivot element at position where all the element left to pivot is less then pivot and right greater. Now start
1,9 8,8,9,2,4,5
1<5 so swapping i=0,j=1
next 9,8,8,9 are not less 5 so no swapping so j will be 4. now next 2 <5 so swapping so not
1,2,8,8,9,9,4,5 i=1 and j=5 so now the 9 was before is swapped after 9 which was relative behind of first 9 so unstable again 4<5 so swapping 1,2,4,8,9,8,9,8,5
finally swap the pivot with i=3 so 1,2,4,5,8,9,8,9.
now see 8 and 9 are swapped with the later 8,9 which is relative order has changed.
Since the relative order is not followed, we can say that quick sort is UNSTABLE SORTING ALGORITHM
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example.
1.give an example and demonstrate that heapsort is not a stable sorting algorithm, 2.discuss why heapsort is unstable
Is quick sort a stable or unstable sorting algorithm. Explain with an example.
. Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....
1. Which is the best sorting algorithm for larger arrays if all the items can not fit in main memory? selection sort insertion sort quicksort Merge sort 2. Which sorting algorithm sorts items without comparing them? quick sort radix sort merge sort Insertion sort 3 What is the average running time for quicksort? O(n2) O(logn) O(nlogn) O(n) O(n2logn) 4. Examine the steps of the following algorithm and write the name of the algorithm described in the blank provided: Recursively divide...
Computer Algorithm question 8) Give an algorithm for building a heap in O(n) 9) Prove the algorithm given in 8) runs in O(n) time. 10) What is the asymptotic runtime of an algorithm represented by the following recurrence equation? 11) Suppose you have the following priority queue implemented as a (max) heap. What will the heap look like when the max node is removed and the heap is readjusted? Assume on each heapify operation the largest child node is selected...
(5 marks; questions to Reza) In Lecture 5, Travis said you can prove QuickSort takes N(n log n) time in the best case the same way he proved any comparison-based sorting algorithm takes (n log n) time in the worst case. Give that proof. Notice it doesn't follow directly: e.g., Insertion Sort takes O(n) time in the best case. You can assume QuickSort divides each array into elements less than or equal to the pivot (including the pivot itself) and...
Please answer only if you are sure and make it clear to understand. Do not make it too long and complicated. Thank you in advance. "Give an example of showing that quicksort is not a stable sorting algorithm".
QUESTION 3 Suppose that you have been running an unknown sorting algorithm. Out of curiosity, you once stopped the algorithm when it was part-way done and examined the partially sorted array. You discovered that the last K elements of the array were sorted into ascending order, but the remainder of the array was not ordered in any obvious manner. Based on this, you guess that the sorting algorithm was (select all that apply): heapsort insertion sort mergesort quicksort Shell's sort...
1.3 Which of the following is true about sorting functions?A.The most optimal partitioning policy for quicksort on an array we know nothing about wouldbe selecting a random element in the array.B.The fastest possible comparison sort has a worst case no better than O(n log n).C.Heapsort is usually best when you need a stable sort.D.Sorting an already sorted array of size n with quicksort takes O(n log n) time.E.When sorting elements that are expensive to copy, it is generally best to...