subject

Consider the Partition algorithm we discussed in class. Partition(A)
pivot = A[0], i = 1, j = A. length-1; // Let the leftmost element be the pivot
while i<=j // Rearrange elements
while i < A. length-1 & A[i] < pivot,
i = i + 1
end while
while j > 0 & A[j] >= pivot,
j = j - 1
end while
if i >= j
break
end if
swap A[i] and A[j]
end while
swap A[0] and A[j]
return j; // Return the index of pivot after the partition
Upon completion of the Partition algorithm, the pivot element would be placed in the middle, elements less than pivot are placed on the left of pivot, and elements greater than or equal to pivot are placed on the right of pivot.
Given a list (A) of n elements, outline an O(n) time in-space algorithm based on the Partition algorithm such that upon completion of the algorithm, elements equal to the pivot (including the pivot element) would be placed in the middle, elements less than pivot are placed on the left of pivot, and elements greater than pivot are placed on the right of pivot.
For example, let A be {1, 2, 2, 2, 6, 1, 7, 0, -5, 2, 8, 1, 3, 1, 1}.
Let the first element 1 be the pivot.
If you call the original Partition method on list A, A would become:
{0, -5, 1, 2, 6, 1, 7, 2, 2, 2, 8, 1, 3, 1, 1}.
This problem asks you to design an algorithm, such that list A would become
{0, -5, 1, 1, 1, 1, 1, 2, 6, 7, 2, 2, 2, 8, 3}, such that the pivot 1 and all the other 1’s would be placed in the middle of the array, elements less than 1 would be on the left of all 1’s, and elements greater than 1 would be on the right of all 1’s.
Full credit (10 points) will be awarded for an algorithm that is O(n) and in-place. Algorithms that are NOT in-place or O(nlogn) or slower will be scored out of 3 points.
Note: In-place means no new arrays or extra data structure can be used.
a) describe the idea behind your algorithm in English
b) provide pseudocode
c) analyze its running time

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 19:20
Number of megabytes of ram in a computer. qualitative or quantitative? because
Answers: 3
question
Computers and Technology, 23.06.2019 16:10
What is the ooh? a. omaha occupation handbook b. online occupational c. occupations online d. occupational outlook handbook select the best answer from the choices provided
Answers: 3
question
Computers and Technology, 25.06.2019 05:00
Brad wants to buy flowers for his friend with 33 dollars.the daisies are 1 dollar each and the roses are 2 dollars each he buy 3 more daisies than roses how much did the roses cost
Answers: 2
question
Computers and Technology, 25.06.2019 08:10
Why should the technician watch for signs of contamination in the oil when recovering refrigerant from a system
Answers: 3
You know the right answer?
Consider the Partition algorithm we discussed in class. Partition(A)
pivot = A[0], i = 1, j...
Questions
question
English, 24.08.2019 04:30
question
Mathematics, 24.08.2019 04:30