Delete Searching algorithms directory

pull/272/head
Kavin18 2024-05-17 15:16:56 +05:30 zatwierdzone przez GitHub
rodzic e96b9fec0e
commit 4856f30309
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
9 zmienionych plików z 0 dodań i 274 usunięć

Wyświetl plik

@ -1,31 +0,0 @@
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
mid_val = arr[mid]
if mid_val == target:
return mid
elif mid_val < target:
low = mid + 1
else:
high = mid - 1
return -1
def main():
arr = list(map(int, input("Enter the array elements: ").split()))
arr.sort()
target = int(input("Enter the number to search for: "))
index = binary_search(arr, target)
if index != -1:
print(f"{target} found at index {index}.")
else:
print(f"{target} not found.")
if __name__ == "__main__":
main()

Wyświetl plik

@ -1,39 +0,0 @@
def binarySearch( arr, l, r, x):
if r >= l:
mid = l + ( r-l ) // 2
if arr[mid] == x:
return mid
if arr[mid] > x:
return binarySearch(arr, l,
mid - 1, x)
return binarySearch(arr, mid + 1, r, x)
return -1
def exponentialSearch(arr, n, x):
if arr[0] == x:
return 0
i = 1
while i < n and arr[i] <= x:
i = i * 2
return binarySearch( arr, i // 2,
min(i, n-1), x)
arr = list(map(int,input("Enter the array elements: ").split()))
n = len(arr)
x = int(input("enter the number to search: "))
result = exponentialSearch(arr, n, x)
if result == -1:
print ("Element not found in the array")
else:
print ("Element is present at index %d" %(result))

Wyświetl plik

@ -1,49 +0,0 @@
from bisect import bisect_left
def fibMonaccianSearch(arr, x, n):
fibMMm2 = 0
fibMMm1 = 1
fibM = fibMMm2 + fibMMm1
while (fibM < n):
fibMMm2 = fibMMm1
fibMMm1 = fibM
fibM = fibMMm2 + fibMMm1
offset = -1
while (fibM > 1):
i = min(offset+fibMMm2, n-1)
if (arr[i] < x):
fibM = fibMMm1
fibMMm1 = fibMMm2
fibMMm2 = fibM - fibMMm1
offset = i
elif (arr[i] > x):
fibM = fibMMm2
fibMMm1 = fibMMm1 - fibMMm2
fibMMm2 = fibM - fibMMm1
else:
return i
if(fibMMm1 and arr[n-1] == x):
return n-1
return -1
arr = list(map(int,input("Enter the array elements: ").split()))
n = len(arr)
x = int(input("enter the number to search: "))
ind = fibMonaccianSearch(arr, x, n)
if ind>=0:
print("Found at index:",ind)
else:
print(x,"isn't present in the array");

Wyświetl plik

@ -1,32 +0,0 @@
def interpolationSearch(arr, lo, hi, x):
if (lo <= hi and x >= arr[lo] and x <= arr[hi]):
pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) *
(x - arr[lo]))
if arr[pos] == x:
return pos
if arr[pos] < x:
return interpolationSearch(arr, pos + 1,
hi, x)
if arr[pos] > x:
return interpolationSearch(arr, lo,
pos - 1, x)
return -1
arr = list(map(int,input("Enter the array elements: ").split()))
n = len(arr)
x = int(input("enter the number to search: "))
index = interpolationSearch(arr, 0, n - 1, x)
if index != -1:
print("Element found at index", index)
else:
print("Element not found")

Wyświetl plik

@ -1,33 +0,0 @@
arr = list(map(int, input("Enter the array elements : ").split()))
import math
def jump_search(arr, target):
n = len(arr)
arr.sort() # Ensure array is sorted
step = int(math.sqrt(n))
prev = 0
while arr[min(step, n)-1] < target:
prev = step
step += int(math.sqrt(n))
if prev >= n:
return -1
while arr[prev] < target:
prev += 1
if prev == min(step, n):
return -1
if arr[prev] == target:
return prev
return -1
target = int(input("Enter the number to search for: "))
index = jump_search(arr, target)
if index != -1:
print(f"{target} found at index {index}.")
else:
print(f"{target} not found in the array.")

Wyświetl plik

@ -1,18 +0,0 @@
def search(arr, N, x):
for i in range(0, N):
if (arr[i] == x):
return i
return -1
if __name__ == "__main__":
arr = list(map(int,input("enter the array elements: ").split()))
x = int(input("enter the number to search: "))
N = len(arr)
result = search(arr, N, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)

Wyświetl plik

@ -1,18 +0,0 @@
import math
def bsearch(A, key_to_search):
n = len(A)
lg = int(math.log2(n-1)) + 1
pos = 0
for i in range(lg - 1, -1, -1):
if A[pos] == key_to_search:
return pos
new_pos = pos | (1 << i)
if new_pos < n and A[new_pos] <= key_to_search:
pos = new_pos
return pos if A[pos] == key_to_search else -1
if __name__ == "__main__":
A = list(map(int, input("Enter the array elements: ").split()))
key = int(input("Enter the number to search: "))
print(bsearch(A, key))

Wyświetl plik

@ -1,22 +0,0 @@
def sentinelSearch(arr, n, key):
last = arr[n - 1]
arr[n - 1] = key
i = 0
while (arr[i] != key):
i += 1
arr[n - 1] = last
if ((i < n - 1) or (arr[n - 1] == key)):
print(key, "is present at index", i)
else:
print("Element Not found")
arr = list(map(int,input("Enter the array elements: ").split()))
n = len(arr)
key = int(input("enter the number to search: "))
sentinelSearch(arr, n, key)

Wyświetl plik

@ -1,32 +0,0 @@
import math as mt
def ternarySearch(l, r, key, ar):
if (r >= l):
mid1 = l + (r - l) //3
mid2 = r - (r - l) //3
if (ar[mid1] == key):
return mid1
if (ar[mid2] == key):
return mid2
if (key < ar[mid1]):
return ternarySearch(l, mid1 - 1, key, ar)
elif (key > ar[mid2]):
return ternarySearch(mid2 + 1, r, key, ar)
else:
return ternarySearch(mid1 + 1, mid2 - 1, key, ar)
return -1
l, r = 0, 9
ar = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
l = 0
r = 9
key = 5
p = ternarySearch(l, r, key, ar)
print("Index of", key, "is", p)
key = 50
p = ternarySearch(l, r, key, ar)
print("Index of", key, "is", p)