Question

In: Computer Science

Using an example of 6 integer elements, Show how quick sort can be done. [4 Marks]...

  1. Using an example of 6 integer elements,

    1. Show how quick sort can be done. [4 Marks]

  1. Describe the worst case scenario of quick sort [2 Marks]

  2. In your answer at d (i), show what makes quick sort categorized as divide and conquer

[2 marks]

Solutions

Expert Solution

Consider the following array: 45,20,5,15,55,30 We need to sort this array in the most efficient manner without using extra place (inplace sorting).

Solution

Step 1:

  • Make any element as pivot: Decide any value to be the pivot from the list. For convenience of code, we often select the rightmost index as pivot or select any at random and swap with rightmost. Suppose for two values “Low” and “High” corresponding to the first index and last index respectively.
    • In our case low is 0 and high is 5.
    • Values at low and high are 45 and 30 and value at pivot is 30.
  • Partition the array on the basis of pivot: Call for partitioning which rearranges the array in such a way that pivot (30) comes to its actual position (of the sorted array). And to the left of the pivot, the array has all the elements less than it, and to the right greater than it.
    • In the partition function, we start from the first element and compare it with the pivot. Since 45 is greater than 30, we don’t make any change and move on to the next element 20.
    • Compare again with the pivot. Since 20 is less than 30, we swap 45 and 20. The array becomes 20, 45, 5, 15, 55, 30
    • We move on to the next element 9 which is again less than pivot (30) thus swapping it with 50 makes our array as 20, 5, 45, 15, 55, 30.
    • Similarly, for next element 15 which is less than 30, the array becomes 20, 5, 15, 45, 55, 30. Now 55 is greater than pivot (30), hence no changes.
    • Lastly, we swap our pivot with 45 so that it comes to the correct position.

Thus the pivot (30) comes at its actual position and all elements to its left are lesser, and all elements to the right are greater than itself.

Step 2: The main array after the first step becomes

20, 5, 15, 30, 55, 45

Step 3: Now the list is divided into two parts:

  1. Sublist before pivot element
  2. Sublist after pivot element

Step 4: Repeat the steps for the left and right sublists recursively. The final array thus becomes
5, 15, 20, 30, 45, 55

Worst case scenario: This happens when we encounter the most unbalanced partitions possible, then the original call takes n time, the recursive call on n-1 elements will take (n-1) time, the recursive call on (n-2) elements will take (n-2) time, and so on. The worst case time complexity of Quick Sort would be O(n2).

  • Its work by selecting a pivot element from the array and partitioning the other elements into sub arrays, whether they are smaller or greater than the pivot. The the sub array are sorted recursively. That is why it came under divide and conqer approach.

Related Solutions

use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
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.
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 the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
* 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...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
6. The worst case scenario in the quick sort occurs when the array is partitioned to...
6. The worst case scenario in the quick sort occurs when the array is partitioned to two equal sized subarray every time that a recursive call takes place. True False 7.Suppose that we want to sort an array of n elements, where each element is a string of at most 1000 characters. What is the time requirement for applying radix sort to sort this array? O(n2) O(1000n) O(l000logn) O(nlogn) 8.Suppose we want to sort the following array of integers using...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT