Skip to content

Commit 7aea1be

Browse files
committed
Searching algos in array
1 parent 13b784f commit 7aea1be

24 files changed

+851
-0
lines changed

Diff for: arrays/BlockSwapAlgorithmIterative.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Block swap algorithm for array rotation(Iterative)
2+
3+
#Initialize A=[0..d-1], B=[d..size-1]
4+
#until size of A is equal to size of B
5+
6+
# a) If A is shorter
7+
# 1. Divide B into Bl and Br such that Br is of same length as A.
8+
# 2. Swap A and Br to change ABlBr into BrBlA.
9+
# 3. Now A is at its final place, so recur on pieces of B.
10+
11+
# b) If A is longer
12+
# 1. Divide A into Al and Ar such that Al is of same length as B.
13+
# 2. Swap Al and B to change AlArB into BArAl.
14+
# 3. Now B is at its final place, so recur on pieces of A.
15+
16+
#Finally when A and B are of equal size, block swap them.
17+
18+
# Iterative approach
19+
20+
def block_swap(a,d)
21+
n=a.length
22+
if n>0
23+
24+
if d>=n
25+
d%=n
26+
end
27+
28+
if d==0
29+
return a
30+
end
31+
32+
if d==n-d
33+
swap(a,0,d,d)
34+
end
35+
36+
i= d
37+
j= n-d
38+
39+
while(i!=j)
40+
if i<j
41+
swap(a,d-i,d+j-i,i)
42+
j-=i
43+
else
44+
swap(a,d-i,d,j)
45+
i-=j
46+
end
47+
end
48+
49+
swap(a,d-i,d,i)
50+
end
51+
52+
return a
53+
54+
end
55+
56+
57+
def swap(a,start1,start2,d)
58+
if (start1 != start2)
59+
for i in 0...d
60+
temp = a[start1+i]
61+
a[start1+i] = a[start2+i]
62+
a[start2+i] = temp
63+
end
64+
end
65+
end
66+
67+
block_swap([1,2,3,4,5,6,7,8,9],5)

Diff for: arrays/BlockSwapAlgorithmRecursive.rb

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Block swap algorithm for array rotation(Recursive)
2+
3+
#Initialize A=[0..d-1], B=[d..size-1]
4+
#until size of A is equal to size of B
5+
6+
# a) If A is shorter
7+
# 1. Divide B into Bl and Br such that Br is of same length as A.
8+
# 2. Swap A and Br to change ABlBr into BrBlA.
9+
# 3. Now A is at its final place, so recur on pieces of B.
10+
11+
# b) If A is longer
12+
# 1. Divide A into Al and Ar such that Al is of same length as B.
13+
# 2. Swap Al and B to change AlArB into BArAl.
14+
# 3. Now B is at its final place, so recur on pieces of A.
15+
16+
#Finally when A and B are of equal size, block swap them.
17+
18+
19+
20+
# Recursive Approach
21+
22+
#Driver function
23+
def rotate_array(a, d) #Input array "a" and rotation by "d" elemets
24+
finish =a.length-1
25+
block_swap(a,0,finish,d)
26+
end
27+
28+
def block_swap(a,start,finish,d)
29+
30+
n=finish-start+1
31+
32+
if n>0
33+
34+
if d>n
35+
d%=n
36+
end
37+
38+
if d==0
39+
return a
40+
end
41+
42+
if d==n-d
43+
swap(a,start,start+d,d)
44+
return a
45+
elsif d<n-d
46+
swap(a,start,finish-d+1,d)
47+
block_swap(a,start,finish-d,d)
48+
else
49+
swap(a,start,d,n-d)
50+
block_swap(a,n-d,n-1,(2*d)-n)
51+
end
52+
53+
end
54+
55+
return a
56+
57+
end
58+
59+
60+
#Utility function for swapping
61+
def swap(a,start1,start2,d)
62+
for i in 0...d
63+
temp = a[start1+i]
64+
a[start1+i] = a[start2+i]
65+
a[start2+i] = temp
66+
end
67+
end
68+
rotate_array([1,2,3,4,5,6,7,8,9,10],6)

Diff for: arrays/ChekPairWithGivenSum.rb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#Given an array A[] and a number x, check for pair in A[] with sum as x
2+
3+
#Approach 1: Using Hash Map
4+
#Time-complexity: O(n), Space-complexity: O(n) {extra space required for hashmap}
5+
6+
def check_pair(arr,x)
7+
len=arr.length
8+
flag=false
9+
map=Hash.new()
10+
for i in 0...len
11+
t=x-arr[i]
12+
if map[t]
13+
flag=true
14+
break
15+
end
16+
map[arr[i]]=1
17+
end
18+
if flag
19+
return "Array has two elements with sum:#{x}"
20+
else
21+
return "Array doesn't have two elements with sum:#{x}"
22+
end
23+
end
24+
25+
check_pair([5,4,10,-2,3,2,-1,9],9)
26+
27+
28+
##Approach 2: Sorting and then searching using left and right indexes.
29+
#Time-complexity: O(nlogn) //O(nlogn)+O(n), Space-complexity: O(1)
30+
31+
def check_pair(arr,x)
32+
len=arr.length
33+
arr.sort! # You can choose your own sorting algorithm
34+
left=0
35+
right=len-1
36+
while left<right
37+
if (arr[left]+arr[right]==x)
38+
return "Array has two elements with sum:#{x}"
39+
elsif (arr[left]+arr[right]>x)
40+
right-=1
41+
else
42+
left+=1
43+
end
44+
end
45+
return "Array doesn't have two elements with sum:#{x}"
46+
end
47+
48+
check_pair([5,4,10,-2,3,2,-1,9],9)

