diff --git a/Bit Manipulation/EvenOrOdd/evenodd.py b/Bit Manipulation/EvenOrOdd/evenodd.py new file mode 100644 index 000000000..d1436b194 --- /dev/null +++ b/Bit Manipulation/EvenOrOdd/evenodd.py @@ -0,0 +1,9 @@ +def isEven(n) : + return (n & 1); + +n = 101; +if(isEven(n) == 0) : + print ("Even"); +else : + print ("Odd"); + diff --git a/Other Algorithms/KMP/pyhton/kmpsearch.py b/Other Algorithms/KMP/pyhton/kmpsearch.py new file mode 100644 index 000000000..2478cdfb2 --- /dev/null +++ b/Other Algorithms/KMP/pyhton/kmpsearch.py @@ -0,0 +1,44 @@ +# Python program for KMP Algorithm +def KMPSearch(pat, txt): + M = len(pat) + N = len(txt) + lps = [0]*M + j = 0 + computeLPSArray(pat, M, lps) + + i = 0 + while i < N: + if pat[j] == txt[i]: + i += 1 + j += 1 + + if j == M: + print "Found pattern at index " + str(i-j) + j = lps[j-1] + elif i < N and pat[j] != txt[i]: + if j != 0: + j = lps[j-1] + else: + i += 1 + +def computeLPSArray(pat, M, lps): + len = 0 + + lps[0] + i = 1 + while i < M: + if pat[i]== pat[len]: + len += 1 + lps[i] = len + i += 1 + else: + if len != 0: + len = lps[len-1] + else: + lps[i] = 0 + i += 1 + +txt = "ABABDABACDABABCABAB" +pat = "ABABCABAB" +KMPSearch(pat, txt) + diff --git a/Recursive Algorithms/LCSRecursive/cpp/LCSREC.py b/Recursive Algorithms/LCSRecursive/cpp/LCSREC.py new file mode 100644 index 000000000..3b75d7603 --- /dev/null +++ b/Recursive Algorithms/LCSRecursive/cpp/LCSREC.py @@ -0,0 +1,11 @@ +def lcs(X, Y, m, n): + if m == 0 or n == 0: + return 0 + elif X[m-1] == Y[n-1]: + return 1 + lcs(X, Y, m-1, n-1) + else: + return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)) + +X = "SDFWERTHK" +Y = "QWEDFGHTY" +print ("Length of LCS is ", lcs(X , Y, len(X), len(Y))) diff --git a/Searching/binary search/Ruby/binSearch.rb b/Searching/binary search/Ruby/binSearch.rb new file mode 100644 index 000000000..dcfc1462a --- /dev/null +++ b/Searching/binary search/Ruby/binSearch.rb @@ -0,0 +1,19 @@ + def binary_search(a, n, x) + index_start = 0 + index_end = n - 1 + + while index_start <= index_end do + index_middle = (index_start + index_end) / 2 + if a[index_middle] == x + return index_middle + elsif x < a[index_middle] + index_end = index_middle - 1 + else + index_start = index_middle + 1 + end + end + return "not found" +end + +a = [1, 2, 3, 4, 5, 7] +puts binary_search(a, a.size, 3) diff --git a/Sorting/Counting Sort/Ruby/countSort.rb b/Sorting/Counting Sort/Ruby/countSort.rb new file mode 100644 index 000000000..bcce4aff1 --- /dev/null +++ b/Sorting/Counting Sort/Ruby/countSort.rb @@ -0,0 +1,18 @@ +class Array + def counting_sort! + replace counting_sort + end + + def counting_sort + min, max = minmax + count = Array.new(max - min + 1, 0) + each {|number| count[number - min] += 1} + (min..max).each_with_object([]) {|i, ary| ary.concat([i] * count[i - min])} + end + end + + ary = [9,7,10,2,9,7,4,3,10,2,7,10,2,1,3,8,7,3,9,5,8,5,1,6,3,7,5,4,6,9,9,6,6,10,2,4,5,2,8,2,2,5,2,9,3,3,5,7,8,4] + p ary.counting_sort.join(",") + + p ary = Array.new(20){rand(-10..10)} + p ary.counting_sort diff --git a/Sorting/Gnome Sort/py3_gnome.py b/Sorting/Gnome Sort/Python/py3_gnome.py similarity index 100% rename from Sorting/Gnome Sort/py3_gnome.py rename to Sorting/Gnome Sort/Python/py3_gnome.py diff --git a/Sorting/Gnome Sort/Ruby/GnomeSort.rb b/Sorting/Gnome Sort/Ruby/GnomeSort.rb new file mode 100644 index 000000000..dbba47402 --- /dev/null +++ b/Sorting/Gnome Sort/Ruby/GnomeSort.rb @@ -0,0 +1,21 @@ +class Array + def gnomesort! + i, j = 1, 2 + while i < length + if self[i-1] <= self[i] + i, j = j, j+1 + else + self[i-1], self[i] = self[i], self[i-1] + i -= 1 + if i == 0 + i, j = j, j+1 + end + end + end + self + end + end + + + ary = [7,6,5,9,8,4,3,1,2,0] + ary.gnomesort! diff --git a/Sorting/Pancake Sorting/Ruby/pancakeSort.rb b/Sorting/Pancake Sorting/Ruby/pancakeSort.rb new file mode 100644 index 000000000..ec48f22da --- /dev/null +++ b/Sorting/Pancake Sorting/Ruby/pancakeSort.rb @@ -0,0 +1,22 @@ +class Array + def pancake_sort! + num_flips = 0 + (self.size-1).downto(1) do |end_idx| + max = self[0..end_idx].max + max_idx = self[0..end_idx].index(max) + next if max_idx == end_idx + + if max_idx > 0 + self[0..max_idx] = self[0..max_idx].reverse + p [num_flips += 1, self] if $DEBUG + end + + self[0..end_idx] = self[0..end_idx].reverse + p [num_flips += 1, self] if $DEBUG + end + self + end + end + + p a = (1..9).to_a.shuffle + p a.pancake_sort! diff --git a/Sorting/Radix Sort/Ruby/radixSort.rb b/Sorting/Radix Sort/Ruby/radixSort.rb new file mode 100644 index 000000000..7716eb9b3 --- /dev/null +++ b/Sorting/Radix Sort/Ruby/radixSort.rb @@ -0,0 +1,23 @@ +class Array + def radix_sort(base=10) + ary = dup + rounds = (Math.log(ary.minmax.map(&:abs).max)/Math.log(base)).floor + 1 + rounds.times do |i| + buckets = Array.new(2*base){[]} + base_i = base**i + ary.each do |n| + digit = (n/base_i) % base + digit += base if 0<=n + buckets[digit] << n + end + ary = buckets.flatten + p [i, ary] if $DEBUG + end + ary + end + def radix_sort!(base=10) + replace radix_sort(base) + end +end + +p [170, 45, 75, 90, 2, 24, 802, 66].radix_sort diff --git a/Sorting/Selection Sort/Ruby/selSort.rb b/Sorting/Selection Sort/Ruby/selSort.rb new file mode 100644 index 000000000..2edfe58cb --- /dev/null +++ b/Sorting/Selection Sort/Ruby/selSort.rb @@ -0,0 +1,19 @@ +def selection_sort(a, n) + (0..n - 2).each do |i| + i_min = i + (i + 1..n - 1).each do |j| + if a[j] < a[i_min] + i_min = j + end + end + + temp = a[i] + a[i] = a[i_min] + a[i_min] = temp + #byebug + end + a + end + + a = [2,7,4,1,5,3] + selection_sort(a, a.size) diff --git a/Tree/Binary Search Tree/Ruby/bst.rb b/Tree/Binary Search Tree/Ruby/bst.rb new file mode 100644 index 000000000..25cb26d36 --- /dev/null +++ b/Tree/Binary Search Tree/Ruby/bst.rb @@ -0,0 +1,111 @@ +class BinarySearchTree + class Node + attr_reader :key, :left, :right + def initialize( key ) + @key = key + @left = nil + @right = nil + end + + def insert( new_key ) + if new_key <= @key + @left.nil? ? @left = Node.new( new_key ) : @left.insert( new_key ) + elsif new_key > @key + @right.nil? ? @right = Node.new( new_key ) : @right.insert( new_key ) + end + end + end + + def initialize + @root = nil + end + + def insert( key ) + if @root.nil? + @root = Node.new( key ) + else + @root.insert( key ) + end + end + + def in_order(node=@root, &block) + return if node.nil? + in_order(node.left, &block) + yield node + in_order(node.right, &block) + end + + def pre_order(node=@root, &block) + return if node.nil? + yield node + in_order(node.left, &block) + in_order(node.right, &block) + end + + def post_order(node=@root, &block) + return if node.nil? + in_order(node.left, &block) + in_order(node.right, &block) + yield node + end + + def search( key, node=@root ) + return nil if node.nil? + if key < node.key + search( key, node.left ) + elsif key > node.key + search( key, node.right ) + else + return node + end + end + + def check_height(node) + return 0 if node.nil? + + leftHeight = check_height(node.left) + return -1 if leftHeight == -1 + + rightHeight = check_height(node.right) + return -1 if rightHeight == -1 + + diff = leftHeight - rightHeight + if diff.abs > 1 + -1 + else + [leftHeight, rightHeight].max + 1 + end + end + + def is_balanced?(node=@root) + check_height(node) == -1 ? false : true + end + +end + +tree = BinarySearchTree.new +tree.insert(50) +tree.insert(25) +tree.insert(75) +tree.insert(12) +tree.insert(37) +tree.insert(87) +tree.insert(63) +puts tree.inspect +puts "tree.is_balanced?" +puts tree.is_balanced? + +puts "pre_order" +tree.pre_order do |node| + puts node.key +end + +puts "in_order" +tree.in_order do |node| + puts node.key +end + +puts "post_order" +tree.post_order do |node| + puts node.key +end