15
1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list to put it in specific order Examples: sorting scores by highest to lowest sorting filenames in alphabetical order sorting students by their student-id This can be done for the benefit of the user or, most often, for efficiency 4/24/2016 Sacramento State - CSc 10A 3 Sorting The bubble sort is one of the least efficient algorithms …but it is easy to understand Basic approach "lighter" elements “bubble up” to the top of the array "heavier" items sink to the bottom 4/24/2016 Sacramento State - CSc 10A 4 Bubble Sort Consists of two For Loops Outer loop runs from the first to the last Inner loop … runs from the second element to the last it checks every two neighbor elements, if the they are out of order, it swaps them so, the smallest element moves up the array 4/24/2016 Sacramento State - CSc 10A 5 How It Works // How to swap items at indexes i and j Declare Integer temp Set temp = array[i] Set array[i] = array[j] Set array[j] = temp 4/24/2016 Sacramento State - CSc 10A 6 Swap code

Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

1

Sorting

Chapter 9

(Spring 2016, CSUS)

Bubble Sort

Chapter 9.1

Often, computers needs to sort a list – to

put it in specific order

Examples:

• sorting scores by highest to lowest

• sorting filenames in alphabetical order

• sorting students by their student-id

This can be done for the benefit of the user

or, most often, for efficiency

4/24/2016 Sacramento State - CSc 10A 3

Sorting

The bubble sort is one of the

least efficient algorithms

…but it is easy to understand

Basic approach

• "lighter" elements “bubble up”

to the top of the array

• "heavier" items sink to the

bottom

4/24/2016 Sacramento State - CSc 10A 4

Bubble Sort

Consists of two For Loops

Outer loop runs from the first to the last

Inner loop …

• runs from the second element to the last

• it checks every two neighbor elements, if the

they are out of order, it swaps them

• so, the smallest element moves up the array

4/24/2016 Sacramento State - CSc 10A 5

How It Works

// How to swap items at indexes i and j

Declare Integer temp

Set temp = array[i]

Set array[i] = array[j]

Set array[j] = temp

4/24/2016 Sacramento State - CSc 10A 6

Swap code

Page 2: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

2

For outer = 0 to size-1

For inner = 1 to size-1

If A[inner-1] > A[inner]

//swap A[inner-1], A[inner]

End If

End For

End For

4/24/2016 Sacramento State - CSc 10A 7

The Bubble Sort

4/24/2016 Sacramento State - CSc 10A 8

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 9

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 10

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 11

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 12

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

Page 3: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

3

4/24/2016 Sacramento State - CSc 10A 13

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 14

Bubble Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 15

Bubble Sort Example

0

array

1

2

3

4

5

42

11

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 16

Bubble Sort Example

0

array

1

2

3

4

5

42

11

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 17

Bubble Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 18

Bubble Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

Page 4: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

4

4/24/2016 Sacramento State - CSc 10A 19

Bubble Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 20

Bubble Sort Example

0

array

1

2

3

4

58

11

42

5

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 21

Bubble Sort Example

0

array

1

2

3

4

58

11

42

5

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 22

Bubble Sort Example

0

array

1

2

3

4

58

11

42

5

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 23

Bubble Sort Example

0

array

1

2

3

4

58

11

42

5

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 24

Bubble Sort Example

0

array

1

2

3

4

58

11

5

42

73

Outer Loop

Inner Loop

Page 5: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

5

4/24/2016 Sacramento State - CSc 10A 25

Bubble Sort Example

0

array

1

2

3

4

58

11

5

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 26

Bubble Sort Example

0

array

1

2

3

4

58

11

5

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 27

Bubble Sort Example

0

array

1

2

3

4

58

11

5

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 28

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 29

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 30

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

Page 6: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

6

4/24/2016 Sacramento State - CSc 10A 31

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 32

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 33

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 34

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 35

Bubble Sort Example

0

array

1

2

3

4

58

5

11

42

73

Outer Loop

Inner Loop Heavier items drop rapidly to bottom (like

stones in water)

Lighter items bubble up slowly

