Question

In: Computer Science

trace by using quicksort on the array : 4 6 5 3 2 7 1 using...

trace by using quicksort on the array : 4 6 5 3 2 7 1 using 7 as the pivot

Solutions

Expert Solution

Steps involved in quick sort are: -

  1. Choose a pivot element.(in this case, 7)
  2. Initialize left and right pointers at extremes.
  3. Starting at the left pointer and moving to the right, find the first element which is greater than or equal to the pivot value.
  4. In the same way, starting at the right pointer and moving to the left, find the first element, which is smaller than pivot value
  5. Swap elements found in 3 and 4.
  6. Repeat steps 3,4,5 until left pointer is greater or equal to right pointer.
  7. Repeat the whole thing for the two subarray to the left and the right of the left pointer.

Now, The given array is {4,6,5,3,2,7,1}

pivot element is 7, left pointer is at 4 and right pointer is at 1.

Now, first element from left which is greater than or equal to 7 is 7 itself and 1st element from right which is smaller than 7 is 1. so left pointer is at 7 and right pointer is at 1. So, we swap them and array becomes

4,6,5,3,2,1,7

Now our pivot element is at it's right place in when the array is sorted. All elements before 7 are smaller than 7.


Related Solutions

Given an array with data 3, 6, 4, 1, 5, 2, 6, 5, 3, 7, 4 using random select to find the 9 th smallest number
Given an array with data 3, 6, 4, 1, 5, 2, 6, 5, 3, 7, 4 using random select to find the 9 th smallest number (use the last element in each sequence as pivot). Show the intermediate steps (the result of each recursive step including the pivot, k’s value and grouping).
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5...
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5 8 7 6 5 7 7 6 7 8 8 8 9 7 8 10 12 11 Test the significance of the correlation coefficient. Then use math test scores (X) to predict physics test scores (Y).  Do the following: Create a scatterplot of X and Y. Write the regression equation and interpret the regression coefficients (i.e., intercept and slope). Predict the physics score for each....
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
4) Let ? = {2, 3, 5, 7}, ? = {3, 5, 7}, ? = {1,...
4) Let ? = {2, 3, 5, 7}, ? = {3, 5, 7}, ? = {1, 7}. Answer the following questions, giving reasons for your answers. a) Is ? ⊆ ?? b)Is ? ⊆ ?? c) Is ? ⊂ ?? d) Is ? ⊆ ?? e) Is ? ⊆ ?? 5) Let ? = {1, 3, 4} and ? = {2, 3, 6}. Use set-roster notation to write each of the following sets, and indicate the number of elements in...
6. Let A = {1, 2, 3, 4} and B = {5, 6, 7}. Let f...
6. Let A = {1, 2, 3, 4} and B = {5, 6, 7}. Let f = {(1, 5),(2, 5),(3, 6),(x, y)} where x ∈ A and y ∈ B are to be determined by you. (a) In how many ways can you pick x ∈ A and y ∈ B such that f is not a function? (b) In how many ways can you pick x ∈ A and y ∈ B such that f : A → B...
Step 2 Data Set A x 1 2 3 4 5 6 7 y 7 7...
Step 2 Data Set A x 1 2 3 4 5 6 7 y 7 7 7 9 9 9 10 Data Set B x 1 2 3 4 5 6 7 8 9 10 11 y 4 6 6 6 8 9 9 9 10 10 10 Step 2 Find the equation for the least-squares line, and graph the line on the scatter plot. Find the sample correlation coefficient r and the coefficient of determination r2. Is r significant?...
Trace the execution of quicksort on the following array, assuming that the first item in each...
Trace the execution of quicksort on the following array, assuming that the first item in each subarray is the pivot value. Show the values of first and last for each recursive call and the array elements after returning from each call. Also, show the value of pivot during each call and the value returned through pivIndex. How many times is sort called, and how many times is partition called? 55 50 10 40 80 90 60 100 70 80 20...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7 9 7 4 6 9 9 -5.48x + 0.17 5.48x + 0.17 -0.17x + 5.48 0.17x + 5.48
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question 's scores, If three points were added to every score in this distribution, what would be the new mean? If three points were added to every score in this distribution, what would be the new standard deviation. Remember, you have already calculated population standard deviation in a previous problem. This problem requires two answers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT