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, 22.06.2019 15:00
When designing content as part of your content marketing strategy, what does the "think" stage represent in the "see, think, do, care" framework?
Answers: 3
question
Computers and Technology, 22.06.2019 19:00
The fourth generation of computers emerged between 1970s and 1980s. which technological advancement brought about this generation of computers? which computer architecture was used most in this generation?
Answers: 3
question
Computers and Technology, 23.06.2019 16:00
Does read theory have answers keys ?
Answers: 1
question
Computers and Technology, 24.06.2019 01:00
Mastercard managers are motivated to increase (1) the number of individuals who have and use a mastercard credit card, (2) the number of banks and other clents who issue mastercards to customers and/or employees, and (3) the number of locations that accept mastercard payments. discuss how mastercard could use its data warehouse to it expand each of these customer bases.
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
Mathematics, 08.06.2020 17:57
question
World Languages, 08.06.2020 17:57
Questions on the website: 13722367