computer science 101 introduction to sorting. sorting one of the most common activities of a...

29
Computer Science 101 Introduction to Sorting

Upload: stephanie-may-carson

Post on 04-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Computer Science 101

Introduction to Sorting

Page 2: 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

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

Page 3: 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

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)

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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.

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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?

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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?

Page 28: 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

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

Page 29: 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

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?