97
Arrays (Continue) Sorting and searching

Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Embed Size (px)

Citation preview

Page 1: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Arrays (Continue)

Sorting and searching

Page 2: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

outlines

• Sorting – Bubble sort

• Linear search– min and max

• Binary search

Page 3: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Sort

Bubble Sort

Page 4: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Sorting

• Sorting takes an unordered collection and makes it an ordered one.

512354277 101

1 2 3 4 5 6

5 12 35 42 77 101

1 2 3 4 5 6

Page 5: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

"Bubbling Up" the Largest Element

• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using pair-

wise comparisons and swapping

512354277 101

1 2 3 4 5 6

Swap42 77

Page 6: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

"Bubbling Up" the Largest Element

• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using pair-

wise comparisons and swapping

512357742 101

1 2 3 4 5 6

Swap35 77

Page 7: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

"Bubbling Up" the Largest Element

• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using pair-

wise comparisons and swapping

512773542 101

1 2 3 4 5 6

Swap12 77

Page 8: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

"Bubbling Up" the Largest Element

• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using pair-

wise comparisons and swapping

577123542 101

1 2 3 4 5 6

No need to swap

Page 9: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

"Bubbling Up" the Largest Element

• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using pair-

wise comparisons and swapping

577123542 101

1 2 3 4 5 6

Swap5 101

Page 10: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

"Bubbling Up" the Largest Element

• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end using pair-

wise comparisons and swapping

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 11: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The “Bubble Up” Algorithm

for(int i = 0; i<A.length; i++) { for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp;

} } }

Page 12: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Items of Interest

• Notice that only the largest value is correctly placed

• All other values are still out of order• So we need to repeat this process

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 13: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Repeat “Bubble Up” How Many Times?

• If we have N elements…

• And if each time we bubble an element, we place it in its correct location…

• Then we repeat the “bubble up” process N – 1 times.

• This guarantees we’ll correctly place all N elements.

Page 14: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

“Bubbling” All the Elements

77123542 5

1 2 3 4 5 6

101

5421235 77

1 2 3 4 5 6

101

42 5 3512 77

1 2 3 4 5 6

101

42 35 512 77

1 2 3 4 5 6

101

42 35 12 5 77

1 2 3 4 5 6

101

N -

1

Page 15: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Reducing the Number of Comparisons

12 35 42 77 101

1 2 3 4 5 6

5

77123542 5

1 2 3 4 5 6

101

5421235 77

1 2 3 4 5 6

101

42 5 3512 77

1 2 3 4 5 6

101

42 35 512 77

1 2 3 4 5 6

101

Page 16: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

for(int i = 0; i<A.length; i++) { for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; }

} }

Page 17: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Already Sorted Collections?

• What if the collection was already sorted?• What if only a few elements were out of

place and after a couple of “bubble ups,” the collection was sorted?

• We want to be able to detect this and “stop early”!

42 35 12 5 77

1 2 3 4 5 6

101

Page 18: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Using a Boolean “Flag”

• We can use a boolean variable to determine if any swapping occurred during the “bubble up.”

• If no swapping occurred, then we know that the collection is already sorted!

• This boolean “flag” needs to be reset after each “bubble up.”

Page 19: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Add flag to reduce the number of iterations

Boolean swap = false;for(int i = 0; i<A.length && !swap; i++) { boolean swap = false; for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; swap = true;

} } }

Page 20: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

j

index

0

A.length 8 swap true

Page 21: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

0

1

A.lenght 8 did_swap false

Page 22: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

0

1

N 8

Swap

did_swap false

Page 23: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

0

1

N 8

Swap

did_swap true

Page 24: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

j

index

7

2

A.length 8 did_swap true

Page 25: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8

Swap

did_swap true

Page 26: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

j

index

0

2

A.length 8

Swap

did_swap true

Page 27: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

j

index

0

3

A.length 8 swap true

Page 28: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

j

index

0

3

A.lengt 8

Swap

swap true

Page 29: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

j

index

0

3

A.lengt 8

Swap

swap true

Page 30: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

j

index

0

4

A.lengt 8 swap true

Page 31: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

j

index

0

4

A.length 8

Swap

did_swap true

Page 32: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

j

index

0

4

A.length 8

Swap

did_swap true

Page 33: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

j

index

0

5

A.lengt 8 swap true

Page 34: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

j

index

0

5

A.lengt 8

Swap

swap true

Page 35: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

j

index

0

5

A.length 8

Swap

did_swap true

Page 36: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

j

index

0

6

A.length 8 swap true

Page 37: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

j

index

0

6

A.lengt 8

Swap

swap true

Page 38: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

j

index

0

6

A.lengt 8

Swap

swap true

Page 39: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

j

index

0

7

A.lengt 8 swap true

Page 40: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

j

index

0

7

A.length 8

Swap

swap true

Page 41: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

An Animated Example

331445 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

0

7

A.length 8

Swap

swap true

Page 42: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

