Download presentation
Presentation is loading. Please wait.
Published byEric Forbes Modified over 10 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 12 3542 77 101 1 2 3 4 5 6 5 12 35 42 77101 1 2 3 4 5 6
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 5 12 3542 77 101 1 2 3 4 5 6 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 5 12 3577 42 101 1 2 3 4 5 6 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 5 12 7735 42 101 1 2 3 4 5 6 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 5 77 1235 42 101 1 2 3 4 5 6 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 5 77 1235 42 101 1 2 3 4 5 6 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 77 1235 42 5 1 2 3 4 5 6 101 Largest value correctly placed
11
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; }
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 77 1235 42 5 1 2 3 4 5 6 101 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 77 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35 5 12 77 1 2 3 4 5 6 10142 35 12 5 77 1 2 3 4 5 6 101 N - 1
15
Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 577 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35 5 12 77 1 2 3 4 5 6 101
16
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; }
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! 42 35 12 5 77 1 2 3 4 5 6 101
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<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; }
20
An Animated Example 674523146339842 1 2 3 4 5 6 7 8 j index 0 A.length 8 swap true
21
An Animated Example 674523146339842 1 2 3 4 5 6 7 8 to_do index 0 1 A.lenght 8 did_swap false
22
An Animated Example 674523146339842 1 2 3 4 5 6 7 8 to_do index 0 1 N 8 Swap did_swap false
23
An Animated Example 674598146332342 1 2 3 4 5 6 7 8 to_do index 0 1 N 8 Swap did_swap true
24
An Animated Example 674598146332342 1 2 3 4 5 6 7 8 j index 7 2 A.length 8 did_swap true
25
An Animated Example 674598146332342 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
26
An Animated Example 679845146332342 1 2 3 4 5 6 7 8 j index 0 2 A.length 8 Swap did_swap true
27
An Animated Example 679845146332342 1 2 3 4 5 6 7 8 j index 0 3 A.length 8 swap true
28
An Animated Example 679845146332342 1 2 3 4 5 6 7 8 j index 0 3 A.lengt 8 Swap swap true
29
An Animated Example 671445986332342 1 2 3 4 5 6 7 8 j index 0 3 A.lengt 8 Swap swap true
30
An Animated Example 671445986332342 1 2 3 4 5 6 7 8 j index 0 4 A.lengt 8 swap true
31
An Animated Example 671445986332342 1 2 3 4 5 6 7 8 j index 0 4 A.length 8 Swap did_swap true
32
An Animated Example 671445698332342 1 2 3 4 5 6 7 8 j index 0 4 A.length 8 Swap did_swap true
33
An Animated Example 671445698332342 1 2 3 4 5 6 7 8 j index 0 5 A.lengt 8 swap true
34
An Animated Example 671445698332342 1 2 3 4 5 6 7 8 j index 0 5 A.lengt 8 Swap swap true
35
An Animated Example 981445667332342 1 2 3 4 5 6 7 8 j index 0 5 A.length 8 Swap did_swap true
36
An Animated Example 981445667332342 1 2 3 4 5 6 7 8 j index 0 6 A.length 8 swap true
37
An Animated Example 981445667332342 1 2 3 4 5 6 7 8 j index 0 6 A.lengt 8 Swap swap true
38
An Animated Example 331445667982342 1 2 3 4 5 6 7 8 j index 0 6 A.lengt 8 Swap swap true
39
An Animated Example 331445667982342 1 2 3 4 5 6 7 8 j index 0 7 A.lengt 8 swap true
40
An Animated Example 331445667982342 1 2 3 4 5 6 7 8 j index 0 7 A.length 8 Swap swap true
41
An Animated Example 331445667422398 1 2 3 4 5 6 7 8 j index 0 7 A.length 8 Swap swap true
42
After First Pass of Outer Loop 331445667422398 1 2 3 4 5 6 7 8 j index 0 8 A.length 8 Finished first Bubble Up swap true
43
The Second Bubble Up 331445667422398 1 2 3 4 5 6 7 8 j index 2 1 A.length 8 swap false
44
The Second Bubble Up 331445667422398 1 2 3 4 5 6 7 8 j index 2 1 A.length 8 swap false No Swap
45
The Second Bubble Up 331445667422398 1 2 3 4 5 6 7 8 j index 1 2 A.length 8 swap false
46
The Second Bubble Up 331445667422398 1 2 3 4 5 6 7 8 j index 1 2 A.length 8 did_swap false Swap
47
The Second Bubble Up 334514667422398 1 2 3 4 5 6 7 8 j index 1 2 A.length 8 did_swap true Swap
48
The Second Bubble Up 334514667422398 1 2 3 4 5 6 7 8 j index 1 3 A.length 8 swap true
49
The Second Bubble Up 334514667422398 1 2 3 4 5 6 7 8 j index 1 3 A.length 8 did_swap true Swap
50
The Second Bubble Up 336144567422398 1 2 3 4 5 6 7 8 j index 1 3 A.length 8 swap true Swap
51
The Second Bubble Up 336144567422398 1 2 3 4 5 6 7 8 j index 1 4 A.length 8 did_swap true
52
The Second Bubble Up 336144567422398 1 2 3 4 5 6 7 8 to_do index 1 4 N 8 swap true No Swap
53
The Second Bubble Up 336144567422398 1 2 3 4 5 6 7 8 to_do index 1 5 N 8 swap true
54
The Second Bubble Up 336144567422398 1 2 3 4 5 6 7 8 to_do index 1 5 N 8 swap true Swap
55
The Second Bubble Up 676144533422398 1 2 3 4 5 6 7 8 j index 1 5 A.length 8 swap true Swap
56
The Second Bubble Up 676144533422398 1 2 3 4 5 6 7 8 j index 1 6 A.length 8 swap true
57
The Second Bubble Up 676144533422398 1 2 3 4 5 6 7 8 to_do index 1 6 N 8 swap true Swap
58
The Second Bubble Up 426144533672398 1 2 3 4 5 6 7 8 to_do index 1 6 N 8 swap true Swap
59
After Second Pass of Outer Loop 426144533672398 1 2 3 4 5 6 7 8 index 1 7 N 8 swap true Finished second Bubble Up
60
The Third Bubble Up 426144533672398 1 2 3 4 5 6 7 8 j index 2 1 A.length 8 swap false
61
The Third Bubble Up 426144533672398 1 2 3 4 5 6 7 8 j index 2 1 A.length 8 swap false Swap
62
The Third Bubble Up 426234533671498 1 2 3 4 5 6 7 8 j index 2 1 A.length 8 swap true Swap
63
The Third Bubble Up 426234533671498 1 2 3 4 5 6 7 8 j index 2 2 A.length 8 swap true
64
The Third Bubble Up 426234533671498 1 2 3 4 5 6 7 8 j index 2 2 A.lengt 8 swap true Swap
65
The Third Bubble Up 422364533671498 1 2 3 4 5 6 7 8 j index 2 2 A.lengt 8 swap true Swap
66
The Third Bubble Up 422364533671498 1 2 3 4 5 6 7 8 to_do index 2 3 N 8 did_swap true
67
The Third Bubble Up 422364533671498 1 2 3 4 5 6 7 8 j index 2 3 A.length 8 swap true No Swap
68
The Third Bubble Up 422364533671498 1 2 3 4 5 6 7 8 j index 2 4 A.length 8 did_swap true
69
The Third Bubble Up 422364533671498 1 2 3 4 5 6 7 8 j index 2 4 A.length 8 swap true Swap
70
The Third Bubble Up 422363345671498 1 2 3 4 5 6 7 8 j index 2 4 A.length 8 swap true Swap
71
The Third Bubble Up 422363345671498 1 2 3 4 5 6 7 8 j index 2 5 A.length 8 did_swap true
72
The Third Bubble Up 422363345671498 1 2 3 4 5 6 7 8 j index 2 5 A.length 8 swap true Swap
73
The Third Bubble Up 452363342671498 1 2 3 4 5 6 7 8 j index 2 5 A.length 8 did_swap true Swap
74
After Third Pass of Outer Loop 452363342671498 1 2 3 4 5 6 7 8 j index 2 6 A.length 8 did_swap true Finished third Bubble Up
75
The Fourth Bubble Up 452363342671498 1 2 3 4 5 6 7 8 j index 3 1 A.length 8 swap false
76
The Fourth Bubble Up 452363342671498 1 2 3 4 5 6 7 8 j index 3 1 A.length 8 swap false Swap
77
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap true Swap
78
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 3 2 A.length 8 swap true
79
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 3 2 A.length 8 did_swap true No Swap
80
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 3 3 A.length 8 swap true
81
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 3 3 A.length 8 did_swap true No Swap
82
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 3 4 A.length 8 swap true
83
The Fourth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 3 4 A.length 8 did_swap true No Swap
84
After Fourth Pass of Outer Loop 452314334267698 1 2 3 4 5 6 7 8 j index 3 5 A.length 8 swap true Finished fourth Bubble Up
85
The Fifth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 4 1 A.length 8 swap false
86
The Fifth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 4 1 A.lenght 8 swap false No Swap
87
The Fifth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 4 2 A.length 8 swap false
88
The Fifth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 4 2 A.length 8 swap false No Swap
89
The Fifth Bubble Up 452314334267698 1 2 3 4 5 6 7 8 j index 4 3 A.length 8 swap false
90
Finished Early 452314334267698 1 2 3 4 5 6 7 8 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
© 2024 SlidePlayer.com Inc.
All rights reserved.