Sei sulla pagina 1di 8

CS202

Quick Sort
Data Structures and
Algorithms „ In the bubble sort, consecutive items are
compared, and possibly exchanged, on each
pass through the list.
Quick sort & Merge sort „ This means that many exchanges may be
needed to move an element to its correct
By
position.
Ravindra De Silva
„ Quick sort is more efficient than bubble sort
because a typical exchange involves elements
that are far apart, so fewer exchanges are
Lecture 10 : 3rd August 2005
required to correctly position an element.

Quick Sort Quick Sort


„ Each iteration of the quick sort selects „ The sorting then continues by quick
an element, known as the pivot, and sorting the left partition followed by
divides the list into 3 groups: quick sorting the right partition.
„ Elements whose keys are less than (or „ The basic algorithm is as follows:
equal to) the pivot’s key.
„ The pivot element
„ Elements whose keys are greater than (or
equal to) the pivot’s key.

Quick Sort Quick Sort


1) Partitioning Step: Take an element in the „ Each time step 1 is performed on a
unsorted array and determine its final subarray, another element is placed in
location in the sorted array. This occurs its final location of the sorted array, and
when all values to the left of the element in
the array are less than (or equal to) the two unsorted subarrays are created.
element, and all values to the right of the „ When a subarray consists of one
element are greater than (or equal to) the element, that subarray is sorted.
element. We now have 1 element in its „ Therefore, that element is in its final
proper location and two unsorted subarrays.
location.
2) Recursive Step: Perform step 1 on each
unsorted subarray.

1
Quick Sort Quick Sort
„ There are several partitioning strategies „ Below is the array we would like to sort:
used in practice (i.e., several “versions”
of quick sort), but the one we are about 1 4 8 9 0 11 5 10 7 6
to describe is known to work well.

„ For simplicity we will select the last


element as the pivot element.

Quick Sort Quick Sort


„ The index left starts at the first element and „ We move left to the right, skipping over
right starts at the next-to-last element. elements that are smaller than the
pivot.
1 4 8 9 0 11 5 10 7 6
left right
1 4 8 9 0 11 5 10 7 6
„ We want to move all of the elements smaller
than the pivot to the left part of the array and
left right
all of the elements larger than the pivot to the
right part.

Quick Sort Quick Sort


„ We then move right to the left, skipping over „ If left is to the left of right (or if left =
elements that are greater than the pivot.
right), those elements are swapped.
1 4 8 9 0 11 5 10 7 6
left right 1 4 8 9 0 11 5 10 7 6
left right
„ When left and right have stopped, left is on an
element greater than (or equal to) the pivot
and right is on an element smaller than (or 1 4 5 9 0 11 8 10 7 6
equal to) the pivot.
left right

2
Quick Sort Quick Sort
„ Effectively, large elements are pushed 1 4 5 9 0 11 8 10 7 6
to the right and small elements are left right
pushed to the left.
„ We then repeat the process until left 1 4 5 9 0 11 8 10 7 6
and right cross.
left right

1 4 5 0 9 11 8 10 7 6
left right

Quick Sort Quick Sort


1 4 5 0 9 11 8 10 7 6
1 4 5 0 9 11 8 10 7 6
right left
left right
„ At this point, left and right have crossed
so no swap is performed.
„ The final part of the partitioning is to
1 4 5 0 9 11 8 10 7 6 swap the pivot element with left.
right left 1 4 5 0 6 11 8 10 7 9
right left

Quick Sort Quick Sort


„ Note that all elements to the left of the „ We now repeat the process using the
pivot are less than (or equal to) the pivot sub-arrays to the left and right of the
and all elements to the right of the pivot pivot.
are greater than (or equal to) the pivot.
1 4 5 0 6 11 8 10 7 9
„ Hence, the pivot element has been
placed in its final sorted position.

1 4 5 0 6 11 8 10 7 9
1 4 5 0 11 8 10 7 9
right left

3
Quick Sort Pseudocode Quick Sort Pseudocode (cont)
quicksort(list, leftMostIndex, rightMostIndex) {
// Find key on right that belongs on left
if (leftMostIndex >= rightMostIndex)
return loop (right >= leftMostIndex && list[right] > pivot)
end if right = right – 1
end loop Necessary if pivot is the
pivot = list[rightMostIndex] smallest element in the array.
left = leftMostIndex Make sure right // Swap out-of-order elements
right = rightMostIndex – 1 and left don’t cross if (left <= right) // Why swap if left = right?
swap(list, left, right)
loop (left <= right) left = left + 1
// Find key on left that belongs on right right = right – 1
loop (list[left] < pivot) Must account for special case of
end if
left = left + 1 list[left]=list[right]=pivot
end loop
end loop

Quick Sort Pseudocode (cont) Quick Sort


// Move the pivot element to its correct location „ A note about quick sort:
swap(list, left, rightMostIndex)
„ There are more optimal ways to choose
the pivot value (such as the median-of-
// Continue splitting list and sorting
three method).
quickSort(list, leftMostIndex, right)
quickSort(list, left+1, rightMostIndex)
}

Bubble Sort vs. Quick Sort Merge Sort


„ If we calculate the Big-O notation, we „ Idea:
find that (in the average case): „ Take the array you would like to sort and
„ Bubble Sort: O(n2) divide it in half to create 2 unsorted
„ Quick Sort: O(nlog2n) subarrays.
„ Next, sort each of the 2 subarrays.
„ Finally, merge the 2 sorted subarrays into
1 sorted array.
„ Efficiency: O(nlog2n)

4
Merge Sort Merge Sort
„ Although the merge step produces a
sorted array, we have overlooked a very
important step.
„ How did we sort the 2 halves
before performing the merge step?

We used merge sort!

Merge Sort Merge Sort


„ By continually calling the merge sort
algorithm, we eventually get a subarray
of size 1.
„ Since an array with only 1 element is
clearly sorted, we can back out and
merge 2 arrays of size 1.

Merge Sort Merge Sort


„ The basic merging algorithm consists „ The smaller of arrayA[indexA] and
of: arrayB[indexB] is copied into
„ 2 input arrays (arrayA and arrayB) arrayC[indexC] and the appropriate
„ An ouput array (arrayC) position holders are advanced.
„ 3 position holders (indexA, indexB, „ When either input list is exhausted, the
indexC), which are initially set to the remainder of the other list is copied into
beginning of their respective arrays. arrayC.

5
Merge Sort Merge Sort
1 13 24 26 We compare arrayA[indexA] 1 13 24 26
arrayA with arrayB[indexB]. arrayA
2 < 13 so we insert
indexA Whichever value is smaller is indexA arrayB[indexB] into
placed into arrayC[indexC]. arrayC[indexC]
arrayB 2 15 27 38 arrayB 2 15 27 38
1 < 2 so we insert
indexB arrayA[indexA] into indexB
arrayC[indexC]

arrayC arrayC
1
indexC indexC

Merge Sort Merge Sort


arrayA 1 13 24 26 arrayA 1 13 24 26
13 < 15 so we insert 15 < 24 so we insert
indexA arrayA[indexA] into indexA arrayB[indexB] into
arrayC[indexC] arrayC[indexC]
arrayB 2 15 27 38 arrayB 2 15 27 38
indexB indexB

arrayC
1 2 arrayC
1 2 13
indexC indexC

Merge Sort Merge Sort


arrayA 1 13 24 26 arrayA 1 13 24 26
24 < 27 so we insert 26 < 27 so we insert
indexA arrayA[indexA] into indexA arrayA[indexA] into
arrayC[indexC] arrayC[indexC]
arrayB 2 15 27 38 arrayB 2 15 27 38
indexB indexB

arrayC
1 2 13 15 arrayC
1 2 13 15 24
indexC indexC

6
Merge Sort Merge Sort
arrayA 1 13 24 26 arrayA 1 13 24 26
Since we have exhausted
one of the arrays, arrayA,
we simply copy the
arrayB 2 15 27 38 remaining items from the arrayB 2 15 27 38
other array, arrayB, into
indexB arrayC

1 2 13 15 24 26 arrayC
1 2 13 15 24 26 27 38
arrayC
indexC

Merge Sort Pseudocode Merge Sort Pseudocode (cont)


mergesort(list, first, last) { merge(list, first, mid, last) {
if( first < last )
// Initialize the first and last indices of our subarrays
mid = (first + last)/2;
indexA = first
// Sort the 1st half of the list
mergesort(list, first, mid); lastA = mid
// Sort the 2nd half of the list indexB = mid+1
mergesort(list, mid+1, last); lastB = last
// Merge the 2 sorted halves
merge(list, first, mid, last); indexC = indexA // Index into our temp array
end if
}

Merge Sort Pseudocode (cont) Merge Sort Pseudocode (cont)


// At this point, one of our subarrays is empty
// Start the merging // Now go through and copy any remaining items
// from the non-empty array into our temp array
loop( indexA <= lastA AND indexB <= lastB ) loop (indexA <= lastA)
if( list[indexA] < list[indexB] ) tempArray[indexC] = list[indexA]
tempArray[indexC] = list[indexA] indexA = indexA + 1
indexA = indexA + 1 indexC = indexC + 1
end loop
else loop (indexB <= lastB )
tempArray[indexC] = list[indexB] tempArray[indexC] = list[indexB]
indexB = indexB + 1 indexB = indexB + 1
end if indexC = indexC + 1
end loop
indexC = indexC + 1;
end loop

7
Merge Sort Pseudocode (cont) Efficiency Summary
// Finally, we copy our temp array back into
Sort Worst Case Average Case
// our original array
indexC = first Insertion O(n2) O(n2)
loop (indexC <= last) Shell (did not cover) O(n1.5) O(n1.25)
list[indexC] = tempArray[indexC] Selection O(n2) O(n2)
indexC = indexC + 1 (did not cover)
end loop
Bubble O(n2) O(n2)
}
Quick O(n2) O(nlog2n)
Heap (did not cover) O(nlog2n) O(nlog2n)
Merge O(nlog2n) O(nlog2n)

Potrebbero piacerti anche