3 searching algorithms in java

15
using Java 2015 Data Structure Prepared by: Mahmoud Rafeek Al-farra in Java 3. Searching Algorithms

Upload: mahmoud-alfarra

Post on 20-Mar-2017

607 views

Category:

Education


2 download

TRANSCRIPT

Page 1: 3 searching algorithms in Java

using Java

2015

Data Structure Prepared by: Mahmoud Rafeek Al-farra

in Java

3. Searching Algorithms

Page 2: 3 searching algorithms in Java

mfarra.cst.ps www.fb.com/MahmoudRFarra

Contents

Which is best ?

Binary Search approach

Linear search approach

Introduction

Page 3: 3 searching algorithms in Java

Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra

Searching for data is a fundamental computer programming task and one that has been studied for many years.

There are two fundamental ways to search for data in a list:The sequential (linear) search approach.The binary search approuach.

Page 4: 3 searching algorithms in Java

Linear search approachmfarra.cst.ps www.fb.com/MahmoudRFarra

The linear search approach compares the key element sequentially with each element in the data structure.

It continues to do so until the key matches an element in the DS or the DS is exhausted without a match being found.

If a match is made, the linear search returns the index of the element in the DS that matches the key.

If no match is found, the search returns -1.

Page 5: 3 searching algorithms in Java

Linear search approachmfarra.cst.ps www.fb.com/MahmoudRFarra

2 5 81

88

90

997 9 3

455

56

66

79

80

1 2 3 4 5 6 7 8 9 10 11 12 13

Wanted = 80

array0

؟ ==

==

....؟== ؟

....

The wanted value at comparison number 10 and position 9

You can see an animated example from the following link:http://www.cs.armstrong.edu/liang/animation/web/LinearSearch.html

Page 6: 3 searching algorithms in Java

Linear search approachmfarra.cst.ps www.fb.com/MahmoudRFarra

1. public class LinearSearch {2. /** The method for finding a key in the list */3. public static int linearSearch(int[] list, int key) {4. for (int i = 0; i < list.length; i++) {5. if (key == list[i])6. return i;7. }8. return -1;9. }10. }

Page 7: 3 searching algorithms in Java

Binary search approachmfarra.cst.ps www.fb.com/MahmoudRFarra

When the records you are searching through are sorted into order, you can perform a more efficient search than the sequential search to find a value. This search is called a binary search.

To use this algorithm, we first need our data stored in order (ascending, preferably) in an array.

You can see an animated example from the following link:http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html

Page 8: 3 searching algorithms in Java

Binary Search approachmfarra.cst.ps www.fb.com/MahmoudRFarra

Page 9: 3 searching algorithms in Java

Binary Search approach

2 5 81 88 90 997 9 34 55 56 66 79 801 2 3 4 5 6 7 8 9 10 11 12 13

(0+13) / 2 = 6If (array[6] == 80)return 6Elseif (array[6] > 80)High = 6 -1Else Low = 6+1

Middle = (low + high)/2

Wanted = 80

array0

(7+13) / 2 = 10If (array[10] == 80)return 10Elseif (array[10] > 80)High = 10 -1Else Low = 10+1

(7+9) / 2 = 8If (array[8] == 80)return 8Elseif (array[8] > 80)High = 8-1Else Low = 8+11 2 3

mfarra.cst.ps www.fb.com/MahmoudRFarra

Page 10: 3 searching algorithms in Java

Binary Search approach

2 5 81 88 90 997 9 34 55 56 66 79 801 2 3 4 5 6 7 8 9 10 11 12 13

Middle = (low + high)/2

Wanted = 80

array0

(7+9) / 2 = 8If (array[8] == 80)return 8Elseif (array[8] > 80)High = 8-1Else Low = 8+13

(9+9) / 2 = 9If (array[9] == 80)return 8Elseif (array[9] > 80)High = 9-1Else Low = 9+14

return 8

mfarra.cst.ps www.fb.com/MahmoudRFarra

Page 11: 3 searching algorithms in Java

Binary Search approachmfarra.cst.ps www.fb.com/MahmoudRFarra

1. public class BinarySearch {2. public static int binarySearch(int[] list, int key) {3. int low = 0;4. int high = list.length - 1;5. while (high >= low) {6. int mid = (low + high) / 2;7. if (key < list[mid])8. high = mid - 1;9. else if (key == list[mid])10. return mid;11. else12. low = mid + 1;13. }14. return –low - 1; // Now high < low, key not found 15. }16. }

Page 12: 3 searching algorithms in Java

Which is best ?mfarra.cst.ps www.fb.com/MahmoudRFarra

The best Complexity

Data structure

Suitabledata

Size ofdata

Page 13: 3 searching algorithms in Java

Which is best ?mfarra.cst.ps www.fb.com/MahmoudRFarra

If an array is sorted, binary search is more efficient than linear search for finding an element in the array.

Linear search is useful for finding an element in a small array or an unsorted array, but it is inefficient for large arrays.

Binary search is more efficient, but it requires that the array be presorted.

Page 14: 3 searching algorithms in Java

Which is best ?mfarra.cst.ps www.fb.com/MahmoudRFarra

Complexity time of linear approach is: O(n)Complexity time of binary approach is: O( log(n)) Supported data structure of linear: ? Supported data structure of Binary: ?

Page 15: 3 searching algorithms in Java

using Java

2015

FB: M a h m o u d R F a r r aYouTube: M a h m o u d R F a r SlidesShare: mralfarra

Thank you