Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a...
-
Upload
stephanie-may-carson -
Category
Documents
-
view
217 -
download
2
Transcript of Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a...
Computer Science 101
Introduction to Sorting
Sorting
• One of the most common activities of a computer is sorting data
• Arrange data into numerical or alphabetical order for purposes of– Reports by category– Summarizing data– Searching data
Sorting
• We’re given a list of data in random order
• At the end of the sorting, each datum is less than or equal to its successor in the list
• For each i from 1 to N - 1, A(i) <= A(i + 1)
Visualizing the Results
34 22 66 80 14 90 32 16
Sorting Algorithm
List ASize = 8
14 16 22 32 34 66 80 90
Sorting algorithms usually move data around in the original list
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.34 22 66 80 14 90 32 16
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.34 22 66 80 14 16 32 90
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.34 22 66 32 14 16 80 90
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.34 22 16 32 14 66 80 90
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.14 22 16 32 34 66 80 90
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.14 22 16 32 34 66 80 90
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.14 16 22 32 34 66 80 90
Largest Upper
First Algorithm: Selection Sort
• Strategy:– Find the largest item in the list and exchange it
with the last item in the list– Find the next largest item in the list and
exchange it with the next to the last item in the list
– Etc.14 16 22 32 34 66 80 90
LargestUpper
First Algorithm: Selection Sort
• Uses an algorithm we’ve already seen, search for the largest, as a component
A The listN The size of the listUpper The end of the unsorted portion of the listLargest The position of the largest item so farCurrent Used to traverse the list during a search
The Selection Sort Algorithmset Upper to Nwhile Upper > 1 do set Largest to the position of the largest item between positions 1 and Upper exchange A(Largest) and A(Upper) decrement Upper
The component in red is the search for largest algorithm
We’ll expand that into detailed form next
Note for now that the code in red executes N – 1 times.
The Selection Sort Algorithmset Upper to Nwhile Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current exchange A(Largest) and A(Upper) decrement Upper
The code in red is the complete search for largest algorithm
Note that the first search requires N – 1 comparisons and each remaining search requires one less comparison
The Selection Sort Algorithmset Upper to Nwhile Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current exchange A(Largest) and A(Upper) decrement Upper
The total number of comparisons = (N2 – N) / 2
The total number of exchanges = N - 1
Improving Selection Sortset Upper to Nwhile Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current if Largest Upper then exchange A(Largest) and A(Upper) decrement Upper
Some exchanges might not be necessary
What is the best case? the worst case?
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
34 22 66 80 14 90 32 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 80 14 90 32 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 80 14 90 32 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 80 14 90 32 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 14 80 90 32 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 14 80 90 32 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 14 80 32 90 16
Current
2nd Algorithm: Bubble Sort
• Strategy:– Pass through the list from left to right– Compare each element with the one to its left– Swap them if they are out of order– Such a pass will put largest at the right end– Continue making these passes until sorted
22 34 66 14 80 32 16 90
Current
The Bubble Sort Algorithmset Upper to Nwhile Upper > 1 do bubble the largest item down to upper decrement Upper
Note that the bubble process is executed N – 1 times
The Bubble Sort Algorithmset Upper to Nwhile Upper > 1 do set Current to 2 while Current <= Upper do if A(Current) < A(Current – 1) then exchange A(Current) and A(Current – 1) increment Current decrement Upper
Note that the bubble process is executed N – 1 times
Note that the first bubble process requires N – 1 comparisons and each remaining bubble process needs one less comparison
How many total comparisons? Exchanges?
Improving Bubble Sortset Upper to Nwhile Upper > 1 do set Current to 2 while Current <= Upper do if A(Current) < A(Current – 1) then exchange A(Current) and A(Current – 1) increment Current decrement Upper
If no exchanges are performed during a bubble process, then the list is sorted
Perhaps we can quit the outer loop when this is the case
Improving Bubble Sortset Upper to NSet Sorted to falsewhile Upper > 1 and not Sorted do set Current to 2 set Sorted to true while Current <= Upper do if A(Current) < A(Current – 1) then set Sorted to false exchange A(Current) and A(Current – 1) increment Current decrement Upper
We assume that the list is sorted before each bubble process and prove that it’s not sorted when an exchange is made
What is the best case # of comparisons now?