Question

In: Computer Science

Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example

Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example

Solutions

Expert Solution

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.


Related Solutions

Recall the definition of stable sorting: a sorting algorithm is said to be stable when elements...
Recall the definition of stable sorting: a sorting algorithm is said to be stable when elements with equal keys (after the sorting is complete) remain in the same order as they were in the input (before the sorting). Answer the following: Is Radix Sort stable, and why? Show an example: start from 10 random integer numbers (of at least 2 digits per integer number) to be sorted, where at least 2 of those 10 elements need to have equal keys;...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
How can I determine or explain how BingoSort is a stable sorting algorithm or not?
The following submission rules apply: • For those questions requiring programs, the solutions must be implemented using JavaScript or Java. o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices. o Solutions must be provided in plain text so that formatting is not lost. • All answers must be provided in this document. • Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.How can I determine or...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call it AWESOME-QUICKSORT , that has worst-case run time O(nlogn). State the cost of your algorithm using a recurrence T(n), and solve then solve this recurrence any way you like (e.g., master method).
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in both Python and Java. This sort is a combination of two different sorting algorithms: Merge sort, and Insertion sort. Recall that the Big-O of Merge sort is O(nlogn) and the Big-O of Insertion sort is O(n 2 ). What advantage would Timsort have to combine the two algorithms if merge-sort has a better Big-O metric? (b) Consider two algorithms: f(n) and g(n). You run...
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons...
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons between elements is the following: Ωn log n in the worst case. you need to prove this , if you can please type answer.
What type of algorithm is the Quicksort algorithm if it has random pivots?
What type of algorithm is the Quicksort algorithm if it has random pivots?
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
Gale-Shapley: Prove or dissprove the following theorem In every execution of the hospitals-propose Stable Matching algorithm,...
Gale-Shapley: Prove or dissprove the following theorem In every execution of the hospitals-propose Stable Matching algorithm, there is at most one hospital that makes offers to every doctor. If anyone can help me with a long-form proof or an example that dissproves this claim!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT