Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Oct 5, 2020
2 parents 3961877 + 2c3b2b9 commit 3c90fef
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
34 changes: 34 additions & 0 deletions Python/Algorithms/Maths/MarathiToEnglish_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'''
English_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Below is marathi numbers list
This program will convert the input number into english number
'''
marathi_digits = ['०', '१', '२', '१', '४', '५', '६', '७', '८', '९']
a = input("Enter marathi digit: ")
if a in marathi_digits:
print("English Digit: ", marathi_digits.index(a))
# It will go to this condition if marathi number is of more than one digit
else:
c = 0 # counter is to check input is valid or not
n1 = ''
for i in a:
if i in marathi_digits:
n1 += str(marathi_digits.index(i))
c = c + 1
if c != 0:
print("English Digit: ", n1)
else:
print("Enter marathi number only")

'''
OUTPUT-:Enter marathi digit:६७८
English Digit: 678
Enter marathi digit:०
English Digit: 0
Enter marathi digit: seven
Enter marathi number only
'''
25 changes: 25 additions & 0 deletions Python/Algorithms/SearchingAlgorithms/InterpolationSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def interpolationSearch(arr, lo, hi, x):
if (lo <= hi and x >= arr[lo] and x <= arr[hi]):
# Probing the position with keeping
# uniform distribution in mind.
pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) * (x - arr[lo]))
# Condition of target found
if arr[pos] == x:
return pos
# If x is larger, x is in right subarray
if arr[pos] < x:
return interpolationSearch(arr, pos + 1, hi, x)
# If x is smaller, x is in left subarray
if arr[pos] > x:
return interpolationSearch(arr, lo, pos - 1, x)
return - 1


n = int(input("Enter number of elements : "))
arr = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n]
x = int(input("Enter the element to be searched :"))
index = interpolationSearch(arr, 0, n - 1, x)
if index != -1:
print("Element found at index", index)
else:
print("Element not found")
85 changes: 82 additions & 3 deletions Python/Algorithms/SearchingAlgorithms/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
# Changelog
#### 01-10-2020:
- Added Sequential Search
<p align="center">
<img src="https://capsule-render.vercel.app/api?type=rect&color=666666&height=100&section=header&text=Search%20Algorithms%20In%20Python&fontSize=55%&fontColor=ffffff">
<h2 align="center"> <img src="searchalgos.png" align="center" /></h2>
</p>

## Introduction

Searching for data stored in different data structures is a crucial part of pretty much every single application.

There are many different algorithms available to utilize when searching, and each have different implementations and rely on different data structures to get the job done.

Being able to choose a specific algorithm for a given task is a key skill for developers and can mean the difference between a fast, reliable and stable application and an application that crumbles from a simple request.

* Linear Search
* Binary Search
* Jump Search
* Fibonacci Search
* Exponential Search
* Interpolation Search

## Linear Search

Linear search is one of the simplest searching algorithms, and the easiest to understand. We can think of it as a ramped-up version of our own implementation of Python's in operator.

The algorithm consists of iterating over an array and returning the index of the first occurrence of an item once it is found.

The time complexity of linear search is O(n), meaning that the time taken to execute increases with the number of items in our input list

## Binary Search

Binary search follows a divide and conquer methodology. It is faster than linear search but requires that the array be sorted before the algorithm is executed.

Assuming that we're searching for a value val in a sorted array, the algorithm compares val to the value of the middle element of the array, which we'll call mid.

* If mid is the element we are looking for (best case), we return its index.
* If not, we identify which side of mid val is more likely to be on based on whether val is smaller or greater than mid, and discard the other side of the array.
* We then recursively or iteratively follow the same steps, choosing a new value for mid, comparing it with val and discarding half of the possible matches in each iteration of the algorithm.

We can only pick one possibility per iteration, and our pool of possible matches gets divided by two in each iteration. This makes the time complexity of binary search O(log n).

## Jump Search

Jump Search is similar to binary search in that it works on a sorted array, and uses a similar divide and conquer approach to search through it.

It can be classified as an improvement of the linear search algorithm since it depends on linear search to perform the actual comparison when searching for a value.

Given a sorted array, instead of searching through the array elements incrementally, we search in jumps.

The time complexity of jump search is O(√n), where √n is the jump size, and n is the length of the list, placing jump search between the linear search and binary search algorithms in terms of efficiency.

## Fibonacci Search

Fibonacci search is another divide and conquer algorithm which bears similarities to both binary search and jump search. It gets its name because it uses Fibonacci numbers to calculate the block size or search range in each step.

Fibonacci numbers start with zero and follow the pattern 0, 1, 1, 2, 3, 5, 8, 13, 21... where each element is the addition of the two numbers that immediately precede it.
The algorithm works with three Fibonacci numbers at a time.

The time complexity for Fibonacci search is O(log n); the same as binary search. This means the algorithm is faster than both linear search and jump search in most cases.

## Exponential Search

Exponential search is another search algorithm that can be implemented quite simply in Python, compared to jump search and Fibonacci search which are both a bit complex. It is also known by the names galloping search, doubling search and Struzik search.

Exponential search depends on binary search to perform the final comparison of values. The algorithm works by:

* Determining the range where the element we're looking for is likely to be
* Using binary search for the range to find the exact index of the item

Exponential search runs in O(log i) time, where i is the index of the item we are searching for. In its worst case, the time complexity is O(log n), when the last item is the item we are searching for (n being the length of the array).

## Interpolation Search

Interpolation search is another divide and conquer algorithm, similar to binary search. Unlike binary search, it does not always begin searching at the middle.

The time complexity of interpolation search is O(log log n) when values are uniformly distributed. If values are not uniformly distributed, the worst-case time complexity is O(n), the same as linear search.

Interpolation search works best on uniformly distributed, sorted arrays. Whereas binary search starts in the middle and always divides into two, interpolation search calculates the likely position of the element and checks the index, making it more likely to find the element in a smaller number of iterations.

<p align="right">
<img src="https://img.shields.io/badge/Language-Python-blue?style=for-the-badge">
</p>

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3c90fef

Please sign in to comment.