Skip to content

Commit 60b06f0

Browse files
committed
Fitcher-yates algo and Algo to find equilibrium index
1 parent 57e2b9b commit 60b06f0

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

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

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])

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ The solutions are easy to understand and complete.
3030
1. [Juggling Algorithm for array rotation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/JugglingAlgorithm.rb)
3131
2. [Reversal Algorithm for array rotation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ReversalAlgorithm.rb)
3232
3. Block Swap Algorithm for array rotation
33-
a. [Recursive](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/BlockSwapAlgorithmRecursive.rb)
34-
b. [Iterative](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/BlockSwapAlgorithmIterative.rb)
33+
a.[Recursive](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/BlockSwapAlgorithmRecursive.rb)
34+
b.[Iterative](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/BlockSwapAlgorithmIterative.rb)
3535
4. [Algorithm to find maximum continuos sum in an array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/MaxSliceSumAlgorithm.rb)
3636
5. [Find missing number in array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FindMissingNumber.rb)
3737
6. [Swapping two numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/SwappingTwoNumbers.rb)
38-
7. [Shuffling an Array(Knuth Algorithm)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/KnuthShufflingAlgorithm.rb)
38+
7. Shuffling an Array
39+
a.[Knuth shuffling Algorithm)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/KnuthShufflingAlgorithm.rb)
40+
b.[Fitcher-Yates shuffling Algorithm)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FitcherYatesShufflingAlgorithm.rb)
3941
8. [Find pair in array with sum equal to given sum](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ChekPairWithGivenSum.rb)
42+
9. [Find equilibrium index in an array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/EquilibriumIndexofArray.rb)
4043

4144

4245

0 commit comments

Comments
 (0)