├── .gitignore ├── CSharpQuickFind └── QuickFind.cs ├── CSharpQuickUnion └── QuickUnion.cs ├── CSharpSelectionSort └── SelectionSortClass.cs ├── CSharpShellSort └── ShellSortClass.cs ├── CSharpWeightedQuickUnion └── WeightedQuickUnionAlgortihm.cs ├── CppInsertionSort └── CppInsertionSort.cpp ├── CsharpBinarySearch ├── BinarySearch.dll └── Class1.cs ├── CsharpBubbleSort ├── BubbleSort.dll └── Class1.cs ├── CsharpInsertionSort ├── InsertionSortDLL.dll └── Program.cs ├── DijkstraTwoStackAlgorithm └── TwoStackAlgo.cs ├── PythonLIFO └── Stack.py ├── PythonLinearSearch └── Program.py ├── PythonMergeSort └── Program.py ├── PythonSelectionSort └── Program.py ├── README.md └── license /.gitignore: -------------------------------------------------------------------------------- 1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs) 2 | [Bb]in/ 3 | [Oo]bj/ 4 | 5 | # mstest test results 6 | TestResults 7 | 8 | ## Ignore Visual Studio temporary files, build results, and 9 | ## files generated by popular Visual Studio add-ons. 10 | 11 | # User-specific files 12 | *.suo 13 | *.user 14 | *.sln.docstates 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Rr]elease/ 19 | x64/ 20 | *_i.c 21 | *_p.c 22 | *.ilk 23 | *.meta 24 | *.obj 25 | *.pch 26 | *.pdb 27 | *.pgc 28 | *.pgd 29 | *.rsp 30 | *.sbr 31 | *.tlb 32 | *.tli 33 | *.tlh 34 | *.tmp 35 | *.log 36 | *.vspscc 37 | *.vssscc 38 | .builds 39 | 40 | # Visual C++ cache files 41 | ipch/ 42 | *.aps 43 | *.ncb 44 | *.opensdf 45 | *.sdf 46 | 47 | # Visual Studio profiler 48 | *.psess 49 | *.vsp 50 | *.vspx 51 | 52 | # Guidance Automation Toolkit 53 | *.gpState 54 | 55 | # ReSharper is a .NET coding add-in 56 | _ReSharper* 57 | 58 | # NCrunch 59 | *.ncrunch* 60 | .*crunch*.local.xml 61 | 62 | # Installshield output folder 63 | [Ee]xpress 64 | 65 | # DocProject is a documentation generator add-in 66 | DocProject/buildhelp/ 67 | DocProject/Help/*.HxT 68 | DocProject/Help/*.HxC 69 | DocProject/Help/*.hhc 70 | DocProject/Help/*.hhk 71 | DocProject/Help/*.hhp 72 | DocProject/Help/Html2 73 | DocProject/Help/html 74 | 75 | # Click-Once directory 76 | publish 77 | 78 | # Publish Web Output 79 | *.Publish.xml 80 | 81 | # NuGet Packages Directory 82 | packages 83 | 84 | # Windows Azure Build Output 85 | csx 86 | *.build.csdef 87 | 88 | # Windows Store app package directory 89 | AppPackages/ 90 | 91 | # Others 92 | [Bb]in 93 | [Oo]bj 94 | sql 95 | TestResults 96 | [Tt]est[Rr]esult* 97 | *.Cache 98 | ClientBin 99 | [Ss]tyle[Cc]op.* 100 | ~$* 101 | *.dbmdl 102 | Generated_Code #added for RIA/Silverlight projects 103 | 104 | # Backup & report files from converting an old project file to a newer 105 | # Visual Studio version. Backup files are not needed, because we have git ;-) 106 | _UpgradeReport_Files/ 107 | Backup*/ 108 | UpgradeLog*.XML 109 | -------------------------------------------------------------------------------- /CSharpQuickFind/QuickFind.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace QuickFind 4 | { 5 | public class QuickFindAlgorithm 6 | { 7 | private int[] id; 8 | 9 | //Initialize the quick find data structure 10 | public QuickFindAlgorithm(int n) 11 | { 12 | int count = -1; 13 | id = new int[n].Select(x => x = ++count).ToArray(); 14 | } 15 | 16 | //Join two components 17 | public void Union(int p, int q) 18 | { 19 | id = (id[p] != id[q]) ? id.Select(x => (x == id[p]) ? x = id[q] : x).ToArray() : id; 20 | } 21 | 22 | //CHeck if two components are connected 23 | public bool AreConnected(int p, int q) 24 | { 25 | return id[p] == id[q]; 26 | } 27 | 28 | //Count the number of connected components 29 | public int CountComponents() 30 | { 31 | return id.Distinct().Count(); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /CSharpQuickUnion/QuickUnion.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace QuickUnion 8 | { 9 | public class QuickUnionAlgorithm 10 | { 11 | private int[] id; 12 | 13 | //Initialize the quick union data structure 14 | public QuickUnionAlgorithm(int n) 15 | { 16 | int count = -1; 17 | id = new int[n].Select(x => x = ++count).ToArray(); 18 | } 19 | 20 | //Join the components, finding the root of p and q 21 | public void Union(int p, int q) 22 | { 23 | id[FindRoot(p)] = id[FindRoot(q)]; 24 | } 25 | 26 | //Find the root of an element 27 | public int FindRoot(int i) 28 | { 29 | while (id[i] != i) 30 | i = id[i]; 31 | return i; 32 | } 33 | 34 | //Check if two components are connected 35 | public bool AreConnected(int p, int q) 36 | { 37 | return id[p] == id[q]; 38 | } 39 | 40 | //Count the number of components 41 | public int CountComponents() 42 | { 43 | int count = -1; 44 | return this.id.Where(x => x == ++count).Count(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CSharpSelectionSort/SelectionSortClass.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace SelectionSort 4 | { 5 | public class SelectionSortClass 6 | { 7 | public int[] Sort(int[] input) 8 | { 9 | for (int i = 0; i < input.Length - 1; i++) 10 | { 11 | int min = 1000000000; 12 | int minIndex = -1; 13 | for (int a = i + 1; a < input.Length; a++) 14 | { 15 | if (input[a] < input[i] && input[a] < min) 16 | { 17 | min = input[a]; 18 | minIndex = a; 19 | } 20 | } 21 | if (minIndex != -1) 22 | { 23 | int temp = input[i]; 24 | input[i] = input[minIndex]; 25 | input[minIndex] = temp; 26 | } 27 | } 28 | return input; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CSharpShellSort/ShellSortClass.cs: -------------------------------------------------------------------------------- 1 | namespace ShellShort 2 | { 3 | public class ShellSortClass 4 | { 5 | public int[] Sort(int[] input) 6 | { 7 | int totalLength = input.Length; 8 | 9 | int hSort = 1; 10 | while (hSort < totalLength / 3) 11 | hSort = (3 * hSort) + 1; 12 | 13 | while (hSort >= 1) 14 | { 15 | for (int i = hSort; i < totalLength; i++) 16 | { 17 | for (int a = i; a >= hSort && (input[a] < input[a - hSort]); a -= hSort) 18 | { 19 | int temp = input[a]; 20 | input[a] = input[a - hSort]; 21 | input[a - hSort] = temp; 22 | } 23 | } 24 | hSort /= 3; 25 | } 26 | return input; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CSharpWeightedQuickUnion/WeightedQuickUnionAlgortihm.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace QuickUnion 4 | { 5 | public class WeightedQuickUnionAlgorithm 6 | { 7 | private int[] id; 8 | 9 | //Initialize the quick union data structure 10 | public WeightedQuickUnionAlgorithm(int n) 11 | { 12 | int count = -1; 13 | id = new int[n].Select(x => x = ++count).ToArray(); 14 | } 15 | 16 | //Join the components, finding the root of p and q 17 | public void Union(int p, int q) 18 | { 19 | if (countComponentsOfARoot(p) >= countComponentsOfARoot(q)) 20 | id[FindRoot(p)] = id[FindRoot(q)]; 21 | else 22 | id[FindRoot(q)] = id[FindRoot(p)]; 23 | } 24 | 25 | //Find the root of an element 26 | public int FindRoot(int i) 27 | { 28 | while (id[i] != i) 29 | i = id[i]; 30 | return i; 31 | } 32 | 33 | //Check if two components are connected 34 | public bool AreConnected(int p, int q) 35 | { 36 | return id[p] == id[q]; 37 | } 38 | 39 | //Count the number of components 40 | public int CountComponents() 41 | { 42 | int count = -1; 43 | return this.id.Where(x => x == ++count).Count(); 44 | } 45 | 46 | //Count the number of elements in the same root 47 | public int countComponentsOfARoot(int i) 48 | { 49 | return this.id.Where(x => x == id[i]).Count(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /CppInsertionSort/CppInsertionSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oscarbralo/Algorithms/c5ef11934c6e676e59fd1990f95685b042f572cf/CppInsertionSort/CppInsertionSort.cpp -------------------------------------------------------------------------------- /CsharpBinarySearch/BinarySearch.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oscarbralo/Algorithms/c5ef11934c6e676e59fd1990f95685b042f572cf/CsharpBinarySearch/BinarySearch.dll -------------------------------------------------------------------------------- /CsharpBinarySearch/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BinarySearch 4 | { 5 | public class BinarySearch 6 | { 7 | public string BinarySeach(int[] input, int value) 8 | { 9 | int search = value; 10 | int i = 0; 11 | int j = input.Length - 1; 12 | int index = -1; 13 | bool found = false; 14 | while (i <= j) 15 | { 16 | index = i + ((j - i) / 2); 17 | if (input[index] == search) 18 | { 19 | found = true; 20 | break; 21 | } 22 | index = (j + i) / 2; 23 | if (input[index] < search) 24 | i = index + 1; 25 | else 26 | j = index; 27 | } 28 | if (input.Length < 1 || found == false) 29 | return "This value doesn´t exist"; 30 | else 31 | return "The index of this value is: " + index.ToString(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CsharpBubbleSort/BubbleSort.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oscarbralo/Algorithms/c5ef11934c6e676e59fd1990f95685b042f572cf/CsharpBubbleSort/BubbleSort.dll -------------------------------------------------------------------------------- /CsharpBubbleSort/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BubbleSortDLL 4 | { 5 | public class BubbleSort 6 | { 7 | public int[] bubbleSort(int[] input) 8 | { 9 | int temp = 0; 10 | int x = 0; 11 | for (int i = 0; i < input.Length; i++) 12 | { 13 | x = 0; 14 | for (int a = x + 1; a < input.Length - i; a++) 15 | { 16 | if (input[x] > input[a]) 17 | { 18 | temp = input[a]; 19 | input[a] = input[x]; 20 | input[x] = temp; 21 | } 22 | x++; 23 | } 24 | } 25 | return input; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CsharpInsertionSort/InsertionSortDLL.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oscarbralo/Algorithms/c5ef11934c6e676e59fd1990f95685b042f572cf/CsharpInsertionSort/InsertionSortDLL.dll -------------------------------------------------------------------------------- /CsharpInsertionSort/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace InsertionSort 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | } 13 | } 14 | 15 | public class InsertionSort 16 | { 17 | public int[] iSort(int[] input) 18 | { 19 | for (int i = 1; i < input.Length; i++) 20 | { 21 | int key = input[i]; 22 | int j = i - 1; 23 | while (j >= 0 && input[j] > key) 24 | { 25 | input[j + 1] = input[j]; 26 | j--; 27 | } 28 | input[j + 1] = key; 29 | } 30 | return input; 31 | } 32 | } 33 | 34 | public class InsertionSortNonIncreasing 35 | { 36 | public int[] iSortNonIncreasing(int[] input) 37 | { 38 | for (int i = 1; i < input.Length; i++) 39 | { 40 | int key = input[i]; 41 | int j = i - 1; 42 | while (j >= 0 && input[j] < key) 43 | { 44 | input[j + 1] = input[j]; 45 | j--; 46 | } 47 | input[j + 1] = key; 48 | } 49 | return input; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /DijkstraTwoStackAlgorithm/TwoStackAlgo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Stack; 7 | using System.Text.RegularExpressions; 8 | 9 | namespace TwoStackAlgorithm 10 | { 11 | public class TwoStackAlgo 12 | { 13 | private Stack values; 14 | private Stack operators; 15 | 16 | const string oper = "+-*/"; 17 | Regex numbers = new Regex("[0-9]"); 18 | 19 | public TwoStackAlgo() 20 | { 21 | values = new Stack(); 22 | operators = new Stack(); 23 | } 24 | 25 | public int EvaluateExpression(string expression) 26 | { 27 | 28 | expression = expression.Replace(" ", ""); 29 | int result = 0; 30 | if (expression.Count(x => x == '(') != expression.Count(x => x == ')')) 31 | return -1; 32 | for (int i = 0; i < expression.Length; i++) 33 | { 34 | if (numbers.IsMatch(expression[i].ToString())) 35 | values.Push(int.Parse(expression[i].ToString())); 36 | else if (oper.Contains(expression[i])) 37 | operators.Push(expression[i].ToString()); 38 | else if (expression[i] == ')') 39 | { 40 | result = DoOperations(); 41 | values.Push(result); 42 | } 43 | } 44 | while (values.Count() > 1) 45 | { 46 | result = DoOperations(); 47 | values.Push(result); 48 | } 49 | return values.Pop(); 50 | } 51 | 52 | public int DoOperations() 53 | { 54 | int result = 0; 55 | int operandRight = values.Pop(); 56 | int operandLeft = values.Pop(); 57 | string tempOperator = operators.Pop(); 58 | switch (tempOperator) 59 | { 60 | case "+": 61 | result += operandLeft + operandRight; 62 | break; 63 | case "-": 64 | result += operandLeft - operandRight; 65 | break; 66 | case "*": 67 | result += operandLeft * operandRight; 68 | break; 69 | case "/": 70 | result += operandLeft / operandRight; 71 | break; 72 | } 73 | return result; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /PythonLIFO/Stack.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | 3 | class Stack(object): 4 | 5 | def __init__(self): 6 | self.items = [] 7 | 8 | def _push(self, item): 9 | self.items.append(item) 10 | 11 | def _pop(self): 12 | return str(self.items.pop()) 13 | 14 | def _get(self): 15 | return self.items 16 | 17 | def _empty(self): 18 | self.items = [] 19 | 20 | def display(): 21 | 22 | print "1. show the stack." 23 | print "2. push to the stack." 24 | print "3. pop from the stack." 25 | print "4. empty the stack." 26 | print "5. exit." 27 | 28 | def main(): 29 | 30 | print "Stack Algorithm:" 31 | print "Do you want to deal with stack? Y/N" 32 | 33 | ch = raw_input() 34 | if (ch.lower == "n"): 35 | exit(0) 36 | 37 | elif (ch.lower() == "y"): 38 | LIFO = Stack() 39 | print "welcome to the Stack" 40 | print "=" * 20 41 | 42 | while True: 43 | display() 44 | ch = raw_input("choice: ") 45 | 46 | if ch == "1": 47 | print "your stack: " + str(LIFO._get()) 48 | 49 | elif ch == "2": 50 | item = raw_input("enter your item: ") 51 | LIFO._push(item) 52 | 53 | elif ch == "3": 54 | print "you have poped this item: " + LIFO._pop() 55 | 56 | elif ch == "4": 57 | LIFO._empty() 58 | 59 | else: 60 | exit(0) 61 | 62 | if __name__ == "__main__": 63 | main() 64 | -------------------------------------------------------------------------------- /PythonLinearSearch/Program.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | 3 | from sys import argv 4 | 5 | def LinearSearch(pkg, ele): 6 | for i in range(len(pkg)): 7 | if pkg[i] == ele: 8 | return True 9 | 10 | return False 11 | 12 | def main(): 13 | if ((len(argv)) < 2): 14 | print "Usage: {0} ".format(argv[0]) 15 | exit(1) 16 | 17 | pkg = argv[1:len(argv) - 1] 18 | wanted = argv[-1] 19 | 20 | if LinearSearch(pkg, wanted): 21 | print '[+] found your element in your package.' 22 | print '[+] {0} is number {1} from package.'.format(wanted, pkg.index(wanted) + 1) 23 | exit(0) 24 | 25 | else: 26 | print '[!!] sorry {0} not found in your package.'.format(wanted) 27 | exit(0) 28 | 29 | if __name__ == "__main__": 30 | main() 31 | 32 | -------------------------------------------------------------------------------- /PythonMergeSort/Program.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | 3 | from sys import argv 4 | 5 | def mergesort(left, right): 6 | result = [] 7 | i, j = 0, 0 8 | 9 | while i < len(left) and j < len(right): 10 | if left[i] <= right[j]: 11 | result.append(left[i]) 12 | i += 1 13 | 14 | else: 15 | result.append(right[j]) 16 | j += 1 17 | 18 | result += left[i:] 19 | result += right[j:] 20 | 21 | return result 22 | 23 | def merge(lst): 24 | if len(lst) <= 1: 25 | return lst 26 | 27 | middle = len(lst) / 2 28 | left = merge(lst[ : middle]) 29 | right = merge(lst[middle : ]) 30 | 31 | return mergesort(left, right) 32 | 33 | def main(): 34 | if len(argv) < 2: 35 | print "Usage: {0} .".format(argv[0]) 36 | exit(1) 37 | 38 | ur_pkg = map(int, argv[1:]) 39 | print "[+] Your package before sorting >> " + str(ur_pkg) 40 | 41 | nw_pkg = merge(ur_pkg) 42 | print "[+] Your package after merge sorting >> " + str(nw_pkg) 43 | 44 | if __name__ == '__main__': 45 | main() 46 | -------------------------------------------------------------------------------- /PythonSelectionSort/Program.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | 3 | from sys import argv 4 | 5 | def SmallestIndx(pkg, ele): 6 | min_Indx = ele 7 | for j in range(ele + 1, len(pkg)): 8 | if pkg[j] < pkg[min_Indx]: 9 | min_Indx = j 10 | 11 | return min_Indx 12 | 13 | def SelecSort(pkg): 14 | for i in range(len(pkg)): 15 | smallest_index = SmallestIndx(pkg, i) 16 | pkg[smallest_index], pkg[i] = pkg[i], pkg[smallest_index] 17 | 18 | return pkg 19 | 20 | def main(): 21 | if (len(argv) < 2): 22 | print "Usage: {0} ".format(argv[0]) 23 | exit(1) 24 | 25 | pkg = map(int, argv[1:]) 26 | print '[*] Your package before sorting >> ' + str(pkg) 27 | 28 | newpkg = SelecSort(pkg) 29 | print '[+] Your package after sorting >> ' + str(newpkg) 30 | 31 | if __name__ == "__main__": 32 | main() 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms # 2 | --- 3 | 4 | Some Algorithms in code. 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) {{year}} {{fullname}} 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | --------------------------------------------------------------------------------