Skip to content

Commit 831f440

Browse files
committed
update readme
1 parent 266a654 commit 831f440

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

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

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

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ The solutions are easy to understand and complete.
4141
b.[Fitcher-Yates shuffling Algorithm)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FitcherYatesShufflingAlgorithm.rb)
4242
6. [Find pair in array with sum equal to given sum](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/ChekPairWithGivenSum.rb)
4343
7. [Find equilibrium index in an array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/EquilibriumIndexofArray.rb)
44+
8. Find fixed point in an array
45+
* [fixed point in unsorted array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FixedPointInUnsorted.rb)
46+
* [fixed point in sorted array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/FixedPointInSorted.rb)
47+
4448

4549

4650

0 commit comments

Comments
 (0)