Søg algoritmer forklaret med eksempler i Java, Python og C ++

Hvad er en søgealgoritme?

Denne form for algoritme ser på problemet med at omarrangere en række elementer i stigende rækkefølge. De to mest klassiske eksempler på det er binær søgning og flettsorteringsalgoritmen.

Eksponentiel søgning

Eksponentiel søgning, også kendt som fingersøgning, søger efter et element i et sorteret array ved at hoppe over   2^ielementer i hver iteration, hvor i repræsenterer værdien af ​​loop-kontrolvariablen, og derefter kontrollere, om søgeelementet er til stede mellem det sidste spring og det aktuelle hoppe.

Kompleksitet Værste tilfælde

O (log (N)) Ofte forvirret på grund af navnet, er algoritmen navngivet så ikke på grund af tidskompleksiteten. Navnet opstår som et resultat af algoritmens springende elementer med trin svarende til eksponenter på 2

Trin

  1. Spring over matrixelementerne   2^i  ad gangen og søg efter tilstanden   Array[2^(i-1)] < valueWanted < Array[2^i]. Hvis   2^i  er større end længden på arrayet, skal du indstille den øvre grænse til arrayets længde.
  2. Foretag en binær søgning mellem   Array[2^(i-1)]  og  Array[2^i]

Kode

// C++ program to find an element x in a // sorted array using Exponential search. #include  using namespace std; int binarySearch(int arr[], int, int, int); // Returns position of first ocurrence of // x in array int exponentialSearch(int arr[], int n, int x) { // If x is present at firt location itself if (arr[0] == x) return 0; // Find range for binary search by // repeated doubling int i = 1; while (i < n && arr[i] <= x) i = i*2; // Call binary search for the found range. return binarySearch(arr, i/2, min(i, n), x); } // A recursive binary search function. It returns // location of x in given array arr[l..r] is // present, otherwise -1 int binarySearch(int arr[], int l, int r, int x) { if (r>= l) { int mid = l + (r - l)/2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it // can only be present n left subarray if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid+1, r, x); } // We reach here when element is not present // in array return -1; } int main(void) { int arr[] = {2, 3, 4, 10, 40}; int n = sizeof(arr)/ sizeof(arr[0]); int x = 10; int result = exponentialSearch(arr, n, x); (result == -1)? printf("Element is not present in array") : printf("Element is present at index %d", result); return 0; } 

Søgning efter sammenkædede lister mod arrays

Antag, at du er nødt til at søge efter et element i en   usorteret  sammenkædet liste og matrix. I så fald skal du foretage en lineær søgning (husk, usorteret). At foretage en lineær søgning efter et element i begge datastrukturer vil være en O (n) -operation.

Hvis du nu har en   sorteret  sammenkædet liste og matrix, kan du stadig søge i begge datastrukturer i O (log n) tid ved hjælp af binær søgning. Selvom det vil være lidt kedeligt at kode, mens du bruger sammenkædede lister.

Tilknyttede lister foretrækkes normalt frem for arrays, hvor indsættelse er en hyppig operation. Det er lettere at indsætte i sammenkædede lister, da kun en markør ændres. Men for at indsætte i en matrix (midten eller begyndelsen) skal du flytte alle elementerne efter det element, du indsætter. Et andet sted, hvor du skal bruge sammenkædede lister, er hvor størrelsen er usikker (du kender ikke størrelsen, når du starter), fordi arrays har fast størrelse.

Arrays giver nogle få fordele i forhold til sammenkædede lister:

  1. Tilfældig adgang
  2. Mindre hukommelse sammenlignet med sammenkædede lister
  3. Arrays har bedre cache-lokalitet, hvilket giver bedre ydeevne

Det afhænger helt af brugen af, om arrays eller sammenkædede lister er bedre.

Lineær søgning

Antag, at du får en liste eller en række elementer. Du søger efter et bestemt emne. Hvordan gør du det?

Find tallet 13 på den givne liste.

Lineær søgning 1

Du ser bare på listen, og der er den!

Lineær søgning 2

Nu, hvordan fortæller du en computer at finde den.

En computer kan ikke se mere end værdien på et givet tidspunkt. Så det tager et element fra arrayet og kontrollerer, om det er det samme som det, du leder efter.

Lineær søgning 3

Det første element matchede ikke. Så gå videre til den næste.

Lineær søgning 4

Og så videre...

Dette gøres, indtil der er fundet et match, eller indtil alle emner er blevet kontrolleret.

Lineær søgning 5

I denne algoritme kan du stoppe, når varen findes, og så er der ingen grund til at se længere.