After First Pass of Outer Loop

331445 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

0

8

A.length 8

Finished first “Bubble Up”

swap true

Page 43: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

2

1

A.length 8 swap false

Page 44: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

2

1

A.length 8 swap false

No Swap

Page 45: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

2

A.length 8 swap false

Page 46: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

2

A.length 8 did_swap false

Swap

Page 47: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

2

A.length 8 did_swap true

Swap

Page 48: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

3

A.length 8 swap true

Page 49: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

3

A.length 8 did_swap true

Swap

Page 50: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

3

A.length 8 swap true

Swap

Page 51: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

j

index

1

4

A.length 8 did_swap true

Page 52: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

1

4

N 8 swap true

No Swap

Page 53: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

1

5

N 8 swap true

Page 54: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

1

5

N 8 swap true

Swap

Page 55: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

j

index

1

5

A.length 8 swap true

Swap

Page 56: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

j

index

1

6

A.length 8 swap true

Page 57: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

1

6

N 8 swap true

Swap

Page 58: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Second “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

1

6

N 8 swap true

Swap

Page 59: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

After Second Pass of Outer Loop

42614 45 33 6723 98

1 2 3 4 5 6 7 8

index

1

7

N 8 swap true

Finished second “Bubble Up”

Page 60: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

j

index

2

1

A.length 8 swap false

Page 61: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

j

index

2

1

A.length 8 swap false

Swap

Page 62: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

1

A.length 8 swap true

Swap

Page 63: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

2

A.length 8 swap true

Page 64: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

2

A.lengt 8 swap true

Swap

Page 65: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

2

A.lengt 8 swap true

Swap

Page 66: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

2

3

N 8 did_swap true

Page 67: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

3

A.length 8 swap true

No Swap

Page 68: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

4

A.length 8 did_swap true

Page 69: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

j

index

2

4

A.length 8 swap true

Swap

Page 70: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

j

index

2

4

A.length 8 swap true

Swap

Page 71: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

j

index

2

5

A.length 8 did_swap true

Page 72: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

j

index

2

5

A.length 8 swap true

Swap

Page 73: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Third “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

j

index

2

5

A.length 8 did_swap true

Swap

Page 74: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

After Third Pass of Outer Loop

45236 33 42 6714 98

1 2 3 4 5 6 7 8

j

index

2

6

A.length 8 did_swap true

Finished third “Bubble Up”

Page 75: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

j

index

3

1

A.length 8 swap false

Page 76: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

j

index

3

1

A.length 8 swap false

Swap

Page 77: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

1

N 8 did_swap true

Swap

Page 78: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

2

A.length 8 swap true

Page 79: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

2

A.length 8 did_swap true

No Swap

Page 80: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

3

A.length 8 swap true

Page 81: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

3

A.length 8 did_swap true

No Swap

Page 82: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

4

A.length 8 swap true

Page 83: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

4

A.length 8 did_swap true

No Swap

Page 84: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

After Fourth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

5

A.length 8 swap true

Finished fourth “Bubble Up”

Page 85: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

4

1

A.length 8 swap false

Page 86: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

4

1

A.lenght 8 swap false

No Swap

Page 87: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

4

2

A.length 8 swap false

Page 88: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

4

2

A.length 8 swap false

No Swap

Page 89: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

4

3

A.length 8 swap false

Page 90: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Finished “Early”

452314 33 42 676 98

1 2 3 4 5 6 7 8

j

index

3

4

A.lengt 8 swap false

We didn’t do any swapping,so all of the other elementsmust be correctly placed.

We can “skip” the last twopasses of the outer loop.

Page 91: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Bubble sort Summary• “Bubble Up” algorithm will move largest value to

its correct location (to the right)• Repeat “Bubble Up” until all elements are

correctly placed:– Maximum of N-1 times– Can finish early if no swapping occurs

• We reduce the number of elements we compare each time one is correctly placed

Page 92: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Linear search

Use linear to seach and value is contained in one array.

Page 93: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Searching for x in an array a

Searching for x in an array a for(int i = 0; i < a.length; ++ i) { if (a[i] == x) System.out.println(“true”); }

Page 94: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Finding the minimal value in array

int minValue = a[0]; int minIndex = 0; for(int i = start + 1; i < end; i ++) { if (a[i] < minValue) { minValue = a[i]; minIndex = i; }

Page 95: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Finding the minimal value in array

int maxValue = a[0]; int maxIndex = 0; for(int i = start + 1; i < end; i ++) { if (a[i] > maxValue) { maxValue = a[i]; maxIndex = i; }

Page 96: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

Binary search for a value in array Searching if x is in array a. We assume a is sorted in ascending order. int start = 0; int end = a.length - 1; boulean found = flase while (start <= end) { int m = (start + end)/2; if (x == a[m]) {found =false;} else if (x < a[m]) { end = m - 1;} else { start = m + 1; } }

Page 97: Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search

summary

• Buble sort • Learnear search • Binary sort