Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the...
-
Upload
claire-marshall -
Category
Documents
-
view
216 -
download
0
Transcript of Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the...
![Page 1: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/1.jpg)
Sorting
SortingArranging items in a collection so that there is an ordering on one (or more) of the fields in the itemsSort KeyThe field (or fields) on which the ordering is basedSorting algorithmsAlgorithms that order the items in the collection based on the sort key
Why is sorting important?
![Page 2: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/2.jpg)
Chapter 13 presents several common algorithms for sorting an array of integers.
Two slow but simple algorithms are Selectionsort and Insertionsort.
This presentation demonstrates how the two algorithms work.
Quadratic Sorting
Data Structuresand Other ObjectsUsing C++
![Page 3: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/3.jpg)
Selection Sort
Given a list of names, put them in alphabetical order Find the name that comes first in the alphabet,
and write it on a second sheet of paper
Cross out the name off the original list
Continue this cycle until all the names on the original list have been crossed out and written onto the second list, at which point the second list contains the same items but in sorted order
![Page 4: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/4.jpg)
Selection Sort
A slight adjustment to this manual approach does away with the need to duplicate space
As you cross a name off the original list, a free space opens up
Instead of writing the value found on a second list, exchange it with the value currently in the position where the crossed-off item should go
![Page 5: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/5.jpg)
Selection Sort
Figure 9.9 Example of a selection sort (sorted elements are shaded)
![Page 6: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/6.jpg)
Sorting an Array of Integers
The picture shows an array of six integers that we want to sort from smallest to largest [1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]
![Page 7: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/7.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
Start by finding the smallest entry.
[0] [1] [2] [3] [4] [5]
![Page 8: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/8.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
Start by finding the smallest entry.
Swap the smallest entry with the first entry. [0] [1] [2] [3]
[4] [5]
![Page 9: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/9.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
Start by finding the smallest entry.
Swap the smallest entry with the first entry. [0] [1] [2] [3]
[4] [5]
![Page 10: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/10.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selectionsort Algorithm
Part of the array is now sorted.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 11: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/11.jpg)
The Selectionsort Algorithm
Find the smallest element in the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 12: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/12.jpg)
The Selectionsort Algorithm
Find the smallest element in the unsorted side.
Swap with the front of the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 13: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/13.jpg)
The Selectionsort Algorithm
We have increased the size of the sorted side by one element.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 14: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/14.jpg)
The Selectionsort Algorithm
The process continues...
Sorted side Unsorted side
Smallestfrom
unsorted
[0] [1] [2] [3] [4] [5]
![Page 15: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/15.jpg)
The Selectionsort Algorithm
The process continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
![Page 16: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/16.jpg)
The Selectionsort Algorithm
The process continues...
Sorted side Unsorted sideSorted side
is bigger
[0] [1] [2] [3] [4] [5]
![Page 17: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/17.jpg)
The Selectionsort Algorithm
The process keeps adding one more number to the sorted side.
The sorted side has the smallest numbers, arranged from small to large.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
![Page 18: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/18.jpg)
The Selectionsort Algorithm
We can stop when the unsorted side has just one number, since that number must be the largest number.
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
![Page 19: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/19.jpg)
The Selectionsort Algorithm
The array is now sorted.
We repeatedly selected the smallest element, and moved this element to the front of the unsorted side.
[0] [1] [2] [3] [4] [5]
![Page 20: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/20.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
The Insertionsort algorithm also views the array as having a sorted side and an unsorted side.
[0] [1] [2] [3] [4] [5]
![Page 21: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/21.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
The sorted side starts with just the first element, which is not necessarily the smallest element.
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
![Page 22: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/22.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
The sorted side grows by taking the front element from the unsorted side...
Sorted side Unsorted side
cur
i=1
for (int i = 1; i < v.size(); i++) { int cur = v[i]; // slide cur down into position to left
![Page 23: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/23.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
...and inserting it in the place that keeps the sorted side arranged from small to large.
Sorted side Unsorted side
j=i-1 i=1
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];cur
![Page 24: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/24.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
In this example, the new element goes in front of the element that was already in the sorted side.
Sorted side Unsorted side
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];v[j+1] = cur;
j--j+1=0
![Page 25: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/25.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
Sometimes we are lucky and the new inserted item doesn't need to move at all.
Sorted side Unsorted side
for (int i = 1; i < v.size(); i++) { int cur = v[i]; // slide cur down into position to left
![Page 26: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/26.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Insertionsort Algorithm
Sometimes we are lucky twice in a row.
Sorted side Unsorted side
v[j+1] = cur;
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 27: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/27.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
Copy the new element to a separate location.
Sorted side Unsorted side
cur
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 28: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/28.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
Shift elements in the sorted side, creating an open space for the new element.
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 29: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/29.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
Shift elements in the sorted side, creating an open space for the new element.
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 30: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/30.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
Continue shifting elements...
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 31: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/31.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
Continue shifting elements...
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 32: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/32.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
...until you reach the location for the new element.
for (int j=i-1; j >= 0 && v[j] > cur; j--) v[j+1] = v[j];
![Page 33: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/33.jpg)
[1] [2] [3] [4] [5] [6]0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
How to Insert One Element
Copy the new element back into the array, at the correct location.
Sorted side Unsorted side
v[j+1] = cur;
![Page 34: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/34.jpg)
How to Insert One Element
The last element must also be inserted. Start by copying it...
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
![Page 35: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/35.jpg)
Bubble Sort
Bubble Sort uses the same strategy:Find the next itemPut it into its proper place
But uses a different scheme for finding the next item Starting with the last list element, compare
successive pairs of elements, swapping whenever the bottom element of the pair is smaller than the one above it
![Page 36: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/36.jpg)
Bubble Sort
Figure 9.10 Example of a bubble sort
![Page 37: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/37.jpg)
Algorithms
Can you write the algorithms for the selection sort and the bubble sort?
Can you think of a way to make the bubble sort more efficient?
![Page 38: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/38.jpg)
Logarithmic Sorting
* Quick Sort* Merge Sort
![Page 39: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/39.jpg)
Quicksort
Figure 9.12 Ordering a list using the Quicksort algorithm
It is easier to sort a smallernumber of items: Sort A…F, G…L, M…R, and S…Z andA…Z is sorted
![Page 40: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/40.jpg)
Quicksort
QuicksortIf (there is more than one item in list[first]..list[last])
Select splitValSplit the list so that
list[first]..list[splitPoint-1] <= splitVallist[splitPoint] = splitVallist[splitPoint+1]..list[last] > splitVal
Quicksort the left halfQuicksort the right half
![Page 41: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/41.jpg)
Quicksort
![Page 42: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/42.jpg)
Quicksort
Split Set left to first + 1Set right to lastDo
Increment left until list[left] > splitVal OR left > rightDecrement right until list[right] < splitVal OR left > right If (left < right)
Swap list[left] and list[right]While (left <= right)Set splitPoint to rightSwap list[first] and last[right]
![Page 43: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/43.jpg)
Quicksort
Figure 9.13 Splitting algorithm
![Page 44: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/44.jpg)
Quick Sort Code
void Quicksort(Vector<int> &v, int start, int stop) {
if (stop > start) { //base case int pivot = Partition(v, start, stop); //partition Quicksort(v, start, pivot-1); //recursive sort left Quicksort(v, pivot+1, stop);//recursive sort right } }
![Page 45: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/45.jpg)
Partition Code – set up pivot
int Partition(vector<int> & arr, int start, int stop) { int lh = start + 1; //left hand int rh = stop; //right hand int pivot; //variable to hold pivot pivot = arr[start]; //set pivot to first element
![Page 46: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/46.jpg)
Partition Code—Move to center
while(true) { while(lh < rh && arr[rh] >= pivot) rh--; while(lh <rh && arr[lh] < pivot) lh++; if(lh == rh) break; //base case
![Page 47: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/47.jpg)
Partition Code—Swap
while(true) { while(lh < rh && arr[rh] >= pivot) rh--; while(lh <rh && arr[lh] < pivot) lh++; if(lh == rh) break; //base case swap(arr[lh], arr[rh]); }
![Page 48: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/48.jpg)
Partition Code—Left Overs?
if(arr[lh] >= pivot) return start; swap(arr[start], arr[lh]; return lh;
} //end Partition function
![Page 49: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/49.jpg)
Merge Sort—Partition
8 7 4 3 9 5 10
![Page 50: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/50.jpg)
Merge Sort—Partition
8 7 4 3
9 5 10
![Page 51: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/51.jpg)
Merge Sort—Partition
8 7
9 5 4 3 10
![Page 52: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/52.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare -- smallest goes first
![Page 53: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/53.jpg)
Merge Sort—Merge
8
9 4 10
7
3
5
Compare -- smallest goes first
![Page 54: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/54.jpg)
Merge Sort—Merge
8
9 4 10
7
3
5
Compare -- smallest goes first
![Page 55: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/55.jpg)
Merge Sort—Merge
8
9 4 10
7
3
5
Compare -- smallest goes first
![Page 56: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/56.jpg)
Merge Sort—Merge
8
9 4
107
3
5
Compare -- smallest goes first
![Page 57: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/57.jpg)
Merge Sort—Merge
8
9 4
107
3
5
Compare -- smallest goes first
![Page 58: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/58.jpg)
Merge Sort—Merge
8
9
4 107
3
5
Compare -- smallest goes first
![Page 59: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/59.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare -- smallest goes first
![Page 60: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/60.jpg)
Merge Sort—Merge
8
9 4 107
3
5
![Page 61: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/61.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest goes firstCompare—smallest goes first
Choose smallest from each stack
![Page 62: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/62.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest goes firstCompare—smallest goes first
Choose smallest from each stack
![Page 63: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/63.jpg)
Merge Sort—Merge
8
9 4
107
3
5
Compare –smallest firstCompare --smallest first
![Page 64: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/64.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest firstCompare –smallest first
![Page 65: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/65.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest goes first
![Page 66: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/66.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest first
![Page 67: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/67.jpg)
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest first
![Page 68: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/68.jpg)
Merge Sort—Merge
8
9
4
107
3
5
Compare –smallest first
![Page 69: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/69.jpg)
Merge Sort—Merge
8
9
4
107
3
5
Compare—smallest first
![Page 70: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/70.jpg)
Merge Sort—Merge
8
9
4
10
7
3
5
Compare –smallest first
![Page 71: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/71.jpg)
Merge Sort—Merge
8
9
4
10
7
3
5
Compare –smallest first
![Page 72: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/72.jpg)
Merge Sort—Merge
8
9
4
10
7
3
5
Compare –smallest first
The remainders
![Page 73: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/73.jpg)
Merge Sort—Merge
8
9 4
10
7
3
5
The remainders
![Page 74: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/74.jpg)
Merge Sort—Done
8
9 4 107
3
5
![Page 75: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/75.jpg)
A Quiz
How many shifts will occur before we copy this element back into the array?
[0] [1] [2] [3] [4] [5]
![Page 76: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/76.jpg)
A Quiz
Four items are shifted.
[0] [1] [2] [3] [4] [5]
![Page 77: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/77.jpg)
A Quiz
Four items are shifted.And then the element is copied back into the array.
[0] [1] [2] [3] [4] [5]
![Page 78: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/78.jpg)
Both Selectionsort and Insertionsort have a worst-case time of O(n2), making them impractical for large arrays.
But they are easy to program, easy to debug. Insertionsort also has good performance when the
array is nearly sorted to begin with. But more sophisticated sorting algorithms are
needed when good performance is needed in all cases for large arrays.
Timing and Other Issues
![Page 79: Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ed95503460f94be7683/html5/thumbnails/79.jpg)
THE END
Presentation copyright 2004 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.