Skip to content

Commit aa40c56

Browse files
authored
Merge pull request #3 from codezoned/master
Update
2 parents 79d016b + e23f2ed commit aa40c56

File tree

11 files changed

+392
-20
lines changed

11 files changed

+392
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
#made by SHASHANK CHAKRAWARTY
3+
#Time complexity of above iterative function is Θ(nLogn)
4+
# Iterative Merge sort (Bottom Up)
5+
6+
# Iterative mergesort function to
7+
# sort arr[0...n-1]
8+
def mergeSort(a):
9+
10+
current_size = 1
11+
12+
# Outer loop for traversing Each
13+
# sub array of current_size
14+
while current_size < len(a) - 1:
15+
16+
left = 0
17+
# Inner loop for merge call
18+
# in a sub array
19+
# Each complete Iteration sorts
20+
# the iterating sub array
21+
while left < len(a)-1:
22+
23+
# mid index = left index of
24+
# sub array + current sub
25+
# array size - 1
26+
mid = left + current_size - 1
27+
28+
# (False result,True result)
29+
# [Condition] Can use current_size
30+
# if 2 * current_size < len(a)-1
31+
# else len(a)-1
32+
right = ((2 * current_size + left - 1,
33+
len(a) - 1)[2 * current_size
34+
+ left - 1 > len(a)-1])
35+
36+
# Merge call for each sub array
37+
merge(a, left, mid, right)
38+
left = left + current_size*2
39+
40+
# Increasing sub array size by
41+
# multiple of 2
42+
current_size = 2 * current_size
43+
44+
# Merge Function
45+
def merge(a, l, m, r):
46+
#you create 2 sub arrays with length n1 and n2
47+
n1 = m - l + 1
48+
n2 = r - m
49+
#2 arrays are created
50+
L = [0] * n1
51+
R = [0] * n2
52+
#copy the elements into the left and right array for sorting
53+
for i in range(0, n1):
54+
L[i] = a[l + i]
55+
for i in range(0, n2):
56+
R[i] = a[m + i + 1]
57+
#here it sorts
58+
i, j, k = 0, 0, l
59+
while i < n1 and j < n2:
60+
if L[i] > R[j]:
61+
a[k] = R[j]
62+
j += 1
63+
else:
64+
a[k] = L[i]
65+
i += 1
66+
k += 1
67+
#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
68+
while i < n1:
69+
a[k] = L[i]
70+
i += 1
71+
k += 1
72+
73+
while j < n2:
74+
a[k] = R[j]
75+
j += 1
76+
k += 1
77+
78+
79+
# Driver code you can change it accordingly
80+
a = [12, 11, 13, 5, 6, 7]
81+
print("Before Sorting ")
82+
print(a)
83+
84+
mergeSort(a)
85+
86+
print("After Sorting ")
87+
print(a)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#Made By SHASHANK CHAKRAWARTY
2+
"""
3+
CONSIDER THE BELOW SATEMENT:
4+
Let there be d digits in input integers.
5+
Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10.
6+
What is the value of d? If k is the maximum possible value, then d would be O(logb(k)).
7+
So overall time complexity is O((n+b) * logb(k)).
8+
"""
9+
10+
11+
#here is your method for RADIX SORT
12+
def radixsort(array):
13+
RADIX = 10
14+
maxLength = False
15+
temp , placement = -1, 1
16+
17+
while not maxLength:
18+
maxLength = True
19+
# declare and initialize buckets
20+
buckets = [list() for i in range( RADIX )]
21+
22+
# split array between lists
23+
for i in array:
24+
temp = int((i / placement) % RADIX)
25+
buckets[temp].append(i)
26+
27+
# Do counting sort for every digit. Note that instead
28+
# of passing digit number, exp is passed. exp is 10^i
29+
# where i is current digit number
30+
31+
if maxLength and temp > 0:
32+
maxLength = False
33+
34+
# empty lists into lst array
35+
a = 0
36+
for b in range( RADIX ):
37+
buck = buckets[b]
38+
for i in buck:
39+
array[a] = i
40+
a += 1
41+
42+
# move to next
43+
placement *= RADIX
44+
45+
#driver code you can change the values accordingly
46+
array = [ 170, 45, 75, 90, 802, 24, 2, 66]
47+
radixsort(array)
48+
49+
print("Sorted elements are:")
50+
for i in range(len(array)):
51+
52+
print(array[i],end=' ')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#made by SHASHANK CHAKRAWARTY
2+
#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
3+
4+
#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.
5+
'''
6+
1)Time Complexity: Can be represented by Recurrance relation Time Complexity:T(n) = 2T(n/2) + \Theta(n)
7+
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.
8+
3)Auxiliary Space: O(n)
9+
4)Algorithmic Paradigm: Divide and Conquer
10+
5)Sorting In Place: No in a typical implementation
11+
6)Stable: Yes
12+
'''
13+
14+
def mergeSort(alist):
15+
print("Splitting ",alist)
16+
if len(alist)>1:
17+
#the array gets divided into the halves
18+
19+
mid = len(alist)//2
20+
21+
#here the subarrays are created
22+
23+
lefthalf = alist[:mid]
24+
righthalf = alist[mid:]
25+
26+
#function calling occurs
27+
mergeSort(lefthalf)
28+
mergeSort(righthalf)
29+
30+
i=0
31+
j=0
32+
k=0
33+
while i < len(lefthalf) and j < len(righthalf):
34+
if lefthalf[i] < righthalf[j]:
35+
alist[k]=lefthalf[i] #copy the value to the newly created array in the lefthalf
36+
i=i+1
37+
else:
38+
alist[k]=righthalf[j] #copy the value to the newly created array in the righthalf
39+
j=j+1
40+
k=k+1
41+
#the process of merging goes from here
42+
while i < len(lefthalf):
43+
alist[k]=lefthalf[i]
44+
i=i+1
45+
k=k+1
46+
47+
while j < len(righthalf):
48+
alist[k]=righthalf[j]
49+
j=j+1
50+
k=k+1
51+
print("Merging ",alist)
52+
#The DRIVER CODE,YOU CAN CHANGE THE VALUE ACCORDINGLY
53+
54+
55+
alist = [54,26,93,17,77,31,44,55,20]
56+
mergeSort(alist)
57+
print(alist)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#Made by SHASHANK CHAKRAWARTY
2+
# Python3 program for Fibonacci search.
3+
'''
4+
Works for sorted arrays
5+
A Divide and Conquer Algorithm.
6+
Has Log n time complexity.
7+
8+
1)Fibonacci Search divides given array in unequal parts
9+
10+
'''
11+
12+
#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.
13+
14+
def fibMonaccianSearch(arr, x, n):
15+
16+
# Initialize fibonacci numbers
17+
18+
var2 = 0 # (m-2)'th Fibonacci No.
19+
var1 = 1 # (m-1)'th Fibonacci No.
20+
res = var2 + var1 # m'th Fibonacci
21+
22+
23+
# fibM is going to store the smallest
24+
# Fibonacci Number greater than or equal to n
25+
26+
while (res < n):
27+
var2 = var1
28+
var1 = res
29+
res = var2 + var1
30+
31+
# Marks the eliminated range from front
32+
offset = -1;
33+
34+
# while there are elements to be inspected.
35+
# Note that we compare arr[var2] with x.
36+
# When fibM becomes 1, var2 becomes 0
37+
38+
39+
while (res > 1):
40+
41+
# Check if var2 is a valid location
42+
i = min(offset+var2, n-1)
43+
44+
# If x is greater than the value at
45+
# index var2, cut the subarray array
46+
# from offset to i
47+
if (arr[i] < x):
48+
res = var1
49+
var1 = var2
50+
var2 = res - var1
51+
offset = i
52+
53+
# If x is greater than the value at
54+
# index var2, cut the subarray
55+
# after i+1
56+
elif (arr[i] > x):
57+
res = var2
58+
var1 = var1 - var2
59+
var2 = res - var1
60+
61+
# element found. return index
62+
else :
63+
return i
64+
65+
# comparing the last element with x */
66+
if(var1 and arr[offset+1] == x):
67+
return offset+1;
68+
69+
# element not found. return -1
70+
return -1
71+
72+
# Driver Code, you can change the values accordingly
73+
arr = [10, 22, 35, 40, 45, 50,
74+
80, 82, 85, 90, 100]
75+
n = len(arr)
76+
x = 85
77+
print("Found at index:",
78+
fibMonaccianSearch(arr, x, n))
79+

