Skip to content

Commit

Permalink
Merge pull request #3 from codezoned/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
master-fury authored Sep 16, 2018
2 parents 79d016b + e23f2ed commit aa40c56
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 20 deletions.
87 changes: 87 additions & 0 deletions Arrays-Sorting/src/Iterative_Merge_Sort/Iterative_Merge_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

#made by SHASHANK CHAKRAWARTY
#Time complexity of above iterative function is Θ(nLogn)
# Iterative Merge sort (Bottom Up)

# Iterative mergesort function to
# sort arr[0...n-1]
def mergeSort(a):

current_size = 1

# Outer loop for traversing Each
# sub array of current_size
while current_size < len(a) - 1:

left = 0
# Inner loop for merge call
# in a sub array
# Each complete Iteration sorts
# the iterating sub array
while left < len(a)-1:

# mid index = left index of
# sub array + current sub
# array size - 1
mid = left + current_size - 1

# (False result,True result)
# [Condition] Can use current_size
# if 2 * current_size < len(a)-1
# else len(a)-1
right = ((2 * current_size + left - 1,
len(a) - 1)[2 * current_size
+ left - 1 > len(a)-1])

# Merge call for each sub array
merge(a, left, mid, right)
left = left + current_size*2

# Increasing sub array size by
# multiple of 2
current_size = 2 * current_size

# Merge Function
def merge(a, l, m, r):
#you create 2 sub arrays with length n1 and n2
n1 = m - l + 1
n2 = r - m
#2 arrays are created
L = [0] * n1
R = [0] * n2
#copy the elements into the left and right array for sorting
for i in range(0, n1):
L[i] = a[l + i]
for i in range(0, n2):
R[i] = a[m + i + 1]
#here it sorts
i, j, k = 0, 0, l
while i < n1 and j < n2:
if L[i] > R[j]:
a[k] = R[j]
j += 1
else:
a[k] = L[i]
i += 1
k += 1
#and you copy the elements into the arrays i.e Left and Right by comparing it with the size of the array which was declared previously
while i < n1:
a[k] = L[i]
i += 1
k += 1

while j < n2:
a[k] = R[j]
j += 1
k += 1


# Driver code you can change it accordingly
a = [12, 11, 13, 5, 6, 7]
print("Before Sorting ")
print(a)

mergeSort(a)

print("After Sorting ")
print(a)
52 changes: 52 additions & 0 deletions Arrays-Sorting/src/Radix_Sort/Radix_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#Made By SHASHANK CHAKRAWARTY
"""
CONSIDER THE BELOW SATEMENT:
Let there be d digits in input integers.
Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10.
What is the value of d? If k is the maximum possible value, then d would be O(logb(k)).
So overall time complexity is O((n+b) * logb(k)).
"""


#here is your method for RADIX SORT
def radixsort(array):
RADIX = 10
maxLength = False
temp , placement = -1, 1

while not maxLength:
maxLength = True
# declare and initialize buckets
buckets = [list() for i in range( RADIX )]

# split array between lists
for i in array:
temp = int((i / placement) % RADIX)
buckets[temp].append(i)

# Do counting sort for every digit. Note that instead
# of passing digit number, exp is passed. exp is 10^i
# where i is current digit number

if maxLength and temp > 0:
maxLength = False

# empty lists into lst array
a = 0
for b in range( RADIX ):
buck = buckets[b]
for i in buck:
array[a] = i
a += 1

# move to next
placement *= RADIX

#driver code you can change the values accordingly
array = [ 170, 45, 75, 90, 802, 24, 2, 66]
radixsort(array)

print("Sorted elements are:")
for i in range(len(array)):

print(array[i],end=' ')
57 changes: 57 additions & 0 deletions Arrays-Sorting/src/merge_sort/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#made by SHASHANK CHAKRAWARTY
#The code demostrates step by step process of the merge sort how it splites, dividesin tothe 2 halves and then after getting sorted in the steps it gets merged

#Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.
'''
1)Time Complexity: Can be represented by Recurrance relation Time Complexity:T(n) = 2T(n/2) + \Theta(n)
2)Time complexity of Merge Sort is \Theta(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves.
3)Auxiliary Space: O(n)
4)Algorithmic Paradigm: Divide and Conquer
5)Sorting In Place: No in a typical implementation
6)Stable: Yes
'''

def mergeSort(alist):
print("Splitting ",alist)
if len(alist)>1:
#the array gets divided into the halves

mid = len(alist)//2

#here the subarrays are created

lefthalf = alist[:mid]
righthalf = alist[mid:]

#function calling occurs
mergeSort(lefthalf)
mergeSort(righthalf)

i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i] #copy the value to the newly created array in the lefthalf
i=i+1
else:
alist[k]=righthalf[j] #copy the value to the newly created array in the righthalf
j=j+1
k=k+1
#the process of merging goes from here
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1

while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",alist)
#The DRIVER CODE,YOU CAN CHANGE THE VALUE ACCORDINGLY


alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)
79 changes: 79 additions & 0 deletions Arrays-searching/src/fibonacci_search/Fibonacci_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#Made by SHASHANK CHAKRAWARTY
# Python3 program for Fibonacci search.
'''
Works for sorted arrays
A Divide and Conquer Algorithm.
Has Log n time complexity.
1)Fibonacci Search divides given array in unequal parts
'''

#Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is present in array else return -1.

def fibMonaccianSearch(arr, x, n):

# Initialize fibonacci numbers

var2 = 0 # (m-2)'th Fibonacci No.
var1 = 1 # (m-1)'th Fibonacci No.
res = var2 + var1 # m'th Fibonacci


# fibM is going to store the smallest
# Fibonacci Number greater than or equal to n

while (res < n):
var2 = var1
var1 = res
res = var2 + var1

# Marks the eliminated range from front
offset = -1;

# while there are elements to be inspected.
# Note that we compare arr[var2] with x.
# When fibM becomes 1, var2 becomes 0


while (res > 1):

# Check if var2 is a valid location
i = min(offset+var2, n-1)

# If x is greater than the value at
# index var2, cut the subarray array
# from offset to i
if (arr[i] < x):
res = var1
var1 = var2
var2 = res - var1
offset = i

# If x is greater than the value at
# index var2, cut the subarray
# after i+1
elif (arr[i] > x):
res = var2
var1 = var1 - var2
var2 = res - var1

# element found. return index
else :
return i

# comparing the last element with x */
if(var1 and arr[offset+1] == x):
return offset+1;

# element not found. return -1
return -1

# Driver Code, you can change the values accordingly
arr = [10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100]
n = len(arr)
x = 85
print("Found at index:",
fibMonaccianSearch(arr, x, n))

43 changes: 43 additions & 0 deletions Image_Processing/src/Binary_Image.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%Binary Image by Master-Fury

clear all;

%Setting up the directory
fpath1=fullfile('C:\Users\Manish (Master)\Desktop\images'); % Add your directory
addpath(fpath1);
basefile=sprintf('*.png'); % Add format of image
all_images=fullfile(fpath1,basefile);
fnames=dir(all_images);


% READING OF IMAGE
for i = 1:length(fnames)
all_images = fullfile(fnames(i).folder, fnames(i).name);
I = imread(all_images); % Image reading using imread
figure
imshow(I) % To see original image
title('Original Image');
% The original image contains colors RGB (in matlab it stores in the format RBG)
% Conversion of image into gray-scale image can be done by removing any one color from image.

Gray_Scale=I(:,:,2); % Green color removed.
figure
imshow(Gray_Scale)
title('Grey-Scale Version of Image')

% Conversion of GRAY-SCALE to BINARY IMAGE
Binary_Image = imbinarize(Gray_Scale,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
% You can use different methods like instead of adaptive you can use global(by default).
% Foreground Polarity can be dark or bright (refer documentaion for more info)
% Sensitivity can be from 0 to 1 , by default it is 0.5
figure
imshow(Binary_Image)
title('Binary Version of Image')
end
% SPECIAL NOTE
% There is major difference between gray-scale image and binary image. The
% differnce is - binary images consists only 0 & 1 (refer workspace) matrix
% 1 represents white and 0 represents black.
% A binary image is a digital image that has only two possible values for each pixel.
% In the case of gray scale image,
% for example 8-bit gray scale image (2^8=256) pixel value may vary between 0 to 256.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#by: shashank
#armstrong number
# ARMSTRONG NUMBER BY SHASHANK

num=int(input('Enter a Number:'))
Sum=0
Expand All @@ -8,13 +7,13 @@
digit=temp%10
Sum+=digit**3
temp//=10
# print(digit,temp,Sum)


if (num == Sum):
print('armstrong')
print('Hurray! It is a Armstrong Number')

else:
print('not armstrong')
print('Sorry! Try again, Its not a Armstrong Number')



Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#GCD of an Array Using Euclidean Algo By Master-Fury

def array_gcd(m,n):
if m<n:
(m,n)=(n,m)
if(m%n)==0:
return(n)
else:
return (gcd(n,m%n))

#DRIVER CODE

n= [2,4,6,8,16] #Enter Your Numbers Here
result=array_gcd(n[0],n[1])
for i in range(2,len(n)):
result=array_gcd(result,n[i])
print("GCD of given numbers is ",result)

##DESCRIPTION
##I have used Euclidean Algorithm to find GCD of two numbers which is a recursive method,
##also I have made this for an array of numbers.
##You can try also different methods like naive GCD and other.


27 changes: 27 additions & 0 deletions Mathematical_Algorithms/src/Sieve_of_Eratosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#Sieve of Eratosthenes by Master-Fury

def SieveOfEratosthenes(n):

prime = [True for i in range(n+1)]
p=2
while (p * p <= n):
if(prime[p]== True):
for i in range (p*2,n+1,p):
prime[i]= False
p+=1
for p in range (2,n):
if prime[p]:
print (p)

#DRIVER CODE
n=10 #ENTER THE NUMBER HERE!!
print ("The following are the prime numbers smaller than or equal to ",n)
SieveOfEratosthenes(n)


##Description
##Given a number n, print all primes smaller than or equal to n.
##It is also given that n is a small number.
##The sieve of Eratosthenes is one of the most efficient ways to find all
##primes smaller than n when n is smaller than 10 million
##Time complexity : O(sqrt(n)loglog(n))
Loading

0 comments on commit aa40c56

Please sign in to comment.