Så hvor lang tid ville det tage at udføre den lineære søgning? I bedste fald kan du være heldig, og den vare, du ser på, måske på den første position i arrayet! Men i værste fald bliver du nødt til at se på hver eneste vare, før du finder varen på det sidste sted, eller inden du indser, at varen ikke er i arrayet.

Kompleksiteten af ​​den lineære søgning er derfor O (n).

Hvis det element, der skal søges, findes på den første hukommelsesblok, vil kompleksiteten være O (1).

Koden til en lineær søgefunktion i JavaScript er vist nedenfor. Denne funktion returnerer positionen for det element, vi leder efter i arrayet. Hvis elementet ikke er til stede i arrayet, returnerer funktionen null.

int linearSearch(int arr[], int num) { int len = (int)( sizeof(arr) / sizeof(arr[0]); int *a = arr; for(int i = 0; i < len; i++) { if(*(a+i) == num) return i; } return -1; } 

Eksempel i JavaScript

function linearSearch(arr, item) { // Go through all the elements of arr to look for item. for (var i = 0; i < arr.length; i++) { if (arr[i] === item) { // Found it! return i; } } // Item not found in the array. return null; } 

Eksempel i Ruby

def linear_search(target, array) counter = 0 while counter < array.length if array[counter] == target return counter else counter += 1 end end return nil end 

Eksempel i C ++

int linear_search(int arr[],int n,int num) { for(int i=0;i
    

Example in Python

def linear_search(array, num): for index, element in enumerate(array): if element == num: return index return -1 

Example in Swift

func linearSearch(for number: Int, in array: [Int]) -> Int? { for (index, value) in array.enumerated() { if value == number { return index } // return the index of the number } return nil // the number was not found in the array } 

Example in Java

int linearSearch(int[] arr, int element) { for(int i=0;i
     

Example in PHP

function linear_search($arr=[],$num=0) { $n = count($arr); for( $i=0; $i<$n; $i++){ if($arr[$i] == $num) return $i; } // Item not found in the array return -1; } $arr = array(1,3,2,8,5,7,4,0); print("Linear search result for 2: "); echo linear_search($arr,2); 

Global Linear Search

What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.

Target = 5

Array = [ 1, 2, 3, 4, 5, 6, 5, 7, 8, 9, 5]

This array has 3 occurances of 5s and we want to return the indexes (where they are in the array) of all of them. This is called global linear search. You will need to adjust your code to return an array of the index points at which it finds the target element. When you find an index element that matches your target, the index point (counter) will be added in the results array. If it doesn’t match the code will continue to move on to the next element in the array by adding 1 to the counter.

def global_linear_search(target, array) counter = 0 results = [] while counter < array.length if array[counter] == target results << counter counter += 1 else counter += 1 end end if results.empty? return nil else return results end end 

Why linear search is not efficient

There is no doubt that linear search is simple but because it compares each element one by one, it is time consuming and hence not very efficient. If we have to find a number from say, 1000000 numbers and number is at the last location, linear search technique would become quite tedious. So, also learn about binary search, exponential search, etc. which are much more efficient than linear search.

Binary Search

A binary search locates an item in a sorted array by repeatedly dividing the search interval in half.

How do you search a name in a telephone directory?

One way would be to start from the first page and look at each name in the phonebook till we find what we are looking for. But that would be an extremely laborious and inefficient way to search.

Because we know that names in the phonebook are sorted alphabetically, we could probably work along the following steps:

  1. Open the middle page of the phonebook
  2. If it has the name we are looking for, we are done!
  3. Otherwise, throw away the half of the phonebook that does not contain the name
  4. Repeat until you find the name or there are no more pages left in the phonebook

[

Binary vs Linear Search

]

Time complexity: As we dispose off one part of the search case during every step of binary search, and perform the search operation on the other half, this results in a worst case time complexity of  O ( log2N ). The best case occurs when the element to be found is in the middle of the list. The best case time complexity is  O ( 1 ).

Space complexity: Binary search takes constant or  O ( 1 ) space meaning that we don't do any input size related variable defining.

for small sets linear search is better but in larger ones it is way more efficient to use binary search.

In detail, how many times can you divide N by 2 until you have 1? This is essentially saying, do a binary search (half the elements) until you found it. In a formula this would be this:

1 = N / 2^x 

Multiply by 2x:

2^x = N 

Now do the log2:

log2(2^x) = log2 N x * log2(2) = log2 N x * 1 = log2 N 

This means you can divide log N times until you have everything divided. Which means you have to divide log N ("do the binary search step") until you found your element.

O ( log2N ) is such so because at every step half of the elements in the data set are gone which is justified by the base of the logarithmic function.

This is the binary search algorithm. It is elegant and efficient but for it to work correctly, the array must be  sorted .

Find 5 in the given array of numbers using binary search.

Binary Search 1

Mark low, high and mid positions in the array.

Binary Search 2

Compare the item you are looking for with the middle element.

Binary Search 3

Throw away the left half and look in the right half.

Binary Search 4

Again compare with the middle element.

Binary Search 5

Now, move to the left half.

Binary Search 6

The middle element is the item we were looking for!

The binary search algorithm takes a divide-and-conquer approach where the array is continuously divided until the item is found or until there are no more elements left for checking. Hence, this algorithm can be defined recursively to generate an elegant solution.

The two base cases for recursion would be:

  • No more elements left in the array
  • Item is found

The Power of Binary Search in Data Systems (B+ trees): Binary Search Trees are very powerful because of their O(log n) search times, second to the hashmap data structure which uses a hashing key to search for data in O(1). It is important to understand how the log n run time comes from the height of a binary search tree. If each node splits into two nodes, (binary), then the depth of the tree is log n (base 2).. In order to improve this speed in Data System, we use B+ trees because they have a larger branching factor, and therefore more height. I hope this short article helps expand your mind about how binary search is used in practical systems.

The code for recursive binary search is shown below:

JavaScript implementation

function binarySearch(arr, item, low, high) { if (low > high) { // No more elements in the array. return null; } // Find the middle of the array. var mid = Math.ceil((low + high) / 2); if (arr[mid] === item) { // Found the item! return mid; } if (item < arr[mid]) { // Item is in the half from low to mid-1. return binarySearch(arr, item, low, mid-1); } else { // Item is in the half from mid+1 to high. return binarySearch(arr, item, mid+1, high); } } var numbers = [1,2,3,4,5,6,7]; print(binarySearch(numbers, 5, 0, numbers.length-1)); 

Here is another implementation in JavaScript:

function binary_search(a, v) { function search(low, high) { if (low === high) { return a[low] === v; } else  var mid = math_floor((low + high) / 2); return (v === a[mid])  } return search(0, array_length(a) - 1); } 

Ruby implementation

def binary_search(target, array) sorted_array = array.sort low = 0 high = (sorted_array.length) - 1 while high >= low middle = (low + high) / 2 if target > sorted_array[middle] low = middle + 1 elsif target < sorted_array[middle] high = middle - 1 else return middle end end return nil end 

Example in C

int binarySearch(int a[], int l, int r, int x) { if (r >= l){ int mid = (l + (r - l))/2; if (a[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); return binarySearch(arr, mid+1, r, x); } return -1; } 

Python implementation

def binary_search(arr, l, r, target): if r >= l: mid = (l + (r - l))/2 if arr[mid] == target: return mid elif arr[mid] > target: return binary_search(arr, l, mid-1, target) else: return binary_search(arr, mid+1, r, target) else: return -1 

Example in C++

Recursive approach!

// Recursive approach in C++ int binarySearch(int arr[], int start, int end, int x) { if (end >= start) { int mid = (start + (end - start))/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, start, mid-1, x); return binarySearch(arr, mid+1, end, x); } return -1; } 

Iterative approach!

int binarySearch(int arr[], int start, int end, int x) { while (start <= end) { int mid = (start + (end - start))/2; if (arr[mid] == x) return mid; if (arr[mid] < x) start = mid + 1; else end = mid - 1; } return -1; } 

Example in Swift

func binarySearch(for number: Int, in numbers: [Int]) -> Int? { var lowerBound = 0 var upperBound = numbers.count while lowerBound < upperBound { let index = lowerBound + (upperBound - lowerBound) / 2 if numbers[index] == number { return index // we found the given number at this index } else if numbers[index] < number { lowerBound = index + 1 } else { upperBound = index } } return nil // the given number was not found } 

Example in Java

// Iterative Approach in Java int binarySearch(int[] arr, int start, int end, int element) { while(start <= end) { int mid = start + ( end - start ) / 2; if(arr[mid] == element) return mid; if(arr[mid] < element) start = mid+1; else end = mid-1; } return -1; } 
// Recursive Approach in Java int binarySearch(int[] arr, int start,int end , int element) { if (end >= start) { int mid = start + ( end - start ) / 2; if(arr[mid] == element) return mid; if(arr[mid] < element) return binarySearch( arr , mid + 1 , end , element ); else return binarySearch( arr, start, mid - 1 , element); } return -1; }