Sorting Big O Activity.docx

advertisement
Sorting Method 2
Compare each pair of elements. If a pair is out of order, swap it. Keep going over the list until you make
a pass and there are no more pairs to swap.
Example
1
8
7
6
7
6
Compare 1 and 8
1
8
Compare 8 and 7. Swap them.
1
8
7
6
1
7
8
6
1
7
6
8
Compare 8 and 6. Swap them.
1
7
8
6
Start again at the beginning. Compare 1 and 7. No need to swap.
1
7
6
8
Answers to Example Big Os
1.
2.
3.
4.
5
Your name ___________________________________ ____ Your netid _______________
Sorting Method 1
Search through the list for the lowest item. Place it aside. Then search for the second lowest item. etc.
Example
4
7
3
8
First element "4" starts out as our lowest.
Compare 4 and 7.
4
7
3
8
4 is lower so it stays our lowest.
Compare 4 and 3.
4
7
3
8
3 is lower so it becomes are new comparison point.
Compare 3 and 8.
4
7
3
8
3 is lowest, so it stays our comparison point. Since there are now more elements 3 must be the lowest.
It is the first element of our "result list". Now we start again with 3 removed.
4
7
8
Download