Question

In: Computer Science

The quick sort algorithm always divides the list into two equal sized sublists, then sorts each...

The quick sort algorithm always divides the list into two equal sized sublists, then sorts each sublists, and then combines both sublists.. True of False

Solutions

Expert Solution

  • Below is the detailed explanation of the problem based on quick sort mentioned above.
  • Correct answer is False
  • Explanation:

In quick sort algorithm our main aim is to partition the given list around a pivot(which can be selected in different ways consider the first element as pivot for simplicity) and then place the pivot at it's correct position in sorted list and the elements in left side of pivot are smaller than it and on the right side are greater than it.And finally call quicksort for let and right part.

So basically what we do in quick sort is that we try to position the pivot and then call for left and right part hence we get the sorted list.

In quick sort algorithm, list is not always partitioned into two equal sized sublist as consider the case when list is sorted initially and we are applying quick sort algorithm to it by taking the pivot element as the first element, In worst case quick sort will call for two subproblems one for size 0 and the other for size n-1, so the recurrence relation becomes T(n)=T(n-1) + T(0) + O(n)(this extra work for partitioning), so this kind of case results in O(n2) complexity of algorithm.

Normally if list is partitioned into two equal sized lists then recurrence is T(n)=T(n/2) + T(n/2) + O(n) which results in complexity of O(nlogn).

  • Algorithm(taking first element as pivot):

#function which sorts a list A with l as starting index and r as ending index+1
def quickSort(A, l, r):
#base case
#if there are at most one element in the list
if r-l<=1:
return()
#considering A[l] as pivot element , do the partitioning
pointer1=l+1
for pointer2 in range(l+1,r):
if A[pointer2] <= A[l]:
(A[pointer1], A[pointer2])=(A[pointer2], A[pointer1])
pointer1=pointer1+1

#now after partitioning move the pivot into its place
(A[l], A[pointer1-1])=(A[pointer1-1], A[l])
#and call recursively for both the parts
quickSort(A,l,pointer1-1)
quickSort(A,pointer1,r)

  • So we can see now that in quick sort we do not divide the list into two equal sized sublist then sort each sublist and then combines both sublists. Actually this is known as merge sort algorithm which is also a divide and conquer(similar to quicksort algorithm) algorithm but works differently than quick sort.

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


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.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
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       ...
Bucket sort is an algorithm that falls into the category of "specialized" sorts that have certain...
Bucket sort is an algorithm that falls into the category of "specialized" sorts that have certain advantages but rely on properties of the array that may not be suitable for all situations. The algorithm itself is simple and uses an array of counters. Given an array of size n, the pseudocode is: Initialize an array of integers of size range(n), bucketCount[], all set to 0 For i in each array element 0 ... n increase bucketCount[array[i]] by 1 range(n) is...
Provide two possible ways to go beyond randomized quick sort to enhance the algorithm to a...
Provide two possible ways to go beyond randomized quick sort to enhance the algorithm to a better running time and implement in Java one of the ways you provide (add few comments to explain your code)
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT