Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
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
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
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?
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
arrayC
1 2 arrayC
1 2 13
indexC indexC
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
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)