Image_Processing/src/Binary_Image.m

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
%Binary Image by Master-Fury
2+
3+
clear all;
4+
5+
%Setting up the directory
6+
fpath1=fullfile('C:\Users\Manish (Master)\Desktop\images'); % Add your directory
7+
addpath(fpath1);
8+
basefile=sprintf('*.png'); % Add format of image
9+
all_images=fullfile(fpath1,basefile);
10+
fnames=dir(all_images);
11+
12+
13+
% READING OF IMAGE
14+
for i = 1:length(fnames)
15+
all_images = fullfile(fnames(i).folder, fnames(i).name);
16+
I = imread(all_images); % Image reading using imread
17+
figure
18+
imshow(I) % To see original image
19+
title('Original Image');
20+
% The original image contains colors RGB (in matlab it stores in the format RBG)
21+
% Conversion of image into gray-scale image can be done by removing any one color from image.
22+
23+
Gray_Scale=I(:,:,2); % Green color removed.
24+
figure
25+
imshow(Gray_Scale)
26+
title('Grey-Scale Version of Image')
27+
28+
% Conversion of GRAY-SCALE to BINARY IMAGE
29+
Binary_Image = imbinarize(Gray_Scale,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
30+
% You can use different methods like instead of adaptive you can use global(by default).
31+
% Foreground Polarity can be dark or bright (refer documentaion for more info)
32+
% Sensitivity can be from 0 to 1 , by default it is 0.5
33+
figure
34+
imshow(Binary_Image)
35+
title('Binary Version of Image')
36+
end
37+
% SPECIAL NOTE
38+
% There is major difference between gray-scale image and binary image. The
39+
% differnce is - binary images consists only 0 & 1 (refer workspace) matrix
40+
% 1 represents white and 0 represents black.
41+
% A binary image is a digital image that has only two possible values for each pixel.
42+
% In the case of gray scale image,
43+
% for example 8-bit gray scale image (2^8=256) pixel value may vary between 0 to 256.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#by: shashank
2-
#armstrong number
1+
# ARMSTRONG NUMBER BY SHASHANK
32

43
num=int(input('Enter a Number:'))
54
Sum=0
@@ -8,13 +7,13 @@
87
digit=temp%10
98
Sum+=digit**3
109
temp//=10
11-
# print(digit,temp,Sum)
10+
1211

1312
if (num == Sum):
14-
print('armstrong')
13+
print('Hurray! It is a Armstrong Number')
1514

1615
else:
17-
print('not armstrong')
16+
print('Sorry! Try again, Its not a Armstrong Number')
1817

1918

2019

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#GCD of an Array Using Euclidean Algo By Master-Fury
2+
3+
def array_gcd(m,n):
4+
if m<n:
5+
(m,n)=(n,m)
6+
if(m%n)==0:
7+
return(n)
8+
else:
9+
return (gcd(n,m%n))
10+
11+
#DRIVER CODE
12+
13+
n= [2,4,6,8,16] #Enter Your Numbers Here
14+
result=array_gcd(n[0],n[1])
15+
for i in range(2,len(n)):
16+
result=array_gcd(result,n[i])
17+
print("GCD of given numbers is ",result)
18+
19+
##DESCRIPTION
20+
##I have used Euclidean Algorithm to find GCD of two numbers which is a recursive method,
21+
##also I have made this for an array of numbers.
22+
##You can try also different methods like naive GCD and other.
23+
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#Sieve of Eratosthenes by Master-Fury
2+
3+
def SieveOfEratosthenes(n):
4+
5+
prime = [True for i in range(n+1)]
6+
p=2
7+
while (p * p <= n):
8+
if(prime[p]== True):
9+
for i in range (p*2,n+1,p):
10+
prime[i]= False
11+
p+=1
12+
for p in range (2,n):
13+
if prime[p]:
14+
print (p)
15+
16+
#DRIVER CODE
17+
n=10 #ENTER THE NUMBER HERE!!
18+
print ("The following are the prime numbers smaller than or equal to ",n)
19+
SieveOfEratosthenes(n)
20+
21+
22+
##Description
23+
##Given a number n, print all primes smaller than or equal to n.
24+
##It is also given that n is a small number.
25+
##The sieve of Eratosthenes is one of the most efficient ways to find all
26+
##primes smaller than n when n is smaller than 10 million
27+
##Time complexity : O(sqrt(n)loglog(n))

0 commit comments

Comments
 (0)