The algorithm is hilariously inefficient

4/24/2016 Sacramento State - CSc 10A 36

Notice…

Page 7: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

7

Selection Sort

Chapter 9.2

The Selection Sort is a similar

to the Bubble Sort

However…

• rather than "bubble up" smaller

items, it scans the entire array

• it finds the smallest element

• only then does it swap the

values

4/24/2016 Sacramento State - CSc 10A 38

Selection Sort

Like the Bubble Sort, it consists of two For

Loops – one outer and one inner

Outer loop runs from the first to the last

Inner loop …

• starts at the position of the outer loop

• scans down and finds the smallest value

Then, after the scan, do a single swap

4/24/2016 Sacramento State - CSc 10A 39

Selection Sort

For outer = 0 to size-1

Set smallest = outer;

For inner = outer+1 to size-1

If array[inner] < array[smallest]

Set smallest = inner

End If

End For

//swap array[outer] and array[smallest]

End For

4/24/2016 Sacramento State - CSc 10A 40

The Selection Sort

4/24/2016 Sacramento State - CSc 10A 41

Selection Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 42

Selection Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

Page 8: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

8

4/24/2016 Sacramento State - CSc 10A 43

Selection Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 44

Selection Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 45

Selection Sort Example

0

array

1

2

3

4

73

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 46

Selection Sort Example

0

array

1

2

3

4

42

11

58

Outer Loop

Inner Loop 73

5

4/24/2016 Sacramento State - CSc 10A 47

Selection Sort Example

0

array

1

2

3

4

5

42

11

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 48

Selection Sort Example

0

array

1

2

3

4

5

42

11

58

73

Outer Loop

Inner Loop

Page 9: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

9

4/24/2016 Sacramento State - CSc 10A 49

Selection Sort Example

0

array

1

2

3

4

5

42

11

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 50

Selection Sort Example

0

array

1

2

3

4

5

42

11

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 51

Selection Sort Example

0

array

1

2

3

4

5

73

58

Outer Loop

Inner Loop

42

11

4/24/2016 Sacramento State - CSc 10A 52

Selection Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 53

Selection Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 54

Selection Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

Page 10: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

10

4/24/2016 Sacramento State - CSc 10A 55

Selection Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 56

Selection Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 57

Selection Sort Example

0

array

1

2

3

4

5

11

42

58

73

Outer Loop

Inner Loop Insertion Sort

Chapter 9.3

The Insertion Sort is sorting

algorithm with several

advantages over the Bubble

and Selection

Often, it is compared to

sorting a deck of cards

4/24/2016 Sacramento State - CSc 10A 59

Insertion Sort

Let’s say were asked to sort a

row of cards… (and you start

sorting from the left)

You will find a card, move it,

and shift the rest of the cards

to the right

So, you build a sorted list a

bit at a time – on the left side

4/24/2016 Sacramento State - CSc 10A 60

Deck of Cards

Page 11: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

11

Consists of two loops – inner and outer

A sorted list is constructed above the outer

Outer loop moves down the list

• current array value is temporarily removed

(copied) from the array

• moves down

4/24/2016 Sacramento State - CSc 10A 61

How it Works

Inner loop

• moves up from the current outer loop position

• if the cell, being looked at, is larger than the

saved value, it is moved down

• so, the "cards" shift to make room for the saved

cards proper position

4/24/2016 Sacramento State - CSc 10A 62

How it Works

For i = 1 to count-1

Set value = array[i]

Set j = i - 1;

While j >= 0 && array[j] > value

array[j + 1] = array[j]

Set j = j - 1

End While

Set array[j + 1] = value

End For

4/24/2016 Sacramento State - CSc 10A 63

The Insertion Sort

4/24/2016 Sacramento State - CSc 10A 64

Insertion Sort Example

0

array

1

2

3

4

73

11

58

5

value

42

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 65

Insertion Sort Example

0

array

1

2

3

4

73

11

58

5

value

42

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 66

Insertion Sort Example

0

array

1

2

3

4

73

11

58

5

value

42

Outer Loop

Inner Loop

Page 12: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

12

4/24/2016 Sacramento State - CSc 10A 67

Insertion Sort Example

0

array

1

2

3

4

73

58

5

value

42

11

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 68

Insertion Sort Example

0

array

1

2

3

4

73

11

58

5

value

42

11

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 69

Insertion Sort Example

0

array

1

2

3

4

73

11

58

5

value

42

11

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 70

Insertion Sort Example

0

array

1

2

3

4

73

11

58

5

value

42

11

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 71

Insertion Sort Example

0

array

1

2

3

4

73

5

value

42

11

58

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 72

Insertion Sort Example

0

array

1

2

3

4

73

5

value

42

11

58

Outer Loop

Inner Loop

Page 13: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

13

4/24/2016 Sacramento State - CSc 10A 73

Insertion Sort Example

0

array

1

2

3

4

73

5

value

42

11

58

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 74

Insertion Sort Example

0

array

1

2

3

4

73

5

value

42

11

58

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 75

Insertion Sort Example

0

array

1

2

3

4

73

Outer Loop

Inner Loop

value

42

11

58

5

4/24/2016 Sacramento State - CSc 10A 76

Insertion Sort Example

0

array

1

2

3

4

73value

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 77

Insertion Sort Example

0

array

1

2

3

4 73

value

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 78

Insertion Sort Example

0

array

1

2

3

4 73

value

42

11

58

5

Outer Loop

Inner Loop

Page 14: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

14

4/24/2016 Sacramento State - CSc 10A 79

Insertion Sort Example

0

array

1

2

3

4 73

value

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 80

Insertion Sort Example

0

array

1

2

3

4 73

value

42

11

58

5

Outer Loop

Inner Loop

4/24/2016 Sacramento State - CSc 10A 81

Insertion Sort Example

0

array

1

2

3

4 73

value

42

11

58

5

Outer Loop

Inner Loop

Binary Search

Chapter 9.4

A binary search is an fast and

efficient way to search an

array

Algorithm works like the

classic "secret number game"

Requires that the array is

sorted before the search

4/24/2016 Sacramento State - CSc 10A 83

Binary Searching

Starts knowing the max & min values

• in the case of arrays, this is the min and max index

• in the number game, it is the min and max value

Algorithm continues

• it looks at the midpoint between the first and last

• if the value > target, the max is set to the midpoint

• if the value < target, the min is set to the midpoint

• this eliminates half of the numbers each iteration

4/24/2016 Sacramento State - CSc 10A 84

How it Works

Page 15: Sorting Bubble Sortathena.ecs.csus.edu/~jacksocj/handouts/CSC10...1 Sorting Chapter 9 (Spring 2016, CSUS) Bubble Sort Chapter 9.1 Often, computers needs to sort a list –to put it

15

4/24/2016 Sacramento State - CSc 10A 85

Binary Example: Find 30

3 85 12 23 30 35 42 47 52 65 77 81

Min Max

4/24/2016 Sacramento State - CSc 10A 86

Binary Example: Find 30

3 85 12 23 30 35 42 47 52 65 77 81

Min Max

4/24/2016 Sacramento State - CSc 10A 87

Binary Example: Find 30

3 85 12 23 30 35 42 47 52 65 77 81

Min Max

4/24/2016 Sacramento State - CSc 10A 88

Binary Example: Find 30

3 85 12 23 30 35 42 47 52 65 77 81

Min Max

The binary search is incredibly efficient

and absolutely necessary for large arrays

Any item can be found only log2(n)

searches!

However, since array must be sorted,

sorting algorithms are equally vital

4/24/2016 Sacramento State - CSc 10A 89

Benefits

Array Size Sequential Search Binary Search

10 10 4

100 100 7

1,000 1,000 10

10,000 10,000 14

100,000 100,000 17

1,000,000 1,000,000 20

10,000,000 10,000,000 24

100,000,000 100,000,000 27

1,000,000,000 1,000,000,000 30

4/24/2016 Sacramento State - CSc 10A 90

Maximum # of Searches