diff --git a/README.md b/README.md index 9bb1972..3b12ffa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -

Algorithm

- -Algorithm implemented in Python. +

Algorithm

+ +Algorithm implemented in Python. diff --git a/dp/dice_combinations.py b/dp/dice_combinations.py index fc59378..e4453ba 100644 --- a/dp/dice_combinations.py +++ b/dp/dice_combinations.py @@ -1,34 +1,34 @@ -''' -Dice Combinations - -Task is to count the number of ways to construct sum n -by throwing a dice one or more times. Each throw produces -an outcome between 1 and 6. - -For example, if n=3, there are 4 ways: -* 1+1+1 -* 1+2 -* 2+1 -* 3 - -''' - -n = int(input("Enter the value of n: ")) - -dp = [0] * (n+1) -dp[0] = 1 - -for i in range(1,n+1): - dp[i] += dp[i-1] - if i>1: - dp[i]+=dp[i-2] - if i>2: - dp[i]+=dp[i-3] - if i>3: - dp[i]+=dp[i-4] - if i>4: - dp[i]+=dp[i-5] - if i>4: - dp[i]+=dp[i-6] - -print(dp[n]) +''' +Dice Combinations + +Task is to count the number of ways to construct sum n +by throwing a dice one or more times. Each throw produces +an outcome between 1 and 6. + +For example, if n=3, there are 4 ways: +* 1+1+1 +* 1+2 +* 2+1 +* 3 + +''' + +n = int(input("Enter the value of n: ")) + +dp = [0] * (n+1) +dp[0] = 1 + +for i in range(1,n+1): + dp[i] += dp[i-1] + if i>1: + dp[i]+=dp[i-2] + if i>2: + dp[i]+=dp[i-3] + if i>3: + dp[i]+=dp[i-4] + if i>4: + dp[i]+=dp[i-5] + if i>4: + dp[i]+=dp[i-6] + +print(dp[n]) diff --git a/dp/fibonacci.py b/dp/fibonacci.py index 839d0c8..2eefedf 100644 --- a/dp/fibonacci.py +++ b/dp/fibonacci.py @@ -1,19 +1,19 @@ -''' -Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, .... -Fib(0) = 0 -Fib(1) = 1 -Fib(N) = Fib(N-1) + Fib(N-2) for N>1 - -This program uses dynamic programming to solve this -Time complexity : O(N) -''' - -n = int(input("Howmany terms of series do you want to print? ")) - -dp = [0,1] - -for i in range(n-1): - dp.append( dp[i] + dp[i+1] ) - -print("The first n terms of Fibonacci series are: ") -print(*dp) +''' +Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, .... +Fib(0) = 0 +Fib(1) = 1 +Fib(N) = Fib(N-1) + Fib(N-2) for N>1 + +This program uses dynamic programming to solve this +Time complexity : O(N) +''' + +n = int(input("Howmany terms of series do you want to print? ")) + +dp = [0,1] + +for i in range(n-1): + dp.append( dp[i] + dp[i+1] ) + +print("The first n terms of Fibonacci series are: ") +print(*dp) diff --git a/searching/binarySearch.py b/searching/binarySearch.py index 881aa56..e03eaf7 100644 --- a/searching/binarySearch.py +++ b/searching/binarySearch.py @@ -1,36 +1,36 @@ - - -def binarySearch (arr, l, r, x): - - - if r >= l: - - mid = l + (r - l) // 2 - - - if arr[mid] == x: - return mid - - - elif arr[mid] > x: - return binarySearch(arr, l, mid-1, x) - - - else: - return binarySearch(arr, mid + 1, r, x) - - else: - - return -1 - -n = int(input("Enter the size of array : ")) - -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -x=int(input("Enter the element to search : ")) - -result = binarySearch(arr, 0, len(arr)-1, x) - -if result != -1: - print ("Element is present at index % d" % result) -else: + + +def binarySearch (arr, l, r, x): + + + if r >= l: + + mid = l + (r - l) // 2 + + + if arr[mid] == x: + return mid + + + elif arr[mid] > x: + return binarySearch(arr, l, mid-1, x) + + + else: + return binarySearch(arr, mid + 1, r, x) + + else: + + return -1 + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +x=int(input("Enter the element to search : ")) + +result = binarySearch(arr, 0, len(arr)-1, x) + +if result != -1: + print ("Element is present at index % d" % result) +else: print ("Element is not present in array") \ No newline at end of file diff --git a/searching/fibonacciSearch.py b/searching/fibonacciSearch.py index d317355..a518c74 100644 --- a/searching/fibonacciSearch.py +++ b/searching/fibonacciSearch.py @@ -1,39 +1,39 @@ -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 - - -n = int(input("Enter the size of array : ")) -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -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"); +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 + + +n = int(input("Enter the size of array : ")) +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +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"); diff --git a/searching/interpolationSearch.py b/searching/interpolationSearch.py new file mode 100644 index 0000000..eae1992 --- /dev/null +++ b/searching/interpolationSearch.py @@ -0,0 +1,36 @@ + + +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 + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +x=int(input("Enter element to search : ")) + +index = interpolationSearch(arr, 0, n - 1, x) + +if index != -1: + print("Element found at index", index) +else: + print("Element not found") \ No newline at end of file diff --git a/searching/jumpSearch.py b/searching/jumpSearch.py index 8084660..c487bf9 100644 --- a/searching/jumpSearch.py +++ b/searching/jumpSearch.py @@ -1,40 +1,40 @@ -import math - -def jumpSearch( arr , x , n ): - - - step = math.sqrt(n) - - - prev = 0 - while arr[int(min(step, n)-1)] < x: - prev = step - step += math.sqrt(n) - if prev >= n: - return -1 - - - while arr[int(prev)] < x: - prev += 1 - - - if prev == min(step, n): - return -1 - - - if arr[int(prev)] == x: - return prev - - return -1 - - -n = int(input("Enter the size of array : ")) - -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -x=int(input("Enter element to search : ")) - - -index = jumpSearch(arr, x, n) - - +import math + +def jumpSearch( arr , x , n ): + + + step = math.sqrt(n) + + + prev = 0 + while arr[int(min(step, n)-1)] < x: + prev = step + step += math.sqrt(n) + if prev >= n: + return -1 + + + while arr[int(prev)] < x: + prev += 1 + + + if prev == min(step, n): + return -1 + + + if arr[int(prev)] == x: + return prev + + return -1 + + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +x=int(input("Enter element to search : ")) + + +index = jumpSearch(arr, x, n) + + print("Number" , x, "is at index" ,"%.0f"%index) \ No newline at end of file diff --git a/searching/linearSearch.py b/searching/linearSearch.py index c15ca14..a312260 100644 --- a/searching/linearSearch.py +++ b/searching/linearSearch.py @@ -1,16 +1,16 @@ -def search(arr, n, x): - for i in range(0, n): - if (arr[i] == x): - return i - return -1 - -n = int(input("Enter the size of array : ")) -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -x=int(input("Enter the element to search : ")) - -result = search(arr, n, x) - -if(result == -1): - print("Element is not present in array") -else: - print("Element is present at index", result) +def search(arr, n, x): + for i in range(0, n): + if (arr[i] == x): + return i + return -1 + +n = int(input("Enter the size of array : ")) +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +x=int(input("Enter the element to search : ")) + +result = search(arr, n, x) + +if(result == -1): + print("Element is not present in array") +else: + print("Element is present at index", result) diff --git a/sorting/bubbleSort.py b/sorting/bubbleSort.py index ff3033c..f0cd73a 100644 --- a/sorting/bubbleSort.py +++ b/sorting/bubbleSort.py @@ -1,18 +1,18 @@ -def bubbleSort(arr): - n = len(arr) - for i in range(n-1): - for j in range(0, n-i-1): - if arr[j] > arr[j+1]: - arr[j], arr[j+1] = arr[j+1], arr[j] - -n = int(input("Enter the size of array : ")) - -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] - -print("Array entered") -print(arr) - -bubbleSort(arr) - -print("Array after sorting the elements") -print(arr) +def bubbleSort(arr): + n = len(arr) + for i in range(n-1): + for j in range(0, n-i-1): + if arr[j] > arr[j+1]: + arr[j], arr[j+1] = arr[j+1], arr[j] + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] + +print("Array entered") +print(arr) + +bubbleSort(arr) + +print("Array after sorting the elements") +print(arr) diff --git a/sorting/countingSort.py b/sorting/countingSort.py index d5fb3ca..8fb870a 100644 --- a/sorting/countingSort.py +++ b/sorting/countingSort.py @@ -1,26 +1,26 @@ -def countingSort(arr): - n = len(arr) - max_of_arr = max(arr) - count = [0 for i in range(max_of_arr+1)] - for i in arr: - count[i] += 1 - ind = 0 - for i in range(max_of_arr+1): - c = count[i] - while c: - arr[ind] = i - ind += 1 - c -= 1 - return arr - -n = int(input("Enter the size of array : ")) - -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] - -print("Array entered") -print(arr) - -countingSort(arr) - -print("Array after sorting the elements") -print(arr) +def countingSort(arr): + n = len(arr) + max_of_arr = max(arr) + count = [0 for i in range(max_of_arr+1)] + for i in arr: + count[i] += 1 + ind = 0 + for i in range(max_of_arr+1): + c = count[i] + while c: + arr[ind] = i + ind += 1 + c -= 1 + return arr + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] + +print("Array entered") +print(arr) + +countingSort(arr) + +print("Array after sorting the elements") +print(arr) diff --git a/sorting/insertionSort.py b/sorting/insertionSort.py index d90588b..8a84203 100644 --- a/sorting/insertionSort.py +++ b/sorting/insertionSort.py @@ -1,22 +1,22 @@ -def insertionSort(arr): - n = len(arr) - for i in range(n): - key = arr[i] - j = i-1 - while j >= 0 and key < arr[j] : - arr[j + 1] = arr[j] - j -= 1 - arr[j+1] = key - return arr - -n = int(input("Enter the size of array : ")) - -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] - -print("Array entered") -print(arr) - -arr = insertionSort(arr) - -print("Array after sorting the elements") -print(arr) +def insertionSort(arr): + n = len(arr) + for i in range(n): + key = arr[i] + j = i-1 + while j >= 0 and key < arr[j] : + arr[j + 1] = arr[j] + j -= 1 + arr[j+1] = key + return arr + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] + +print("Array entered") +print(arr) + +arr = insertionSort(arr) + +print("Array after sorting the elements") +print(arr) diff --git a/sorting/mergeSort.py b/sorting/mergeSort.py index 26a1e12..9da036b 100644 --- a/sorting/mergeSort.py +++ b/sorting/mergeSort.py @@ -1,34 +1,34 @@ -def mergeSort(arr): - if len(arr) > 1: - mid = len(arr)//2 - L = arr[:mid] - R = arr[mid:] - mergeSort(L) - mergeSort(R) - i = j = k = 0 - while i < len(L) and j < len(R): - if L[i] < R[j]: - arr[k] = L[i] - i += 1 - else: - arr[k] = R[j] - j += 1 - k += 1 - while i < len(L): - arr[k] = L[i] - i += 1 - k += 1 - while j < len(R): - arr[k] = R[j] - j += 1 - k += 1 - - -n = int(input("Enter the size of array : ")) - -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -print("Array Before") -print(arr) -mergeSort(arr) -print("Array After") -print(arr) +def mergeSort(arr): + if len(arr) > 1: + mid = len(arr)//2 + L = arr[:mid] + R = arr[mid:] + mergeSort(L) + mergeSort(R) + i = j = k = 0 + while i < len(L) and j < len(R): + if L[i] < R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 + + +n = int(input("Enter the size of array : ")) + +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +print("Array Before") +print(arr) +mergeSort(arr) +print("Array After") +print(arr) diff --git a/sorting/pancakeSort.py b/sorting/pancakeSort.py index 5a09183..8b9e2a2 100644 --- a/sorting/pancakeSort.py +++ b/sorting/pancakeSort.py @@ -1,33 +1,33 @@ -def flip(arr, i): - start = 0 - while start < i: - temp = arr[start] - arr[start] = arr[i] - arr[i] = temp - start += 1 - i -= 1 - -def findMax(arr, n): - mi = 0 - for i in range(0,n): - if arr[i] > arr[mi]: - mi = i - return mi - -def pancakeSort(arr, n): - curr_size = n - while curr_size > 1: - mi = findMax(arr, curr_size) - if mi != curr_size-1: - flip(arr, mi) - flip(arr, curr_size-1) - curr_size -= 1 - - -n = int(input("Enter the size of array : ")) -arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -print("Before") -print(arr) -pancakeSort(arr, n); -print ("Sorted Array ") -print(arr) +def flip(arr, i): + start = 0 + while start < i: + temp = arr[start] + arr[start] = arr[i] + arr[i] = temp + start += 1 + i -= 1 + +def findMax(arr, n): + mi = 0 + for i in range(0,n): + if arr[i] > arr[mi]: + mi = i + return mi + +def pancakeSort(arr, n): + curr_size = n + while curr_size > 1: + mi = findMax(arr, curr_size) + if mi != curr_size-1: + flip(arr, mi) + flip(arr, curr_size-1) + curr_size -= 1 + + +n = int(input("Enter the size of array : ")) +arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +print("Before") +print(arr) +pancakeSort(arr, n); +print ("Sorted Array ") +print(arr) diff --git a/sorting/quickSort.py b/sorting/quickSort.py index 06d88f7..b92156b 100644 --- a/sorting/quickSort.py +++ b/sorting/quickSort.py @@ -1,29 +1,29 @@ -def partition(start, end, array): - pivot_index = start - pivot = array[pivot_index] - while start < end: - while start < len(array) and array[start] <= pivot: - start += 1 - while array[end] > pivot: - end -= 1 - if(start < end): - array[start], array[end] = array[end], array[start] - array[end], array[pivot_index] = array[pivot_index], array[end] - return end - -def quick_sort(start, end, array): - if (start < end): - p = partition(start, end, array) - quick_sort(start, p - 1, array) - quick_sort(p + 1, end, array) - - -n = int(input("Enter the size of array : ")) -array = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] -print("Before") -print(arr) - -quick_sort(0, len(array) - 1, array) - -print("After sorting") -print(arr) +def partition(start, end, array): + pivot_index = start + pivot = array[pivot_index] + while start < end: + while start < len(array) and array[start] <= pivot: + start += 1 + while array[end] > pivot: + end -= 1 + if(start < end): + array[start], array[end] = array[end], array[start] + array[end], array[pivot_index] = array[pivot_index], array[end] + return end + +def quick_sort(start, end, array): + if (start < end): + p = partition(start, end, array) + quick_sort(start, p - 1, array) + quick_sort(p + 1, end, array) + + +n = int(input("Enter the size of array : ")) +array = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] +print("Before") +print(arr) + +quick_sort(0, len(array) - 1, array) + +print("After sorting") +print(arr) diff --git a/sorting/selectionSort.py b/sorting/selectionSort.py index 696e2cd..670a41c 100644 --- a/sorting/selectionSort.py +++ b/sorting/selectionSort.py @@ -1,22 +1,22 @@ - -import sys - -n = int(input("Enter the size of array : ")) - -A = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] - - -for i in range(len(A)): - - - min_idx = i - for j in range(i+1, len(A)): - if A[min_idx] > A[j]: - min_idx = j - - - A[i], A[min_idx] = A[min_idx], A[i] - - -print ("Sorted array") -print(A) + +import sys + +n = int(input("Enter the size of array : ")) + +A = list(map(int, input("Enter the array elements :\n").strip().split()))[:n] + + +for i in range(len(A)): + + + min_idx = i + for j in range(i+1, len(A)): + if A[min_idx] > A[j]: + min_idx = j + + + A[i], A[min_idx] = A[min_idx], A[i] + + +print ("Sorted array") +print(A)