Question

In: Computer Science

Given the unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] P E R...

Given the unordered array:

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

P

E

R

Y

I

H

J

L

S

Suppose this array were being sorted using the quick sort algorithm from the course content,

into ASCENDING order, with the left-most item as the pivot value.

List the letters in the resulting array, in order AFTER the FIRST PARTITIONING.

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

Solutions

Expert Solution

Code used:
   private static int partition(int start, int end) {
       char pivot = a[start];

       int p1 = start + 1;

       for (int i = start + 1; i <= end; i++) {
           if (a[i] < pivot) {
               swap(i, p1);
               p1++;
           }
       }

       a[start] = a[p1 - 1];
       a[p1 - 1] = pivot;

       return p1 - 1;
   }


original array: [P, E, R, Y, I, H, J, L, S]
Swapping E and E
[P, E, R, Y, I, H, J, L, S]
Swapping I and R
[P, E, I, Y, R, H, J, L, S]
Swapping H and Y
[P, E, I, H, R, Y, J, L, S]
Swapping J and R
[P, E, I, H, J, Y, R, L, S]
Swapping L and Y
[P, E, I, H, J, L, R, Y, S]

Final array:

[0]L

[1]E

[2]I

[3]H

[4]J

[5]P

[6]R

[7]Y

[8]S


Related Solutions

1a)Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. What is...
1a)Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. What is the running time of the insertion sort if both A[1..n/2] and A[n/2+1,n] are also sorted. Justify your answer. 2-illustrate the operation of RADIX-SORT on the following list of English words: COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, TEA, NOW, FOX. 3-Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4, 3
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
Given: x y -5 1 -4 5 -3 4 -2 7 -1 10 0 8 1...
Given: x y -5 1 -4 5 -3 4 -2 7 -1 10 0 8 1 9 2 13 3 14 4 13 5 18 What are the confidence limits (alpha = 0.05) for the true mean value of Y when X = 3?
Correct the code to prints the following: 0 1 2 3 4 5 6 7 8...
Correct the code to prints the following: 0 1 2 3 4 5 6 7 8 9 int[] numbers = new int[10]; for(int i=0; i < numbers.length; ++i)         numbers[i] = i * 3; for(int i=0; i < numbers.length; ++i)         System.out.print(numbers[i] / 2 + 1 + " "); System.out.println();
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5 4; 9 8 7] Note: TA2 is defined to be a linear transformation that maps any vector x to A2* x. That is TA2 = A2*x. Also the range of the Linear transformation represented by A2 is the same as the column space of A2. l) Find a basis for the null(TA2). m) Find nullity of A2, TA2 and A2tA2. n) Find rank(A2), rank(A2t),...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
Given the vectors d = [−3, 6, 7] e = [8, −5, 3] and f =...
Given the vectors d = [−3, 6, 7] e = [8, −5, 3] and f = [3, −1, 4] a. Let x1(t) be the equation between d and f, express x1(t) in vector and parametric form. Then find x1(3); x1(-2); x1(1) b. Let x2(t) be the equation through f and parallel to x1(t), express x2(t) in vector and parametric form c. Let g = 4d – 2f, express the equation of the line containing g and e
Find regression line for the data X 0   1   2   3    4   5   6   7  8          &nbsp
Find regression line for the data X 0   1   2   3    4   5   6   7  8               [3 MARKS] Y 11 21 31 41 51 61 71 81 91 b. X  0   2   4   6   8  10                            [3 MARKS]       Y  12 15 17 18 20 22
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C,...
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. How many hexadecimal strings of length twelve have five A’s and five B’s? How many hexadecimal strings of length twelve have at most three E’s? How many hexadecimal strings of length twelve have exactly three A’s and at least two B’s? How many hexadecimal strings of length twelve have exactly two A’s and exactly two B’s, so that the two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT