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) on which the ordering is based Sorting algorithms Algorithms that order the items in the collection based on the sort key Why is sorting important?
79
Embed
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)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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?
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++
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
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
Selection Sort
Figure 9.9 Example of a selection sort (sorted elements are shaded)
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]
[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]
[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]
[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]
[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]
The Selectionsort Algorithm
Find the smallest element in the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
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]
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]
The Selectionsort Algorithm
The process continues...
Sorted side Unsorted side
Smallestfrom
unsorted
[0] [1] [2] [3] [4] [5]
The Selectionsort Algorithm
The process continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
The Selectionsort Algorithm
The process continues...
Sorted side Unsorted sideSorted side
is bigger
[0] [1] [2] [3] [4] [5]
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]
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
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]
[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]
[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
[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
[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.
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]
Quicksort
Figure 9.13 Splitting algorithm
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 } }
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
Compare –smallest goes firstCompare—smallest goes first
Choose smallest from each stack
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest goes firstCompare—smallest goes first
Choose smallest from each stack
Merge Sort—Merge
8
9 4
107
3
5
Compare –smallest firstCompare --smallest first
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest firstCompare –smallest first
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest goes first
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest first
Merge Sort—Merge
8
9 4 107
3
5
Compare –smallest first
Merge Sort—Merge
8
9
4
107
3
5
Compare –smallest first
Merge Sort—Merge
8
9
4
107
3
5
Compare—smallest first
Merge Sort—Merge
8
9
4
10
7
3
5
Compare –smallest first
Merge Sort—Merge
8
9
4
10
7
3
5
Compare –smallest first
Merge Sort—Merge
8
9
4
10
7
3
5
Compare –smallest first
The remainders
Merge Sort—Merge
8
9 4
10
7
3
5
The remainders
Merge Sort—Done
8
9 4 107
3
5
A Quiz
How many shifts will occur before we copy this element back into the array?
[0] [1] [2] [3] [4] [5]
A Quiz
Four items are shifted.
[0] [1] [2] [3] [4] [5]
A Quiz
Four items are shifted.And then the element is copied back into the array.
[0] [1] [2] [3] [4] [5]
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
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.