Download presentation

Presentation is loading. Please wait.

Published byEric Forbes Modified over 2 years ago

1
Arrays (Continue) Sorting and searching

2
outlines Sorting – Bubble sort Linear search – min and max Binary search

3
Sort Bubble Sort

4
Sorting Sorting takes an unordered collection and makes it an ordered one

5
"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 Swap 4277

6
"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 Swap 3577

7
"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 Swap 1277

8
"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 No need to swap

9
"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 Swap 5101

10
"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 Largest value correctly placed

11
The Bubble Up Algorithm for(int i = 0; i

12
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 Largest value correctly placed

13
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 well correctly place all N elements.

14
Bubbling All the Elements N - 1

15
Reducing the Number of Comparisons

16
for(int i = 0; i

17
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!

18
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.

19
Add flag to reduce the number of iterations Boolean swap = false; for(int i = 0; i

20
An Animated Example j index 0 A.length 8 swap true

21
An Animated Example to_do index 0 1 A.lenght 8 did_swap false

22
An Animated Example to_do index 0 1 N 8 Swap did_swap false

23
An Animated Example to_do index 0 1 N 8 Swap did_swap true

24
An Animated Example j index 7 2 A.length 8 did_swap true

25
An Animated Example to_do index 7 2 N 8 Swap did_swap true

26
An Animated Example j index 0 2 A.length 8 Swap did_swap true

27
An Animated Example j index 0 3 A.length 8 swap true

28
An Animated Example j index 0 3 A.lengt 8 Swap swap true

29
An Animated Example j index 0 3 A.lengt 8 Swap swap true

30
An Animated Example j index 0 4 A.lengt 8 swap true

31
An Animated Example j index 0 4 A.length 8 Swap did_swap true

32
An Animated Example j index 0 4 A.length 8 Swap did_swap true

33
An Animated Example j index 0 5 A.lengt 8 swap true

34
An Animated Example j index 0 5 A.lengt 8 Swap swap true

35
An Animated Example j index 0 5 A.length 8 Swap did_swap true

36
An Animated Example j index 0 6 A.length 8 swap true

37
An Animated Example j index 0 6 A.lengt 8 Swap swap true

38
An Animated Example j index 0 6 A.lengt 8 Swap swap true

39
An Animated Example j index 0 7 A.lengt 8 swap true

40
An Animated Example j index 0 7 A.length 8 Swap swap true

41
An Animated Example j index 0 7 A.length 8 Swap swap true

42
After First Pass of Outer Loop j index 0 8 A.length 8 Finished first Bubble Up swap true

43
The Second Bubble Up j index 2 1 A.length 8 swap false

44
The Second Bubble Up j index 2 1 A.length 8 swap false No Swap

45
The Second Bubble Up j index 1 2 A.length 8 swap false

46
The Second Bubble Up j index 1 2 A.length 8 did_swap false Swap

47
The Second Bubble Up j index 1 2 A.length 8 did_swap true Swap

48
The Second Bubble Up j index 1 3 A.length 8 swap true

49
The Second Bubble Up j index 1 3 A.length 8 did_swap true Swap

50
The Second Bubble Up j index 1 3 A.length 8 swap true Swap

51
The Second Bubble Up j index 1 4 A.length 8 did_swap true

52
The Second Bubble Up to_do index 1 4 N 8 swap true No Swap

53
The Second Bubble Up to_do index 1 5 N 8 swap true

54
The Second Bubble Up to_do index 1 5 N 8 swap true Swap

55
The Second Bubble Up j index 1 5 A.length 8 swap true Swap

56
The Second Bubble Up j index 1 6 A.length 8 swap true

57
The Second Bubble Up to_do index 1 6 N 8 swap true Swap

58
The Second Bubble Up to_do index 1 6 N 8 swap true Swap

59
After Second Pass of Outer Loop index 1 7 N 8 swap true Finished second Bubble Up

60
The Third Bubble Up j index 2 1 A.length 8 swap false

61
The Third Bubble Up j index 2 1 A.length 8 swap false Swap

62
The Third Bubble Up j index 2 1 A.length 8 swap true Swap

63
The Third Bubble Up j index 2 2 A.length 8 swap true

64
The Third Bubble Up j index 2 2 A.lengt 8 swap true Swap

65
The Third Bubble Up j index 2 2 A.lengt 8 swap true Swap

66
The Third Bubble Up to_do index 2 3 N 8 did_swap true

67
The Third Bubble Up j index 2 3 A.length 8 swap true No Swap

68
The Third Bubble Up j index 2 4 A.length 8 did_swap true

69
The Third Bubble Up j index 2 4 A.length 8 swap true Swap

70
The Third Bubble Up j index 2 4 A.length 8 swap true Swap

71
The Third Bubble Up j index 2 5 A.length 8 did_swap true

72
The Third Bubble Up j index 2 5 A.length 8 swap true Swap

73
The Third Bubble Up j index 2 5 A.length 8 did_swap true Swap

74
After Third Pass of Outer Loop j index 2 6 A.length 8 did_swap true Finished third Bubble Up

75
The Fourth Bubble Up j index 3 1 A.length 8 swap false

76
The Fourth Bubble Up j index 3 1 A.length 8 swap false Swap

77
The Fourth Bubble Up to_do index 3 1 N 8 did_swap true Swap

78
The Fourth Bubble Up j index 3 2 A.length 8 swap true

79
The Fourth Bubble Up j index 3 2 A.length 8 did_swap true No Swap

80
The Fourth Bubble Up j index 3 3 A.length 8 swap true

81
The Fourth Bubble Up j index 3 3 A.length 8 did_swap true No Swap

82
The Fourth Bubble Up j index 3 4 A.length 8 swap true

83
The Fourth Bubble Up j index 3 4 A.length 8 did_swap true No Swap

84
After Fourth Pass of Outer Loop j index 3 5 A.length 8 swap true Finished fourth Bubble Up

85
The Fifth Bubble Up j index 4 1 A.length 8 swap false

86
The Fifth Bubble Up j index 4 1 A.lenght 8 swap false No Swap

87
The Fifth Bubble Up j index 4 2 A.length 8 swap false

88
The Fifth Bubble Up j index 4 2 A.length 8 swap false No Swap

89
The Fifth Bubble Up j index 4 3 A.length 8 swap false

90
Finished Early j index 3 4 A.lengt 8 swap false We didnt do any swapping, so all of the other elements must be correctly placed. We can skip the last two passes of the outer loop.

91
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

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

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

94
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; }

95
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; }

96
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; }

97
summary Buble sort Learnear search Binary sort

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google