Question

In: Computer Science

Compute the first 4 pivots using quick sort algorithm on an array A=<10,15,17,18,6,120,30,250,95>. Write the content...

Compute the first 4 pivots using quick sort algorithm on an array A=<10,15,17,18,6,120,30,250,95>. Write the content of the array A after each pivot is chosen.

Solutions

Expert Solution

In case of any queries, please revert back. I will solve them ASAP.

A={10,15,17,18,6,120,30,250,95}

Lets say we always choose the last element as the pivot.

What are the steps?

1. Choose Pivot

2. All elements smaller than pivot are left to pivot.

3. All elements larger than the pivot are right to pivot.

4. Repeat for left and right half

Order of elements does not change.

============== 1ST PIVOT =================

Pivot Chosen is 95 ie last element.

A = {10,15,17,18,6,30,95,120,250}

This is the array after the first pivot.

============== 2ND PIVOT =================

Pivot Chosen is 30 ie last element of left part!

A = {10,15,17,18,6,30,95,120,250}

This is the array after the second pivot. 30 was at the correct position.

=============== 3RD PIVOT ===================

Pivot Chosen is 6 ie last element of left part of 30.

A = {6,10,15,17,18,30,95,120,250}

This is the array after the third pivot. Here 6 was smallest so all elements moved to right.

==============4th PIVOT =====================

Now 6 has not left part. So we start with right part to move up the recursion. So, Pivot choosen is 18.

A = {6,10,15,17,18,30,95,120,250}

This is the array after the fourth pivot. 18 was at the correct postion as there is no greater element than 18.

If we move forward we see that 17,15 and 10 are at the correct positon! These will be 5th,6th and 7th pivot.

A = {6,10,15,17,18,30,95,120,250}

Now, recursion moves forward and checks right of 95.

So, 120 and 250 are at correct position

A = {6,10,15,17,18,30,95,120,250}


Related Solutions

Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e.,...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e., from pass 1 to pass 7). Each pass is defined as the completion of one partition. We always pick the first array element as the pivot for each partition.
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
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.
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
Consider the quick sort algorithm. The quick sort algorithm is a divide and conquer approach which...
Consider the quick sort algorithm. The quick sort algorithm is a divide and conquer approach which chooses a pivot value and divides the subarrays as the lower values on the left and the bigger values on the right (comparing them to pivot). Then, each subarray is processed similarly until the array becomes sorted. Quick sort algorithm has an average time complexity of O(nlogn). However, if the array to be used in the quicksort is already sorted, then the complexity becomes...
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
●In this task, the quick sort algorithm selects the first element in the list as the...
●In this task, the quick sort algorithm selects the first element in the list as the pivot. Revise it by selecting the median among the first, middle, and last elements in the list. ● Write the algorithm for searching for entries using linear probing. ● Write the algorithm for removing entries using linear probing. ● Create a diagram similar to the one above that shows the hash table of size 11 after entries with the keys 34, 29, 53, 44,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
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