Partially adopted from C Plus Data Structure textbook slides Basic Sorting Algorithms Selection sort select the current smallest to put in the current first position Bubble sort bubble small values up ID: 735727
Download Presentation The PPT/PDF document "13. Sorting Yan Shi CS/SE 2630 Lecture ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
13. Sorting
Yan ShiCS/SE 2630 Lecture Notes
Partially adopted from C++ Plus Data Structure textbook slidesSlide2
Basic Sorting Algorithms
Selection sort:select the current smallest to put in the current first position
Bubble sort:
bubble small values up
Insertion sort:
insert new items into sorted list
They are all
O(n
2
)
!
Use recursion to get more efficient sorting:
O(nlog
2
n)
Heap sort
Merge sort
Quick sortSlide3
Merge Sort
Basic idea: divide and conquer
divide the list into two parts
sort each half (recursively)
merge (combine) the two parts
[first] [middle] [middle + 1]
[last]
74 36 . . . 95
75 29 . . . 52
36 74 . . . 95
29 52 . . . 75 Slide4
Merge Sort Implementation
// Recursive merge sort algorithm
template <class
ItemType
>
void
MergeSort
(
ItemType
values[ ] ,
int
first ,
int last ) // Pre: first <= last
// Post: Array values[first..last] sorted into // ascending order.
{ if ( first < last ) // general case {
int middle = ( first + last ) / 2; MergeSort ( values, first, middle );
MergeSort( values, middle + 1, last );
// now merge two subarrays // values [ first . . . middle ] with
// values [ middle + 1, . . . last ].
Merge(values, first, middle, middle + 1, last); } }
// How to merge?Slide5
Merge
2 5 10 16 30
1 3 18 20
1
leftFirst
leftLast
rightFirst
rightLast
1 < 2
fill 1 and move
leftFirst
Slide6
Merge
2 5 10 16 30
1 3 18 20
1
leftFirst
leftLast
rightFirst
rightLast
3 > 2 fill 2 and move
rightFirst
2Slide7
Merge
2 5 10 16 30
1 3 18 20
1 2
leftFirst
leftLast
rightFirst
rightLast
3 < 5 fill 3 and move
leftFirst
3Slide8
Merge
2 5 10 16 30
1 3 18 20
1 2 3
leftFirst
leftLast
rightFirst
rightLast
18 > 5 fill 5 and move
rightFirst
5Slide9
Merge
2 5 10 16 30
1 3 18 20
1 2 3 5
leftFirst
leftLast
rightFirst
rightLast
18 > 10 fill 10 and move
rightFirst
10Slide10
Merge
2 5 10 16 30
1 3 18 20
1 2 3 5 10
leftFirst
leftLast
rightFirst
rightLast
18 > 16 fill 16 and move
rightFirst
16Slide11
Merge
2 5 10 16 30
1 3 18 20
1 2 3 5 10 16
leftFirst
leftLast
rightFirst
rightLast
18 < 30 fill 18 and move
leftFirst
18Slide12
Merge
2 5 10 16 30
1 3 18 20
1 2 3 5 10 16 18
leftFirst
leftLast
rightFirst
rightLast
20 < 30 fill 20 and move
leftFirst
leftFirst
>
leftLast
, stop
20Slide13
Merge
2 5 10 16 30
1 3 18 20
1 2 3 5 10 16 18 20
leftFirst
leftLast
rightFirst
rightLast
Append the remaining of the right part DONE!
30Slide14
Merge Algorithm
Given left array and right array, create a temp array of the size as the sum of left and right
while more items in left AND more items in right
if values[
leftFirst
] < values[
rightFirst
]temp[i] = values[leftFirst]; leftFirst++
elsetemp[i] = values[rightFirst]; rightFirst++
i++Copy any remaining item from the left half to temp
Copy any remaining item from the right half to tempCopy the sorted temp array back to values arrayIssue: need a second array to merge, more space consuming compared to quicksort and heapsortSlide15
Merge Sort Complexity
16
8 8
4 4 4 4
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
O(nlog
2
n)!Slide16
Quick Sort
Divide and conquer
! Can we divide a list into two pieces and sort both independently?
Basic
idea of algorithm:
select a value from the
list (
pivot)partition the list so that all smaller values are to the left and all larger values are to the right of the pivotsort left portion (recursively)sort right portionKey is splitting: organize the list as
values smaller (or equal to) pivot (split point)pivotvalues larger than pivotQuestion: Where to split?Slide17
Quick Sort Implementation
// Recursive quick sort algorithm
template <class
ItemType
>
void
QuickSort
( ItemType values[ ] ,
int first ,
int last ) // Pre: first <= last
// Post: Sorts array values[ first, last ] into ascending order{
if ( first < last ) // general case
{ int splitPoint
; Split ( values, first, last, splitPoint ) ;
// values [first]..values[splitPoint - 1] <= splitVal
// values [splitPoint] == splitVal
// values [splitPoint + 1]..values[last] > splitVal
QuickSort(values, first, splitPoint - 1); QuickSort
(values, splitPoint + 1, last); }
} ;
// How to decide
splitPoint?Slide18
Before
Split
values[first] [last]
We choose the first item as the pivot:
splitVal
= 9
GOAL: place
splitVal
in its proper position with
all values less than or equal to
splitVal
on its left
and all larger values on its right
9 20 6 18 14 3 60 11 Slide19
After
Split
values[first] [last]
splitVal
= 9
smaller values larger values
in left part in right part
6
3 9 18 14 20 60 11
splitVal in correct position
How do we do that?Slide20
Split: Iteration 1
9
20 6 18 14 3 60 11
splitVal
= 9
left
right
20 > 9, not on correct side
stop left
11 > 9, on correct side right --
60 > 9, on correct side right –
3 < 9, not on correct side stop right
9
20 6 18 14 3 60 11
left
right
left <= right, swap [left] and [right], move them toward each other
9
3
6 18 14
20
60 11
left
rightSlide21
Split: Iteration 2
9
3
6 18 14
20
60 11
splitVal
= 9
left
right
6 < 9, on correct side
left++
18 > 9, not on correct side stop left
14 > 9, on correct side
right--
18 > 9, on correct side right-- left > right, stop right
9
3 6 18 14 20 60 11
left
right
left > right, we stop the loop and make right the split point swap 9 and 6
6 3
9
18 14 20 60 11
split pointSlide22
Split Algorithm
void
Split(
ItemType
a
[],
int first, int last,
int & splitpt){
ItemType
splitVal = a[first]; // use first element as the pivot
int left = first + 1, right = last; do
{ while (left <= right && a[left] <= splitVal)
++left; while (left <= right && a[right] > splitVal) --right; if ( left < right) swap( a[left++], a[right--]);
} while (left <= right); splitpt = right;
swap(a[first], a[splitpt]);}Slide23
Let’s finish the quick sort
6 3
9
18 14 20 60 11
split point
3
6
11 14
18
60 20
3
11
14
20
60
14
20
3 6 9
11 14
18 20 60Slide24
Quick Sort of N elements:
How many comparisons?
N For first call, when each of N elements
is compared to the split value
2 * N/2 For the next pair of calls, when N/2
elements in each
“
half
”
of the original
array are compared to their own split values.4 * N/4 For the four calls when N/4 elements in each
“quarter” of original array are compared to their own split values.
. .
. HOW MANY SPLITS CAN OCCUR? Slide25
Quick Sort of N elements:
How many splits can occur?
It depends on the order of the original array elements!
If each split divides the
subarray
approximately in half,
there will be only
log
2
N
splits, and
QuickSort is O(N*log2
N).
But, if the original array was sortedwill split up the array into parts of unequal
length, with one part empty, and
the other part containing all the rest of the array except for split value itself.
In this case, there can be as many as N-1 splits, and QuickSort is O(N2
). solution: use the middle value as the split value and swap with the first value to begin with.Slide26
Sorting Objects
When sorting an array of objects we are manipulating references to the object, and not the objects themselves