Diff for: arrays/ClosestSum.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#Given array of integers(both +ve and -ve) find the two elements such that their sum is closest to given number x.
2+
#Time-complexity: O(nlogn) // O(nlogn){for sorting}+O(n), Auxiliary-space:O(1)
3+
4+
#Algorithm: Sort the array and using two indexes left and right update the closest sum
5+
6+
def closest_sum(a,x)
7+
a.sort! #You can choose any sorting algorithm of your choice with
8+
left=min_l=0
9+
right=min_r=a.length-1
10+
min_sum = 1.0/0.0 #Initializing min_sum with infinity
11+
while(left<right)
12+
sum=(a[left]+a[right])-x
13+
if sum.abs<min_sum
14+
min_sum=sum.abs
15+
min_l=left
16+
min_r=right
17+
end
18+
if sum<0
19+
left+=1
20+
else
21+
right-=1
22+
end
23+
end
24+
print " The two elements whose sum is minimum are #{a[min_l]} and #{a[min_r]}"
25+
end
26+
27+
closest_sum([1,3,2,4,5],9) # => The two elements whose sum is minimum are 4 and 5

Diff for: arrays/EquilibriumIndexofArray.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=begin
2+
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.
3+
For example, in an arrya A:
4+
A[]=[-7,1,5,2,-4,3,0]
5+
3 is an equilibrium index, because:
6+
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
7+
Time-complexity: O(n)
8+
Auxiliary-space: O(1)
9+
=end
10+
11+
def find_equilibrium(a)
12+
len = a.length
13+
leftsum=0
14+
rightsum=0
15+
for x in a
16+
rightsum+=x
17+
end
18+
for i in 0...len
19+
rightsum-=a[i]
20+
if (leftsum == rightsum)
21+
return i
22+
end
23+
leftsum+=a[i]
24+
end
25+
26+
return -1
27+
28+
end

Diff for: arrays/FindMissingNumber.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Problem:
2+
=begin
3+
Given an array of size N ,taht has elements in range 1 to N ,all elements in the arrayb are unique,
4+
Find the missing number.
5+
e.g.
6+
array = [1,5,2,4,6]
7+
answer = 3
8+
=end
9+
10+
#Approach 1 - Find the sum of all elements of array and substract it from sum of the range , the difference will be missing number.
11+
# Time-complexity = O(n)
12+
#Caveats: If the numbers are very large the sum can lead to integer overflow.
13+
14+
def find_missing_number(a)
15+
n = a.length+1
16+
range_sum = (n * (n+1))/2 # Sum of numbers from 1 to n is (n* n+1 )/2
17+
arr_sum = 0
18+
for x in a
19+
arr_sum+=x
20+
end
21+
22+
return missing_element = range_sum - arr_sum
23+
24+
end
25+
26+
27+
#Approach 2 (No integer overflow)- Take bitwise XOR of all elements of the array with the numbers in given range,
28+
#As( a XOR a == 0 ) all numbers present in array will xor out and become zero except the missing number.
29+
# Time-complexity = O(n)
30+
31+
def find_missing_number(a)
32+
n = a.length+1
33+
missing_element = 0
34+
(1..n).each {|num| missing_element^= num}
35+
for x in a
36+
missing_element^=x
37+
end
38+
return missing_element
39+
end
40+
41+
42+
43+
#Ruby magic
44+
#One line code but it uses O(n) auxiliary-space, it returns an array containing only the missing_element
45+
# Array1-Array2 in ruby returns an array containing all the elements that are in Array1 but not in Array2
46+
47+
def find_missing_number(a)
48+
n = a.length+1
49+
range = Array (1..n)
50+
missing_element = range-a
51+
end

Diff for: arrays/FitcherYatesShufflingAlgorithm.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Fitcher-Yates shuffling algorithm
2+
#(start from last element,swap with random element from the array,decrement array size by one and repeat until array size is 1)
3+
#Time-complexity: O(n)
4+
5+
def shuffle(arr)
6+
n=arr.length
7+
for i in (n-1).downto(1)
8+
j=Random.rand(i+1)
9+
swap(arr,i,j)
10+
end
11+
print arr
12+
end
13+
14+
def swap(arr,i,j)
15+
temp = arr[i]
16+
arr[i] = arr[j]
17+
arr[j] = temp
18+
end
19+
20+
shuffle([1,2,3,4,5,6])

Diff for: arrays/FixedPointInSorted.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=begin
2+
Given an array of n distinct integers sorted in ascending order, find a fixed point in the array.
3+
Fixed Point in an array is an index i such that arr[i] is equal to i, i.e. a[i]==i
4+
Time-complexity: O(logn)
5+
Space-complexity: O(1)
6+
Algorithm: Binary Search
7+
=end
8+
9+
def fixed_point(a)
10+
n=a.length
11+
hi=n-1
12+
lo=0
13+
while(hi>=lo)
14+
mid=lo+(hi-lo)/2
15+
16+
if a[mid]==mid
17+
return mid
18+
elsif a[mid]<mid
19+
lo=mid+1
20+
else
21+
hi=mid-1
22+
end
23+
end
24+
25+
#if no fixed point exists
26+
return -1
27+
28+
end

Diff for: arrays/FixedPointInUnsorted.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=begin
2+
Given an array, find a fixed point in the array.
3+
Fixed Point in an array is an index i such that arr[i] is equal to i, i.e. a[i]==i
4+
Time-complexity: O(n)
5+
Space-complexity: O(1)
6+
Algorithm: Linear Search
7+
=end
8+
def fixed_point(a)
9+
n=a.length
10+
for i in 0...n
11+
if a[i]==i
12+
return i
13+
end
14+
end
15+
#if no fixed point exists
16+
return -1
17+
end

0 commit comments

Comments
 (0)