Access over 20 million homework & study documents
search

Consider the following brute force algorithm for finding the distan

Content type

User Generated

Rating

Showing Page:
1/4
Consider the following brute - force algorithm for finding
the distance between the two closest elements in an array
of numbers. Algorithm Min Distance (A[0..n -
1])//Input:Array A[0..n - 1] of numbers//Output:Minimum
distance between two of its elements d min leftarrow
infinity for i leftarrow 0 to n - 1 do for j leftarrow 0 to n - 1
do if i notequalto j and |A[i] - A[j]|
Solution
a)
Adding quick sort algorithm to this brute force algorithm to
increase performance...
// Partition function..
partition(left, right, pivotEle){
// variables tp hold both extremem ends
leftPtr left-1;
rightPtr = right;
while(true)
while(A[++leftPtr] < pivot){
// nothing doing
}
while(rightPtr > 0 && A[-rightPtr] > pivotEle){

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
// nothing doing
}
if leftPtr >= rightPtr
break
else
// swapping if elenment is greater than neigbour
element
swap leftPtr,rightPtr
end if
end while
swap leftPtr,right
return leftPtr
}
procedure qSort(A[0..n-1], left, right){
if right-left <= 0
return
else {
pivotEle = A[right]
partition = partition(left, right, pivotEle)
qSort(left,partition-1)
qSort(partition+1,right)
}
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 4 pages?
Access Now

Unformatted Attachment Preview

Consider the following brute - force algorithm for finding the distance between the two closest elements in an array of numbers. Algorithm Min Distance (A[0..n 1])//Input:Array A[0..n - 1] of numbers//Output:Minimum distance between two of its elements d min leftarrow infinity for i leftarrow 0 to n - 1 do for j leftarrow 0 to n - 1 do if i notequalto j and |A[i] - A[j]| Solution a) Adding quick sort algorithm to this brute force algorithm to increase performance... // Partition function.. partition(left, right, pivotEle){ // variables tp hold both extremem ends leftPtr left-1; rightPtr = right; while(true) while(A[++leftPtr] < pivot){ // nothing doing } while(rightPtr > 0 && A[-rightPtr] > pivotEle){ // nothing doing } if leftPtr >= rightPtr break else // swapping if elenment is greater than neigbour element swap leftPtr,rightPtr end if end while swap leftPtr,right return leftPtr } procedure qSort(A[0..n-1], left, right){ if right-left ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Just the thing I needed, saved me a lot of time.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4