Download presentation
Presentation is loading. Please wait.
1
Sorting CSCE 121 J. Michael Moore
Based on slides created by Carlos Soto.
2
Sorting Algorithms There are lots of ways to sort
you will implement several in CSCE 221 C++ STL has built in sorting So do many other languages Still very useful to know how to implement Good first algorithm to code Even a small amount of code can be tricky
3
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. Outer Loop Inner Loop
4
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. Insert 1 2 3 4 5 6 7 8 27 1 93 2 42 3 77 4 19 5 55 6 212 7 32 8 111
5
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. Insert 1 2 3 4 5 6 7 8 27 1 93 2 42 3 77 4 19 5 55 6 212 7 32 8 111 Smallest
6
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 2 3 4 5 6 7 8 27 1 93 2 42 3 77 4 55 5 212 6 32 7 111 8 Insert
7
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 2 3 4 5 6 7 8 27 1 93 2 42 3 77 4 55 5 212 6 32 7 111 8 Insert
8
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 3 4 5 6 7 8 93 1 42 2 77 3 55 4 212 5 32 6 111 7 8 Insert
9
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 3 4 5 6 7 8 93 1 42 2 77 3 55 4 212 5 32 6 111 7 8 Insert Smallest
10
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 4 5 6 7 8 93 1 42 2 77 3 55 4 212 5 111 6 7 8 Insert
11
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 4 5 6 7 8 93 1 42 2 77 3 55 4 212 5 111 6 7 8 Smallest Insert
12
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 5 6 7 8 93 1 77 2 55 3 212 4 111 5 6 7 8 Insert
13
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 5 6 7 8 93 1 77 2 55 3 212 4 111 5 6 7 8 Smallest Insert
14
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 6 7 8 93 1 77 2 212 3 111 4 5 6 7 8 Insert
15
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 6 7 8 93 1 77 2 212 3 111 4 5 6 7 8 Smallest Insert
16
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 7 8 93 1 212 2 111 3 4 5 6 7 8 Insert
17
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 7 8 93 1 212 2 111 3 4 5 6 7 8 Insert
18
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 93 7 8 212 1 111 2 3 4 5 6 7 8 Insert
19
Selection Sort Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 93 7 8 212 1 111 2 3 4 5 6 7 8 Smallest Insert
20
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 1 2 3 4 5 6 7 8 Insert
21
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 1 2 3 4 5 6 7 8 Insert
22
Selection Sort Start with an ‘empty’ list.
Smallest Start with an ‘empty’ list. While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 1 2 3 4 5 6 7 8 Insert
23
Selection Sort Sorted! Start with an ‘empty’ list.
While we still have values to insert, Find smallest value and put at end of list. 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 1 2 3 4 5 6 7 8
24
Improve? Created a second vector.
Do we have to? NO! Keep sorted list and remaining items to place in the same vector. When we start the sorted list has size 0 (zero) the number of items to place is the size of the list Start with the place to insert at zero Find smallest starting from the place to insert Swap smallest with item in the place to insert Increment place to insert
25
Selection Sort (Second Version)
Set place to insert to first location in list While we still have values to insert, Find smallest value and swap with location to insert. Increment place to insert
26
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert Insert 27 1 93 2 42 3 77 4 19 5 55 6 212 7 32 8 111 Smallest Items to search
27
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert Insert 19 1 93 2 42 3 77 4 27 5 55 6 212 7 32 8 111 Smallest Items to search
28
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 93 2 42 3 77 4 27 5 55 6 212 7 32 8 111 Insert Smallest Items to search
29
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 42 3 77 4 93 5 55 6 212 7 32 8 111 Insert Smallest Items to search
30
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 42 3 77 4 93 5 55 6 212 7 32 8 111 Insert Items to search Smallest
31
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert. 19 1 27 2 32 3 77 4 93 5 55 6 212 7 42 8 111 Insert Items to search Smallest
32
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert. 19 1 27 2 32 3 77 4 93 5 55 6 212 7 42 8 111 Insert Items to search Smallest
33
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert. 19 1 27 2 32 3 42 4 93 5 55 6 212 7 77 8 111 Insert Items to search Smallest
34
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 93 5 55 6 212 7 77 8 111 Insert Smallest Items to search
35
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 93 6 212 7 77 8 111 Insert Smallest Items to search
36
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 93 6 212 7 77 8 111 Insert Items to search Smallest
37
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 212 7 93 8 111 Insert Items to search Smallest
38
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 212 7 93 8 111 Insert Smallest Items to search
39
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 93 7 212 8 111 Insert Smallest Items to search
40
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 93 7 212 8 111 Insert Items to search Smallest
41
Selection Sort Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 Insert Items to search Smallest
42
Selection Sort Since last item has to be in the right place, we actually stop before inserting into last location. Set place to insert to first location in list While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 Smallest Items to search Insert
43
Selection Sort Sorted! Set place to insert to first location in list
While we still have values to insert, Find smallest value and swap with location to insert Increment place to insert 19 1 27 2 32 3 42 4 55 5 77 6 93 7 111 8 212 Sorted!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.