├── .github ├── code-of-conduct.md ├── contributing.md ├── issue-template.md └── pull-request-template.md ├── algorithms ├── cryptography │ ├── caesar.cs │ └── playfair.cs ├── data-structures │ ├── DoublyLinkedList.cs │ ├── linkedlist.cs │ ├── queue.cs │ └── stack.cs ├── dizeleri │ └── replacing.cs ├── dynamic-programming │ └── coin change │ │ └── CoinChange.cs ├── greedy │ ├── Dijkstra.cs │ └── Prim.cs ├── math │ ├── IterativePermutation.cs │ ├── isPerfectSquare.cs │ └── isPrime.cs ├── maze-generator │ └── binary-space-partitioning │ │ ├── Program.cs │ │ ├── Room.cs │ │ └── Tile.cs ├── searches │ ├── GenericBinarySearchTree.cs │ ├── InterpolationSearch.cs │ ├── binarysearch.cs │ ├── jumpsearch.cs │ ├── linearsearch.cs │ └── ternarysearch.cs ├── sorting │ ├── bitonicsort.cs │ ├── bogosort.cs │ ├── bubblesort.cs │ ├── bucketsort.cs │ ├── cocktailsort.cs │ ├── countingsort.cs │ ├── heapsort.cs │ ├── insertionsort.cs │ ├── mergesort.cs │ ├── quicksort.cs │ ├── radixsort.cs │ ├── selectionsort.cs │ ├── shellsort.cs │ └── timsort.cs └── strings │ ├── FilterIntegers.cs │ ├── Levenshtein.cs │ ├── RabinKarp.cs │ ├── StringReverse.cs │ ├── palindrome.cs │ └── reversestring.cs ├── license ├── readme.md └── src └── .gitkeep /.github/code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Attribution 36 | 37 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 38 | 39 | [homepage]: http://contributor-covenant.org 40 | [version]: http://contributor-covenant.org/version/1/4/ 41 | -------------------------------------------------------------------------------- /.github/contributing.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | > Please note that this project is released with a [Contributor Code of Conduct](code-of-conduct.md). By participating in this project you agree to abide by its terms. -------------------------------------------------------------------------------- /.github/issue-template.md: -------------------------------------------------------------------------------- 1 | I am creating an issue because... 2 | -------------------------------------------------------------------------------- /.github/pull-request-template.md: -------------------------------------------------------------------------------- 1 | I am creating a pull request for... 2 | 3 | - [ ] New algorithm 4 | - [ ] Update to an algorithm 5 | - [ ] Fix an error 6 | - [ ] Other - *Describe below* -------------------------------------------------------------------------------- /algorithms/cryptography/caesar.cs: -------------------------------------------------------------------------------- 1 | // Encode a char 2 | static char EncodeCaractere(char caractere, char encodeur) 3 | { 4 | int asciiCaractere = (int)caractere; 5 | int asciiEncodeur = (int)encodeur; 6 | int asciiCaractereCode = ((asciiCaractere - 97) + (asciiEncodeur - 97)) % 26; 7 | char caractereCode = (char)(asciiCaractereCode + 97); 8 | 9 | return caractereCode; 10 | } 11 | 12 | // Decode a char 13 | static char DecodeCaractere(char caractere, char encodeur) 14 | { 15 | int asciiCaractere = (int)caractere; 16 | int asciiEncodeur = (int)encodeur; 17 | int asciiCaractereCode = ((asciiCaractere - 97) - (asciiEncodeur - 97)) % 26; 18 | char caractereCode = (char)(asciiCaractereCode + 97); 19 | 20 | return caractereCode; 21 | } 22 | 23 | // Encode a word 24 | static String EncodeMot(String mot, Char encodeur) 25 | { 26 | char[] retour = new char[mot.Length]; 27 | for(int i = 0; i < retour.Length; i++) 28 | { 29 | retour[i] = EncodeCaractere(mot[i], encodeur); 30 | } 31 | string str = new string(retour); 32 | return str; 33 | } 34 | 35 | // Decode a word 36 | static String DecodeMot(String mot, Char encodeur) 37 | { 38 | char[] retour = new char[mot.Length]; 39 | for (int i = 0; i < retour.Length; i++) 40 | { 41 | retour[i] = DecodeCaractere(mot[i], encodeur); 42 | } 43 | string str = new string(retour); 44 | return str; 45 | } 46 | 47 | // Encode a sentence 48 | static String EncodePhrase(String phrase, Char encodeur) 49 | { 50 | String[] ph = phrase.Split(' '); 51 | String[] retour = ph; 52 | for (int i = 0; i < ph.Length; i++) 53 | { 54 | retour[i] = EncodeMot(ph[i], encodeur); 55 | } 56 | StringBuilder builder = new StringBuilder(); 57 | foreach(string str in retour) 58 | { 59 | builder.Append(str); 60 | builder.Append(" "); 61 | } 62 | return builder.ToString(); 63 | } 64 | 65 | // Decode a sentence 66 | static String DecodePhrase(String phrase, Char encodeur) 67 | { 68 | String[] ph = phrase.Split(' '); 69 | String[] retour = ph; 70 | for (int i = 0; i < ph.Length; i++) 71 | { 72 | retour[i] = DecodeMot(ph[i], encodeur); 73 | } 74 | StringBuilder builder = new StringBuilder(); 75 | foreach (string str in retour) 76 | { 77 | builder.Append(str); 78 | builder.Append(" "); 79 | } 80 | return builder.ToString(); 81 | } 82 | 83 | // Main method to encode any text input 84 | static void CesarEncode() 85 | { 86 | String userInput = Console.ReadLine(); 87 | String crypt = EncodePhrase(userInput, 'c'); 88 | Console.WriteLine("La phrase cryptée est la suivante :"); 89 | Console.WriteLine(crypt); 90 | } 91 | 92 | // Main method to decode any text input 93 | static void CesarDecode() 94 | { 95 | StreamReader sr = new StreamReader(fichier, true); 96 | StringBuilder builder = new StringBuilder(); 97 | while (sr.Peek() > 0) 98 | { 99 | builder.Append(sr.ReadLine()); 100 | builder.Append(" "); 101 | } 102 | sr.Close(); 103 | String code = builder.ToString(); 104 | String dcode = DecodePhrase(code, 'c'); 105 | Console.WriteLine("La phrase décryptée est la suivante :"); 106 | Console.WriteLine(dcode); 107 | 108 | } 109 | -------------------------------------------------------------------------------- /algorithms/cryptography/playfair.cs: -------------------------------------------------------------------------------- 1 | // HOW TO USE: 2 | // 3 | // Encode: _msg -> a message that you want to encode 4 | // key -> can be a word, a sentecen, you decide 5 | // 6 | // Decode: _msg -> a message already encoded before 7 | // key -> the same key used to encode _msg 8 | // NOTES: 9 | // 10 | // the algorithm will fill the table with chars from _key 11 | // and ignore all duplicates chars 12 | // 13 | // 'I' and 'J' will be assumed as only one letter 14 | // So, if the decode msg == jojnthehacktoberfest 15 | // it means: join the hacktoberfest 16 | // 17 | // if the last char cant form a pair, a 'Q' will be add to de end (nobody alone here, okay?) 18 | // decoded msg == abcq 19 | // it means: abc 20 | // 21 | // ************** ONLY USE LETTERS AND BLANK SPACES, OTHERWISE, IT WILL CRASH ************** 22 | // ************** YOU HAVE BEEN WARNED ***************************************************** 23 | // 24 | // have fun c: 25 | 26 | static string playfair_lowcase(string _msg, string _key) 27 | { 28 | const string alphabet = "abcdefghijklmnopqrstuvwxyz"; 29 | char[,] table = new char[5,5]; 30 | int count = 0; 31 | 32 | //Remove all spaces and replace all 'i' with 'j' 33 | //Playfair also accepts remove a rare char(e.g. X or Q) instead 34 | string msg = _msg.Replace('i', 'j').Replace(" ", ""); 35 | string key = (_key + alphabet).Replace('i', 'j').Replace(" ", ""); 36 | 37 | //Check for char alones 38 | //Playfair only work with pairs 39 | if((msg.Length % 2) != 0) 40 | msg += 'q'; 41 | 42 | //Remove duplicates from the key 43 | key = RemoveDuplicates(key); 44 | 45 | char[] msg_array = msg.ToCharArray(); 46 | char[] key_array = key.ToCharArray(); 47 | 48 | //Fill the table with key 49 | for(int x = 0; x < 5; x++) 50 | for(int y = 0; y < 5; y++) 51 | { 52 | table[x,y] = key_array[count]; 53 | count++; 54 | } 55 | 56 | char A, B; 57 | int[] a = { 0, 0 }; 58 | int[] b = { 0, 0 }; 59 | bool aCheck = false; 60 | bool bCheck = false; 61 | bool writed = false; 62 | 63 | string out_msg = ""; 64 | 65 | //Encode All Sentence 66 | for(int x = 0; x < msg_array.Length; x ++) 67 | { 68 | A = msg_array[x]; 69 | B = msg_array[(x + 1)]; 70 | 71 | aCheck = false; 72 | bCheck = false; 73 | writed = false; 74 | 75 | //Get the pairs 76 | for(int y = 0; y < 5; y++) 77 | for(int z = 0; z < 5; z++) 78 | { 79 | //Get pos of A 80 | if(A == table[y,z]) 81 | { 82 | a[0] = y; 83 | a[1] = z; 84 | 85 | aCheck = true; 86 | } 87 | 88 | //Get pos of B 89 | if(B == table[y,z]) 90 | { 91 | b[0] = y; 92 | b[1] = z; 93 | 94 | bCheck = true; 95 | } 96 | 97 | //Get the AB pair encode/decode when the app have A and B 98 | if(aCheck && bCheck) 99 | //Check write the same encode/decode pair only one time 100 | if(!writed) 101 | { 102 | char aCh = table[a[0], b[1]]; 103 | char bCh = table[b[0], a[1]]; 104 | 105 | out_msg += aCh; 106 | out_msg += bCh; 107 | 108 | writed = true; 109 | } 110 | } 111 | 112 | x++; 113 | } 114 | 115 | return out_msg; 116 | } 117 | 118 | static string RemoveDuplicates(string str) 119 | { 120 | return new string(str.ToCharArray().Distinct().ToArray()); 121 | } -------------------------------------------------------------------------------- /algorithms/data-structures/DoublyLinkedList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class DoublyLinkedList { 4 | 5 | private Node _front; 6 | private Node _end; 7 | 8 | /// 9 | /// Count of list's elements 10 | /// 11 | public int Count { get; private set; } 12 | 13 | /// 14 | /// Insert an item at the front of the list. 15 | /// 16 | /// Item to insert. 17 | public void InsertAtFront(T item) 18 | { 19 | var node = Node.Create(item); 20 | node.Next = _front; 21 | _front.Previous = node; 22 | _front = node; 23 | if (_end == null){ 24 | _end = _front; 25 | } 26 | } 27 | 28 | /// 29 | /// Insert an item at the end of the list. 30 | /// 31 | /// Item to insert. 32 | public void InsertAtEnd(T item) 33 | { 34 | var node = Node.Create(item); 35 | if (_end == null) 36 | { 37 | _front = node; 38 | _end = _front; 39 | } 40 | else 41 | { 42 | node.Previous = _end; 43 | _end.Next = node; 44 | _end = node; 45 | } 46 | } 47 | 48 | /// 49 | /// Search an item from the list. 50 | /// 51 | /// Item to search. 52 | /// True if found, otherwise false. 53 | public bool Search(T item) 54 | { 55 | var found = false; 56 | var node = _front; 57 | while (node != null) 58 | { 59 | if (node.Data.Equals(item)) 60 | { 61 | found = true; 62 | break; 63 | } 64 | node = node.Next; 65 | } 66 | return found; 67 | } 68 | 69 | /// 70 | /// Returns the content of the list as string. 71 | /// 72 | /// Content of the list as string. 73 | public override string ToString() 74 | { 75 | var result = string.Empty; 76 | var node = _front; 77 | while (node != null) 78 | { 79 | result += node.Data.ToString() + " "; 80 | node = node.Next; 81 | } 82 | return result.Trim(); 83 | } 84 | 85 | /// 86 | /// Clears the list. 87 | /// 88 | public void Clear() 89 | { 90 | _front = null; 91 | _end = null; 92 | } 93 | } 94 | 95 | /// 96 | /// Generic linked list's node. 97 | /// 98 | public class Node 99 | { 100 | /// 101 | /// Creates a new instance of node. 102 | /// 103 | /// Data instance. 104 | private Node(T data) 105 | { 106 | Data = data; 107 | } 108 | 109 | /// 110 | /// Gets the node data. 111 | /// 112 | public T Data { get; private set; } 113 | 114 | /// 115 | /// Gets or sets the next node. 116 | /// 117 | public Node Next { get; set; } 118 | 119 | /// 120 | /// Gets or sets the previous node. 121 | /// 122 | public Node Previous { get; set; } 123 | 124 | /// 125 | /// Creates a new instance of node. 126 | /// 127 | /// Data instance. 128 | /// Node instance. 129 | public static Node Create(T data) 130 | { 131 | return new Node(data); 132 | } 133 | } 134 | 135 | class DoublyLinkedListTest 136 | { 137 | static void Main(string[] args) 138 | { 139 | DoublyLinkedList list = new DoublyLinkedList(); 140 | 141 | list.InsertAtEnd(1); 142 | list.InsertAtEnd(2); 143 | list.InsertAtEnd(3); 144 | list.InsertAtFront(0); 145 | list.InsertAtFront(-1); 146 | list.InsertAtFront(-2); 147 | 148 | Console.WriteLine("Searching for 2, found: {0}", list.Search(2)); 149 | Console.WriteLine("Searching for -1, found: {0}", list.Search(-1)); 150 | Console.WriteLine("Searching for 10, found: {0}", list.Search(10)); 151 | Console.WriteLine("Linked list content: {0}", list.ToString()); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /algorithms/data-structures/linkedlist.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | /// 5 | /// Generic linked list implementation. 6 | /// 7 | /// Item type. 8 | class LinkedList 9 | { 10 | private Node _front; 11 | private Node _end; 12 | 13 | /// 14 | /// Initialize the linked list. 15 | /// 16 | public LinkedList() 17 | { 18 | } 19 | 20 | /// 21 | /// Insert an item at the front of the list. 22 | /// 23 | /// Item to insert. 24 | public void InsertAtFront(T item) 25 | { 26 | var node = Node.Create(item); 27 | node.Next = _front; 28 | _front = node; 29 | if (_end == null) 30 | _end = _front; 31 | } 32 | 33 | /// 34 | /// Insert an item at the end of the list. 35 | /// 36 | /// Item to insert. 37 | public void InsertAtEnd(T item) 38 | { 39 | var node = Node.Create(item); 40 | if (_end == null) 41 | { 42 | _front = node; 43 | _end = _front; 44 | } 45 | else 46 | { 47 | _end.Next = node; 48 | _end = node; 49 | } 50 | } 51 | 52 | /// 53 | /// Search an item from the list. 54 | /// 55 | /// Item to search. 56 | /// True if found, otherwise false. 57 | public bool Search(T item) 58 | { 59 | var found = false; 60 | var node = _front; 61 | while (node != null) 62 | { 63 | if (node.Data.Equals(item)) 64 | { 65 | found = true; 66 | break; 67 | } 68 | node = node.Next; 69 | } 70 | return found; 71 | } 72 | 73 | /// 74 | /// Returns the content of the list as string. 75 | /// 76 | /// Content of the list as string. 77 | public override string ToString() 78 | { 79 | var result = string.Empty; 80 | var node = _front; 81 | while (node != null) 82 | { 83 | result += node.Data.ToString() + " "; 84 | node = node.Next; 85 | } 86 | return result.Trim(); 87 | } 88 | 89 | /// 90 | /// Clears the list. 91 | /// 92 | public void Clear() 93 | { 94 | _front = null; 95 | _end = null; 96 | } 97 | } 98 | 99 | /// 100 | /// Generic linked list's node. 101 | /// 102 | class Node 103 | { 104 | /// 105 | /// Creates a new instance of node. 106 | /// 107 | /// Data instance. 108 | private Node(T data) 109 | { 110 | Data = data; 111 | } 112 | 113 | /// 114 | /// Gets the node data. 115 | /// 116 | public T Data { get; private set; } 117 | 118 | /// 119 | /// Gets or sets the next node. 120 | /// 121 | public Node Next { get; set; } 122 | 123 | /// 124 | /// Creates a new instance of node. 125 | /// 126 | /// Data instance. 127 | /// Node instance. 128 | public static Node Create(T data) 129 | { 130 | return new Node(data); 131 | } 132 | } 133 | 134 | class LinkedListTest 135 | { 136 | public static void Main() 137 | { 138 | var list = new LinkedList(); 139 | list.InsertAtFront(3); 140 | list.InsertAtEnd(4); 141 | list.InsertAtEnd(5); 142 | list.InsertAtFront(2); 143 | list.InsertAtFront(1); 144 | Console.WriteLine("Searching for 2, found: {0}", list.Search(2)); 145 | Console.WriteLine("Searching for 5, found: {0}", list.Search(5)); 146 | Console.WriteLine("Searching for 10, found: {0}", list.Search(10)); 147 | Console.WriteLine("Linked list content: {0}", list.ToString()); 148 | } 149 | } -------------------------------------------------------------------------------- /algorithms/data-structures/queue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | /// 5 | /// Generic queue (First In First Out) implementation using linked list. 6 | /// 7 | /// Item type. 8 | class Queue 9 | { 10 | private Node _head; 11 | private Node _tail; 12 | 13 | /// 14 | /// Initialize the queue. 15 | /// 16 | public Queue() 17 | { 18 | } 19 | 20 | /// 21 | /// Enqueue an item. 22 | /// 23 | /// Item to queue. 24 | public void Enqueue(T item) 25 | { 26 | var node = Node.Create(item); 27 | if (_head == null) 28 | { 29 | _head = node; 30 | _tail = node; 31 | } 32 | else 33 | { 34 | _tail.Next = node; 35 | _tail = node; 36 | } 37 | } 38 | 39 | /// 40 | /// Dequeue an item. 41 | /// 42 | /// Dequeued item. 43 | public T Dequeue() 44 | { 45 | return DequeueOrPeek(true); 46 | } 47 | 48 | /// 49 | /// Peek an item. 50 | /// 51 | /// Peeked item. 52 | public T Peek() 53 | { 54 | return DequeueOrPeek(false); 55 | } 56 | 57 | private T DequeueOrPeek(bool dequeue) 58 | { 59 | if (_head == null) 60 | throw new InvalidOperationException("Queue is empty."); 61 | var node = _head; 62 | 63 | if (dequeue) 64 | { 65 | _head = _head.Next; 66 | if (_head == null) 67 | _tail = null; 68 | } 69 | return node.Data; 70 | } 71 | 72 | /// 73 | /// Returns the content of the queue as string. 74 | /// 75 | /// Content of the queue as string. 76 | public override string ToString() 77 | { 78 | var result = string.Empty; 79 | var node = _head; 80 | while (node != null) 81 | { 82 | result += node.Data.ToString() + " "; 83 | node = node.Next; 84 | } 85 | return result.Trim(); 86 | } 87 | 88 | /// 89 | /// Clears the queue. 90 | /// 91 | public void Clear() 92 | { 93 | _head = null; 94 | _tail = null; 95 | } 96 | } 97 | 98 | /// 99 | /// Generic linked list's node. 100 | /// 101 | class Node 102 | { 103 | /// 104 | /// Creates a new instance of node. 105 | /// 106 | /// Data instance. 107 | private Node(T data) 108 | { 109 | Data = data; 110 | } 111 | 112 | /// 113 | /// Gets the node data. 114 | /// 115 | public T Data { get; private set; } 116 | 117 | /// 118 | /// Gets or sets the next node. 119 | /// 120 | public Node Next { get; set; } 121 | 122 | /// 123 | /// Creates a new instance of node. 124 | /// 125 | /// Data instance. 126 | /// Node instance. 127 | public static Node Create(T data) 128 | { 129 | return new Node(data); 130 | } 131 | } 132 | 133 | class QueueTest 134 | { 135 | public static void Main() 136 | { 137 | var queue = new Queue(); 138 | queue.Enqueue(1); 139 | queue.Enqueue(2); 140 | queue.Enqueue(3); 141 | Console.WriteLine("Dequeued: {0}", queue.Dequeue()); 142 | queue.Enqueue(4); 143 | queue.Enqueue(5); 144 | Console.WriteLine("Peeked: {0}", queue.Peek()); 145 | queue.Enqueue(6); 146 | queue.Enqueue(7); 147 | Console.WriteLine("Peeked: {0}", queue.Peek()); 148 | queue.Enqueue(8); 149 | queue.Enqueue(9); 150 | queue.Enqueue(10); 151 | Console.WriteLine("Dequeued: {0}", queue.Dequeue()); 152 | Console.WriteLine("Dequeued: {0}", queue.Dequeue()); 153 | Console.WriteLine("Queue contents: {0}", queue.ToString()); 154 | } 155 | } -------------------------------------------------------------------------------- /algorithms/data-structures/stack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | /// 5 | /// Generic stack (Last In First Out) implementation using an array. 6 | /// 7 | /// Item type. 8 | class Stack 9 | { 10 | private T[] _array; 11 | public int _size; 12 | public int _top; 13 | 14 | /// 15 | /// Initialize the stack. 16 | /// 17 | public Stack() 18 | { 19 | _size = 10; 20 | _top = -1; 21 | _array = new T[_size]; 22 | } 23 | 24 | /// 25 | /// Push an item to the stack. 26 | /// 27 | /// Item to be pushed. 28 | public void Push(T item) 29 | { 30 | if (_top == _size - 1) 31 | { 32 | // Array top capacity reached. resizing array 33 | _size = _size * 2; 34 | T[] newArray = new T[_size]; 35 | Array.Copy(_array, 0, newArray, 0, _array.Length); 36 | _array = newArray; 37 | } 38 | _top++; 39 | _array[_top] = item; 40 | } 41 | 42 | /// 43 | /// Pop last item from the stack. Pop removes the popped item from the stack. 44 | /// 45 | /// Popped item. 46 | public T Pop() 47 | { 48 | return PopOrPeek(true); 49 | } 50 | 51 | /// 52 | /// Peek last item from the stack. Peek won't remove the item from the stack. 53 | /// 54 | /// Peeked item. 55 | public T Peek() 56 | { 57 | return PopOrPeek(false); 58 | } 59 | 60 | private T PopOrPeek(bool pop) 61 | { 62 | if (_top == -1) 63 | throw new InvalidOperationException("Stack is empty."); 64 | T item = _array[_top]; 65 | 66 | if (pop) 67 | { 68 | _array[_top] = default(T); 69 | _top--; 70 | } 71 | return item; 72 | } 73 | 74 | /// 75 | /// Returns the content of the stack as string. 76 | /// 77 | /// Content of the stack as string. 78 | public override string ToString() 79 | { 80 | var result = string.Empty; 81 | for (int i = 0; i<= _top; i++) 82 | { 83 | result += _array[i].ToString() + " "; 84 | } 85 | return result.Trim(); 86 | } 87 | 88 | /// 89 | /// Clear all items from the stack. 90 | /// 91 | public void Clear() 92 | { 93 | Array.Clear(_array, 0, _size); 94 | _top = -1; 95 | } 96 | } 97 | 98 | class StackTest 99 | { 100 | public static void Main() 101 | { 102 | var stack = new Stack(); 103 | stack.Push(1); 104 | stack.Push(2); 105 | stack.Push(3); 106 | Console.WriteLine("Popped: {0}", stack.Pop()); 107 | stack.Push(4); 108 | stack.Push(5); 109 | Console.WriteLine("Peeked: {0}", stack.Peek()); 110 | stack.Push(6); 111 | stack.Push(7); 112 | Console.WriteLine("Peeked: {0}", stack.Peek()); 113 | stack.Push(8); 114 | stack.Push(9); 115 | stack.Push(10); 116 | Console.WriteLine("Popped: {0}", stack.Pop()); 117 | Console.WriteLine("Popped: {0}", stack.Pop()); 118 | Console.WriteLine("Stack contents: {0}", stack.ToString()); 119 | } 120 | } -------------------------------------------------------------------------------- /algorithms/dizeleri/replacing.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 test 8 | { 9 | public class Program 10 | { 11 | public static string replacing(string keyword) 12 | { 13 | char[] trChars = { 'ı', 'ğ', 'İ', 'Ğ', 'ç', 'Ç', 'ş', 'Ş', 'ö', 'Ö', 'ü', 'Ü', ' ' }; 14 | char[] engChars = { 'i', 'g', 'I', 'G', 'c', 'C', 's', 'S', 'o', 'O', 'u', 'U', '-' }; 15 | for (int i = 0; i < trChars.Length; i++) 16 | { 17 | 18 | keyword = keyword.Replace(trChars[i], engChars[i]); 19 | 20 | } 21 | return keyword; 22 | } 23 | static void Main(string[] args) 24 | { 25 | string key = "Merhaba Dünya"; 26 | Console.WriteLine(replacing(key));//Merhaba-Dunya 27 | Console.ReadKey(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /algorithms/dynamic-programming/coin change/CoinChange.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 CoinChange 8 | { 9 | class CoinChange 10 | { 11 | const int LENGTH = 15; 12 | int[] coins; // Coins Type 13 | int[][] memos = new int[LENGTH][]; // Used For Memoization 14 | 15 | public CoinChange(int[] coins) 16 | { 17 | this.coins = coins; 18 | 19 | for (int i = 0; i < LENGTH; i++) 20 | { 21 | memos[i] = new int[LENGTH]; 22 | 23 | for (int j = 0; j < LENGTH; j++) 24 | { 25 | memos[i][j] = -1; // Initialize Default Value 26 | } 27 | } 28 | } 29 | 30 | public int Start(int currentIndex, int currentValue) 31 | { 32 | if (currentIndex >= coins.Length || currentValue < 0) 33 | return 0; 34 | 35 | if (currentValue == 0) 36 | return 1; 37 | 38 | if (memos[currentIndex][currentValue] != -1) 39 | return memos[currentIndex][currentValue]; 40 | 41 | int totalWays = 0; 42 | 43 | totalWays += Start(currentIndex, currentValue - coins[currentIndex]); 44 | totalWays += Start(currentIndex + 1, currentValue); 45 | memos[currentIndex][currentValue] = totalWays; 46 | 47 | return totalWays; 48 | } 49 | 50 | 51 | static void Main(string[] args) 52 | { 53 | int moneyToReturn = 7; 54 | int[] coinTypes = { 2, 3, 5 }; 55 | 56 | CoinChange coinChangeCalculator = new CoinChange(coinTypes); 57 | int totalWays = coinChangeCalculator.Start(0, moneyToReturn); 58 | 59 | Console.WriteLine("Total Ways To Return {0} is {1}.", moneyToReturn, totalWays); 60 | Console.ReadKey(); // Hold The Screen 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /algorithms/greedy/Dijkstra.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics; 6 | 7 | namespace DijkstraAlgorithm 8 | { 9 | class Dijkstra 10 | { 11 | 12 | private static int MinimumDistance(int[] distance, bool[] shortestPathTreeSet, int verticesCount) 13 | { 14 | int min = int.MaxValue; 15 | int minIndex = 0; 16 | 17 | for (int v = 0; v < verticesCount; ++v) 18 | { 19 | if (shortestPathTreeSet[v] == false && distance[v] <= min) 20 | { 21 | min = distance[v]; 22 | minIndex = v; 23 | } 24 | } 25 | 26 | return minIndex; 27 | } 28 | 29 | private static void OutputDistance(int[] distance, int verticesCount) 30 | { 31 | Console.WriteLine("Vertex Distance from source"); 32 | 33 | for (int i = 0; i < verticesCount; ++i) 34 | Console.WriteLine("{0}\t {1}", i, distance[i]); 35 | } 36 | 37 | public static void DijkstraAlgo(int[,] graph, int source, int verticesCount) 38 | { 39 | int[] distance = new int[verticesCount]; 40 | bool[] shortestPathTreeSet = new bool[verticesCount]; 41 | 42 | for (int i = 0; i < verticesCount; ++i) 43 | { 44 | distance[i] = int.MaxValue; 45 | shortestPathTreeSet[i] = false; 46 | } 47 | 48 | distance[source] = 0; 49 | 50 | for (int count = 0; count < verticesCount - 1; ++count) 51 | { 52 | int u = MinimumDistance(distance, shortestPathTreeSet, verticesCount); 53 | shortestPathTreeSet[u] = true; 54 | 55 | for (int v = 0; v < verticesCount; ++v) 56 | if (!shortestPathTreeSet[v] && Convert.ToBoolean(graph[u, v]) && distance[u] != int.MaxValue && distance[u] + graph[u, v] < distance[v]) 57 | distance[v] = distance[u] + graph[u, v]; 58 | } 59 | 60 | OutputDistance(distance, verticesCount); 61 | } 62 | 63 | static void Main(string[] args) 64 | { 65 | int[,] graph = { 66 | { 0, 6, 0, 0, 0, 0, 0, 9, 0 }, 67 | { 6, 0, 9, 0, 0, 0, 0, 11, 0 }, 68 | { 0, 9, 0, 5, 0, 6, 0, 0, 2 }, 69 | { 0, 0, 5, 0, 9, 16, 0, 0, 0 }, 70 | { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, 71 | { 0, 0, 6, 0, 10, 0, 2, 0, 0 }, 72 | { 0, 0, 0, 16, 0, 2, 0, 1, 6 }, 73 | { 9, 11, 0, 0, 0, 0, 1, 0, 5 }, 74 | { 0, 0, 2, 0, 0, 0, 6, 5, 0 } 75 | }; 76 | 77 | DijkstraAlgo(graph, 0, 9); 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /algorithms/greedy/Prim.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Prim { 4 | static void PrimAlgorithm (int V, int[, ] G) { 5 | // create a array to track selected vertex 6 | int INF = 9999999; 7 | // set number of edge to 0 8 | int no_edge = 0; 9 | /* the number of egde in minimum spanning tree will be 10 | always less than(V - 1), where V is number of vertices in 11 | graph */ 12 | int[] selected = new int[V]; 13 | for (int i = 0; i < V; i++) { 14 | selected[i] = 0; 15 | } 16 | // choose 0th vertex and make it true 17 | selected[0] = 1; 18 | // print for edge and weight 19 | Console.WriteLine ("Edge : Weight\n"); 20 | while (no_edge < V - 1) { 21 | /* For every vertex in the set S, find the all adjacent vertices 22 | calculate the distance from the vertex selected at step 1. 23 | if the vertex is already in the set S, discard it otherwise 24 | choose another vertex nearest to selected vertex at step 1. */ 25 | int minimum = INF; 26 | int x = 0; 27 | int y = 0; 28 | for (int i = 0; i < V; i++) { 29 | if (selected[i] == 1) { 30 | for (int j = 0; j < V; j++) { 31 | if ((selected[j] == 0) && G[i, j] > 0) { 32 | // not in selected and there is an edge 33 | if (minimum > G[i, j]) { 34 | minimum = G[i, j]; 35 | x = i; 36 | y = j; 37 | } 38 | } 39 | } 40 | } 41 | } 42 | Console.WriteLine (x + "-" + y + ":" + G[x, y]); 43 | selected[y] = 1; 44 | no_edge += 1; 45 | } 46 | } 47 | 48 | static void Main (string[] args) { 49 | 50 | int V = 5; 51 | /* create a 2d array of size 5x5 52 | for adjacency matrix to represent graph */ 53 | int[, ] G = new int[5, 5] { { 0, 9, 75, 0, 0 }, { 9, 0, 95, 19, 42 }, { 75, 95, 0, 51, 66 }, { 0, 19, 51, 0, 31 }, { 0, 42, 66, 31, 0 } 54 | 55 | }; 56 | PrimAlgorithm (V, G); 57 | } 58 | } -------------------------------------------------------------------------------- /algorithms/math/IterativePermutation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | public static class GeneratePermutationsIteratively 5 | { 6 | public static void Main() 7 | { 8 | Console.WriteLine("Enter a number: "); 9 | var num = int.Parse(Console.ReadLine()); 10 | 11 | var numberOfPerm = 1; 12 | var elements = Enumerable.Range(1, num).ToArray(); 13 | var workArr = Enumerable.Range(0, elements.Length + 1).ToArray(); 14 | 15 | PrintPerm(elements); 16 | var index = 1; 17 | while (index < elements.Length) 18 | { 19 | workArr[index]--; 20 | var j = 0; 21 | if (index % 2 == 1) 22 | { 23 | j = workArr[index]; 24 | } 25 | 26 | SwapInts(ref elements[j], ref elements[index]); 27 | index = 1; 28 | while (workArr[index] == 0) 29 | { 30 | workArr[index] = index; 31 | index++; 32 | } 33 | 34 | numberOfPerm++; 35 | PrintPerm(elements); 36 | } 37 | 38 | Console.WriteLine("Total permutations: {0}", numberOfPerm); 39 | } 40 | 41 | private static void PrintPerm(int[] elements) 42 | { 43 | Console.WriteLine(string.Join(", ", elements)); 44 | } 45 | 46 | private static void SwapInts(ref int a, ref int b) 47 | { 48 | a ^= b; 49 | b ^= a; 50 | a ^= b; 51 | } 52 | } -------------------------------------------------------------------------------- /algorithms/math/isPerfectSquare.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | /// 4 | /// This is a c# algorithm to check 5 | /// if a number is a perfect square 6 | /// 7 | public class Math 8 | { 9 | public IsPerfectSquare(double num) 10 | { 11 | double result = Math.Sqrt(num); 12 | bool isPerfectSquare = result % 1 == 0; 13 | } 14 | 15 | 16 | ///[Test] 17 | ///isPerfectSquare returns true for all squares 18 | ///isPerfectSquare returns false for non perfect squares 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/math/isPrime.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Program 4 | { 5 | public static bool IsPrime(int num) 6 | { 7 | if (num <= 1) return false; 8 | if (num == 2) return true; 9 | if (num % 2 == 0) return false; 10 | 11 | var mid=(int)Math.Floor((Math.Sqrt(num))); 12 | 13 | for (int i = 3; i <= mid; i+=2) 14 | { 15 | if((num%i) == 0) 16 | { 17 | return false; 18 | } 19 | } 20 | return true; 21 | } 22 | } -------------------------------------------------------------------------------- /algorithms/maze-generator/binary-space-partitioning/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BinarySpacePartitioning 4 | { 5 | class Program 6 | { 7 | const int MAP_SIZE = 50, // Map Size 8 | MIN_LENGTH = 5; // The Smallest Room Size 9 | 10 | Room root; // The Biggest Room 11 | Tile[][] map; // The Floors / Tiles 12 | 13 | public Program() 14 | { 15 | // Initialize Map Size 16 | map = new Tile[MAP_SIZE][]; 17 | for (int i = 0; i < MAP_SIZE; i++) 18 | { 19 | map[i] = new Tile[MAP_SIZE]; 20 | 21 | for (int j = 0; j < MAP_SIZE; j++) 22 | { 23 | map[i][j] = new Tile(); 24 | } 25 | } 26 | 27 | // Create First Room (The Biggest One) 28 | root = MakeRoom(0, 0, MAP_SIZE, MAP_SIZE); 29 | 30 | // Split The Room 31 | SplitRoom(root); 32 | 33 | // Flagging Each Point As Wall 34 | MakeWall(root); 35 | 36 | // Print Map 37 | for (int i = 0; i < MAP_SIZE; i++) 38 | { 39 | for (int j = 0; j < MAP_SIZE; j++) 40 | { 41 | if (map[j][i].isVerticalDoor) 42 | Console.Write("-"); 43 | else if (map[j][i].isHorizontalDoor) 44 | Console.Write("|"); 45 | else if (map[j][i].isWall) 46 | Console.Write("#"); 47 | else 48 | Console.Write(" "); 49 | } 50 | 51 | Console.WriteLine(); 52 | } 53 | 54 | Console.ReadKey(); 55 | } 56 | 57 | // Function To Initialize New Room Object 58 | public Room MakeRoom(int x, int y, int width, int height) 59 | { 60 | Room room = new Room(); 61 | room.x = x; 62 | room.y = y; 63 | room.width = width; 64 | room.height = height; 65 | room.leftRoom = room.rightRoom = null; 66 | 67 | return room; 68 | } 69 | 70 | // Function To Split The Room Object Into 2 New Sub Room Object 71 | public void SplitRoom(Room room) 72 | { 73 | // For Door X Coordinate or Door Y Coordinate 74 | int door; 75 | 76 | // For Door X Coordinate or Door Y Coordinate 77 | int randomPoint; 78 | 79 | // Check If We Should Split The Room Horizontally or Vertically 80 | if (room.width >= room.height && 81 | room.width > MIN_LENGTH * 2) 82 | { 83 | do 84 | { 85 | // Split Randomly 86 | randomPoint = new Random().Next() % room.width; 87 | } while (randomPoint < MIN_LENGTH || // Validating That Random Point Is Not Smaller Than Minimum Length 88 | room.width - randomPoint < MIN_LENGTH || // Validating That The New Sub Room Is Not Smaller Than Minimum Length 89 | map[room.y][room.x + randomPoint - 1].isHorizontalDoor || // Validating That There Is No Door At The End Of The Newly Generated Wall 90 | map[room.y + room.height - 1][room.x + randomPoint - 1].isHorizontalDoor 91 | ); 92 | 93 | // Generate Door at Random Point 94 | do 95 | { 96 | door = new Random().Next() % room.height; 97 | } 98 | while (door % (room.height - 1) == 0); // Validating That Door Is Not At The End of The Newly Generated Wall 99 | 100 | // Place Door 101 | map[room.y + door][room.x + randomPoint - 1].isVerticalDoor = true; 102 | 103 | // Generate The New Room To Left Sub Room and Split It 104 | room.leftRoom = MakeRoom(room.x, room.y, randomPoint, room.height); 105 | SplitRoom(room.leftRoom); 106 | 107 | // Generate The New Room To Right Sub Room and Split It 108 | room.rightRoom = MakeRoom(room.x + randomPoint - 1, room.y, room.width - randomPoint + 1, room.height); 109 | SplitRoom(room.rightRoom); 110 | } 111 | else if (room.height > MIN_LENGTH * 2) 112 | { 113 | do 114 | { 115 | // Split Randomly 116 | randomPoint = new Random().Next() % room.height; 117 | } while (randomPoint < MIN_LENGTH || // Validating That Random Point Is Not Smaller Than Minimum Length 118 | room.height - randomPoint < MIN_LENGTH || // Validating That The New Sub Room Is Not Smaller Than Minimum Length 119 | map[room.y + randomPoint - 1][room.x].isVerticalDoor || // Validating That There Is No Door At The End Of The Newly Generated Wall 120 | map[room.y + randomPoint - 1][room.x + room.width - 1].isVerticalDoor 121 | ); 122 | 123 | // Generate Door at Random Point 124 | do 125 | { 126 | door = new Random().Next() % room.width; 127 | } 128 | while (door % (room.width - 1) == 0); // Validating That Door Is Not At The End of The Newly Generated Wall 129 | 130 | // Place Door 131 | map[room.y + randomPoint - 1][room.x + door].isHorizontalDoor = true; 132 | 133 | // Generate The New Room To Left Sub Room 134 | room.leftRoom = MakeRoom(room.x, room.y, room.width, randomPoint); 135 | 136 | // Generate The New Room To Right Sub Room 137 | room.rightRoom = MakeRoom(room.x, room.y + randomPoint - 1, room.width, room.height - randomPoint + 1); 138 | 139 | // Split The Room 140 | SplitRoom(room.leftRoom); 141 | SplitRoom(room.rightRoom); 142 | } 143 | } 144 | 145 | public void MakeWall(Room room) 146 | { 147 | if (room != null) 148 | { 149 | for (int i = 0; i < room.height; i++) 150 | { 151 | for (int j = 0; j < room.width; j++) 152 | { 153 | if (i % (room.height - 1) == 0 || 154 | j % (room.width - 1) == 0) 155 | { 156 | map[i + room.y][j + room.x].isWall = true; 157 | } 158 | } 159 | } 160 | 161 | MakeWall(room.leftRoom); 162 | MakeWall(room.rightRoom); 163 | } 164 | } 165 | 166 | static void Main(string[] args) 167 | { 168 | new Program(); 169 | } 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /algorithms/maze-generator/binary-space-partitioning/Room.cs: -------------------------------------------------------------------------------- 1 | namespace BinarySpacePartitioning 2 | { 3 | public class Room 4 | { 5 | public int x, 6 | y, 7 | width, 8 | height; 9 | 10 | public Room leftRoom, 11 | rightRoom; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/maze-generator/binary-space-partitioning/Tile.cs: -------------------------------------------------------------------------------- 1 | namespace BinarySpacePartitioning 2 | { 3 | public class Tile 4 | { 5 | // There's Always A Status If The Tile is A Wall, Just A Tile, or A Door 6 | public bool isWall, 7 | isHorizontalDoor, 8 | isVerticalDoor; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /algorithms/searches/GenericBinarySearchTree.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 Datastructures.Trees 8 | { 9 | //for every node, all nodes in the right subtree have a smaller key and all nodes in the left subtree have a key greater or equal. 10 | public class BinarySearchTree where Tkey : IComparable 11 | { 12 | private Random random; 13 | protected BinaryKeyValueNode Root { get; set; } 14 | public int Count { get; protected set; } 15 | 16 | public BinarySearchTree() 17 | { 18 | Root = null; 19 | random = new Random(1); 20 | Count = 0; 21 | } 22 | 23 | public Tvalue FindFirst(Tkey key) 24 | { 25 | return FindNode(key).Value; 26 | } 27 | 28 | public Tvalue FindFirstOrDefault(Tkey key) 29 | { 30 | var node=FindNode(key, false); 31 | return node == null ? default(Tvalue) : node.Value; 32 | } 33 | 34 | public void DeleteFirst(Tkey key) 35 | { 36 | DeleteNode(FindNode(key)); 37 | } 38 | 39 | public void Insert(Tkey key, Tvalue value) 40 | { 41 | BinaryKeyValueNode parent = null; 42 | BinaryKeyValueNode current = Root; 43 | int compare = 0; 44 | while (current != null) 45 | { 46 | parent = current; 47 | compare = current.Key.CompareTo(key); 48 | current = compare < 0 ? current.RightChild : current.LeftChild; 49 | } 50 | BinaryKeyValueNode newNode = new BinaryKeyValueNode(key, value); 51 | if (parent != null) 52 | if (compare < 0) 53 | parent.RightChild = newNode; 54 | else 55 | parent.LeftChild = newNode; 56 | else 57 | Root = newNode; 58 | newNode.Parent = parent; 59 | Count++; 60 | } 61 | 62 | public IEnumerable TraverseTree(DepthFirstTraversalMethod method) 63 | { 64 | return TraverseNode(Root, method); 65 | } 66 | 67 | protected IEnumerable TraverseNode(BinaryKeyValueNode node, DepthFirstTraversalMethod method) 68 | { 69 | IEnumerable TraverseLeft = node.LeftChild == null ? new Tvalue[0] : TraverseNode(node.LeftChild, method), 70 | TraverseRight = node.RightChild == null ? new Tvalue[0] : TraverseNode(node.RightChild, method), 71 | Self = new Tvalue[1] { node.Value }; 72 | switch(method) 73 | { 74 | case DepthFirstTraversalMethod.PreOrder: 75 | return Self.Concat(TraverseLeft).Concat(TraverseRight); 76 | case DepthFirstTraversalMethod.InOrder: 77 | return TraverseLeft.Concat(Self).Concat(TraverseRight); 78 | case DepthFirstTraversalMethod.PostOrder: 79 | return TraverseLeft.Concat(TraverseRight).Concat(Self); 80 | default: 81 | throw new ArgumentException(); 82 | } 83 | } 84 | 85 | protected BinaryKeyValueNode FindNode(Tkey key, bool ExceptionIfKeyNotFound = true) 86 | { 87 | BinaryKeyValueNode current = Root; 88 | while (current != null) 89 | { 90 | int compare = current.Key.CompareTo(key); 91 | if (compare == 0) 92 | return current; 93 | if (compare < 0) 94 | current = current.RightChild; 95 | else 96 | current = current.LeftChild; 97 | } 98 | if (ExceptionIfKeyNotFound) 99 | throw new KeyNotFoundException(); 100 | else 101 | return null; 102 | } 103 | 104 | protected void DeleteNode(BinaryKeyValueNode node) 105 | { 106 | if (node == null) 107 | throw new ArgumentNullException(); 108 | if (node.LeftChild != null && node.RightChild != null) //2 childs 109 | { 110 | BinaryKeyValueNode replaceBy = random.NextDouble() > .5 ? InOrderSuccesor(node) : InOrderPredecessor(node); 111 | DeleteNode(replaceBy); 112 | node.Value = replaceBy.Value; 113 | node.Key = replaceBy.Key; 114 | } 115 | else //1 or less childs 116 | { 117 | var child = node.LeftChild == null ? node.RightChild : node.LeftChild; 118 | if (node.Parent.RightChild == node) 119 | node.Parent.RightChild = child; 120 | else 121 | node.Parent.LeftChild = child; 122 | } 123 | Count--; 124 | } 125 | 126 | protected BinaryKeyValueNode InOrderSuccesor(BinaryKeyValueNode node) 127 | { 128 | BinaryKeyValueNode succesor = node.RightChild; 129 | while (succesor.LeftChild != null) 130 | succesor = succesor.LeftChild; 131 | return succesor; 132 | } 133 | 134 | protected BinaryKeyValueNode InOrderPredecessor(BinaryKeyValueNode node) 135 | { 136 | BinaryKeyValueNode succesor = node.LeftChild; 137 | while (succesor.RightChild != null) 138 | succesor = succesor.RightChild; 139 | return succesor; 140 | } 141 | 142 | protected class BinaryKeyValueNode where Tkey : IComparable 143 | { 144 | public Tkey Key { get; protected internal set; } 145 | public Tvalue Value { get; protected internal set; } 146 | public BinaryKeyValueNode Parent { get; protected internal set; } 147 | public BinaryKeyValueNode LeftChild { get; protected internal set; } 148 | public BinaryKeyValueNode RightChild { get; protected internal set; } 149 | public BinaryKeyValueNode(Tkey key, Tvalue value) 150 | { 151 | Value = value; 152 | Key = key; 153 | } 154 | } 155 | 156 | public enum DepthFirstTraversalMethod 157 | { 158 | PreOrder, 159 | InOrder, 160 | PostOrder 161 | } 162 | } 163 | } -------------------------------------------------------------------------------- /algorithms/searches/InterpolationSearch.cs: -------------------------------------------------------------------------------- 1 | /* Interpolation Search C# - Created by: Ehsan Mohammadi 2 | 3 | Algorithm: 4 | Step1: In a loop, calculate the value of "middle" using the probe position formula. 5 | Step2: If it is a match, return the index of the item, and exit. 6 | Step3: If the item is less than list[middle], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array. 7 | Step4: Repeat until a match is found or the sub-array reduces to 0. 8 | */ 9 | 10 | using System; 11 | using System.Collections.Generic; 12 | 13 | namespace InterpolationSearch 14 | { 15 | class InterpolationSearch 16 | { 17 | public static int InterpSearch(List list, int data) 18 | { 19 | // Initialize 20 | int low = 0, high = list.Count - 1, middle; 21 | int index = -1; 22 | 23 | int denominator; 24 | 25 | if (list[low] <= data && list[high] >= data) 26 | { 27 | while (low <= high && index == -1) 28 | { 29 | denominator = list[high] - list[low]; 30 | if (denominator == 0) 31 | middle = low; 32 | else 33 | middle = low + (int)(((data - list[low])*(high - low))/denominator); 34 | 35 | if (data == list[middle]) 36 | index = middle; 37 | else if (data < list[middle]) 38 | high = middle - 1; 39 | else 40 | low = middle + 1; 41 | } 42 | } 43 | 44 | return index; 45 | } 46 | 47 | static void Main(string[] args) 48 | { 49 | List list = new List { 2, 4, 5, 8, 11 }; 50 | int index = InterpSearch(list, 8); 51 | 52 | if(index != -1) 53 | Console.WriteLine("Item found at index " + index); 54 | else 55 | Console.WriteLine("Item not found"); 56 | 57 | Console.ReadKey(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /algorithms/searches/binarysearch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class BS 4 | { 5 | static int BinarySearch(int[] data, int key) 6 | { 7 | int min = 0; 8 | int N = data.Length; 9 | int max = N - 1; 10 | do 11 | { 12 | int mid = (min + max) / 2; 13 | if (key > data[mid]) 14 | min = mid + 1; 15 | else 16 | max = mid - 1; 17 | if (data[mid] == key) 18 | return mid; 19 | } while (min <= max); 20 | 21 | return -1; 22 | } 23 | 24 | public static void Main() 25 | { 26 | int[] values = { 1, 6, 12, 18, 36, 71 }; 27 | int search = 36; 28 | int index = BinarySearch(values, search); 29 | Console.WriteLine((index > 0) ? $"Item {search} found at position {index}" : $"Item {search} not found"); 30 | 31 | search = 3; 32 | index = BinarySearch(values, search); 33 | Console.WriteLine((index > 0) ? $"Item {search} found at position {index}" : $"Item {search} not found"); 34 | Console.ReadKey(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /algorithms/searches/jumpsearch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | public class JS 6 | { 7 | public class JumpSearch 8 | { 9 | private readonly List _items; 10 | public JumpSearch(List items) 11 | { 12 | items.Sort(); 13 | this._items = items; 14 | } 15 | 16 | public int Search(int itemToFind) 17 | { 18 | var listLength = _items.Count; 19 | 20 | var jumpStep = (int)Math.Floor(Math.Sqrt(listLength)); 21 | 22 | var prevStep = 0; 23 | while (_items.ElementAt(Math.Min(jumpStep, listLength) - 1) < itemToFind) 24 | { 25 | prevStep = jumpStep; 26 | jumpStep += (int)Math.Floor(Math.Sqrt(listLength)); 27 | if (prevStep >= listLength) 28 | return -1; 29 | } 30 | 31 | while (_items.ElementAt(prevStep) < itemToFind) 32 | { 33 | prevStep++; 34 | if (prevStep == Math.Min(jumpStep, listLength)) 35 | return -1; 36 | } 37 | 38 | if (_items.ElementAt(prevStep) == itemToFind) 39 | return prevStep; 40 | 41 | return -1; 42 | } 43 | } 44 | 45 | public static void Main() 46 | { 47 | var items = new List() { 12, 8, 4, 30, 6, 88 }; 48 | JumpSearch jsearch = new JumpSearch(items); 49 | 50 | var itemToFind = 4; 51 | int index = jsearch.Search(itemToFind); 52 | Console.WriteLine((index >= 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found"); 53 | 54 | itemToFind = 13; 55 | index = jsearch.Search(itemToFind); 56 | Console.WriteLine((index > 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found"); 57 | 58 | itemToFind = 30; 59 | index = jsearch.Search(itemToFind); 60 | Console.WriteLine((index > 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found"); 61 | 62 | Console.ReadLine(); 63 | } 64 | } -------------------------------------------------------------------------------- /algorithms/searches/linearsearch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | class LS 7 | { 8 | public class LinearSearch 9 | { 10 | private readonly List _list; 11 | public LinearSearch(List list) 12 | { 13 | this._list = list; 14 | } 15 | 16 | public int Search(int itemToFind) 17 | { 18 | for (int i = 0; i < _list.Count(); i++) 19 | { 20 | int currentValue = _list.ElementAt(i); 21 | if (currentValue == itemToFind) 22 | { 23 | return i; 24 | } 25 | } 26 | return -1; 27 | } 28 | } 29 | static void Main(string[] args) 30 | { 31 | var items = new List() { 12, 8, 4, 30, 6, 88 }; 32 | LinearSearch jsearch = new LinearSearch(items); 33 | 34 | var itemToFind = 4; 35 | int index = jsearch.Search(itemToFind); 36 | Console.WriteLine((index >= 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found"); 37 | 38 | itemToFind = 13; 39 | index = jsearch.Search(itemToFind); 40 | Console.WriteLine((index > 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found"); 41 | 42 | itemToFind = 30; 43 | index = jsearch.Search(itemToFind); 44 | Console.WriteLine((index > 0) ? $"Item {itemToFind} found at position {index}" : $"Item {itemToFind} not found"); 45 | 46 | Console.ReadLine(); 47 | } 48 | } -------------------------------------------------------------------------------- /algorithms/searches/ternarysearch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class TS 4 | { 5 | static int TernarySearch(int[] data, int left, int right, int key) 6 | { 7 | if (right >= left) 8 | { 9 | int mid1 = left + (right - left) / 3; 10 | int mid2 = right - (right - left) / 3; 11 | 12 | if (data[mid1] == key) 13 | return mid1; 14 | 15 | if (data[mid2] == key) 16 | return mid2; 17 | 18 | if (key < data[mid1]) 19 | return TernarySearch(data, left, mid1 - 1, key); 20 | else if (key > data[mid2]) 21 | return TernarySearch(data, mid2 + 1, right, key); 22 | else 23 | return TernarySearch(data, mid1 + 1, mid2 - 1, key); 24 | } 25 | return -1; 26 | } 27 | 28 | public static void Main() 29 | { 30 | int[] values = { 2, 3, 5, 6, 8, 9, 12, 13, 14 }; 31 | int search = 6; 32 | int index = TernarySearch(values, 0, values.Length - 1, search); 33 | Console.WriteLine((index > 0) ? $"Item {search} found at position {index}" : $"Item {search} not found"); 34 | 35 | search = 10; 36 | index = TernarySearch(values, 0, values.Length - 1, search); 37 | Console.WriteLine((index > 0) ? $"Item {search} found at position {index}" : $"Item {search} not found"); 38 | Console.ReadKey(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /algorithms/sorting/bitonicsort.cs: -------------------------------------------------------------------------------- 1 | namespace Algorithms 2 | { 3 | /// 4 | /// This is a C# port of the bitonicsort algorithm 5 | /// described at https://www.geeksforgeeks.org/bitonic-sort/ 6 | /// This is merely a demonstration of BitonicSort and not an optimised 7 | /// implementation that should be used in production environments. 8 | /// 9 | public class bitonicsort 10 | { 11 | private static void CompAndSwap(int[] array, int i, int j, int dir) 12 | { 13 | if ((dir != 0) == (array[i] > array[j])) 14 | { 15 | int temp = array[i]; 16 | array[i] = array[j]; 17 | array[j] = temp; 18 | } 19 | } 20 | 21 | private static void BitonicMerge(int[] array, int low, int count, int dir) 22 | { 23 | if (count > 1) 24 | { 25 | int k = count / 2; 26 | for (int i = low; i < low + k; i++) 27 | { 28 | CompAndSwap(array, i, i + k, dir); 29 | } 30 | 31 | BitonicMerge(array, low, k, dir); 32 | BitonicMerge(array, low + k, k, dir); 33 | } 34 | } 35 | 36 | private static void Sort(int[] array, int low, int count, int dir) 37 | { 38 | if (count > 1) 39 | { 40 | int k = count / 2; 41 | 42 | Sort(array, low, k, 1); 43 | 44 | Sort(array, low + k, k, 0); 45 | 46 | BitonicMerge(array, low, count, dir); 47 | } 48 | } 49 | 50 | // Only works when input size is of a power of 2 51 | public static void BitonicSort(int[] array) 52 | { 53 | Sort(array, 0, array.Length, 1); 54 | } 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /algorithms/sorting/bogosort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace bogosort 4 | { 5 | public class BogoSort 6 | { 7 | 8 | private static void Swap(int[] arr, int left, int right) 9 | { 10 | int leftOrig = arr[left]; 11 | arr[left] = arr[right]; 12 | arr[right] = leftOrig; 13 | } 14 | 15 | private static bool IsSorted(int[] arr) 16 | { 17 | for (int i = 0; i < arr.Length - 1; i++) 18 | { 19 | if (arr[i] > arr[i + 1]) return false; 20 | } 21 | return true; 22 | } 23 | 24 | private static void Shuffle(int[] arr) 25 | { 26 | for (int i = 0; i < arr.Length; i++) 27 | { 28 | Random swapper = new Random(); 29 | 30 | int swap = swapper.Next(arr.Length); 31 | Swap(arr, i, swap); 32 | } 33 | } 34 | 35 | public static void Sort(int[] arr) 36 | { 37 | while (!IsSorted(arr)) 38 | { 39 | Shuffle(arr); 40 | } 41 | } 42 | 43 | 44 | /* Prints the array */ 45 | static void printArray(int[] arr) 46 | { 47 | int n = arr.Length; 48 | for (int i = 0; i < n; ++i) 49 | Console.Write(arr[i] + " "); 50 | Console.WriteLine(); 51 | } 52 | 53 | // Driver method 54 | public static void Main() 55 | { 56 | int[] arr = { 64, 34, 25 }; 57 | Sort(arr); 58 | Console.WriteLine("Sorted array"); 59 | printArray(arr); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /algorithms/sorting/bubblesort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class BubbleSort { 4 | static void bubbleSort(int []arr) { 5 | int n = arr.Length; 6 | 7 | for (int i = 0; i < n - 1; i++) 8 | for (int j = 0; j < n - i - 1; j++) 9 | if (arr[j] > arr[j + 1]) { 10 | // swap temp and arr[i] 11 | int temp = arr[j]; 12 | arr[j] = arr[j + 1]; 13 | arr[j + 1] = temp; 14 | } 15 | } 16 | 17 | /* Prints the array */ 18 | static void printArray(int []arr) { 19 | int n = arr.Length; 20 | for (int i = 0; i < n; ++i) 21 | Console.Write(arr[i] + " "); 22 | Console.WriteLine(); 23 | } 24 | 25 | /* Driver method */ 26 | public static void Main() { 27 | int n, i; 28 | Console.WriteLine("Enter your Array length"); 29 | n = Console.ReadLine(); 30 | 31 | int []arr=new arr[n]; 32 | for(i=0;i<=n;i++) { 33 | Console.WriteLine("Enter your elements one by one"); 34 | [i]arr=Console.ReadLine(); 35 | } 36 | 37 | bubbleSort(arr); 38 | Console.WriteLine("Sorted array"); 39 | printArray(arr); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /algorithms/sorting/bucketsort.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 Sorting_Algorithms_In_C_Sharp 8 | { 9 | public static class BucketSort 10 | { 11 | public static List BucketSort1(params int[] x) 12 | { 13 | List result = new List(); 14 | int numOfBuckets = 10; 15 | 16 | List[] buckets = new List[numOfBuckets]; 17 | for (int i = 0; i < numOfBuckets; i++) 18 | buckets[i] = new List(); 19 | 20 | for (int i = 0; i < x.Length; i++) 21 | { 22 | int buckitChoice = (x[i] / numOfBuckets); 23 | buckets[buckitChoice].Add(x[i]); 24 | } 25 | 26 | for (int i = 0; i < numOfBuckets; i++) 27 | { 28 | int [] temp = BubbleSortList(buckets[i]); 29 | result.AddRange(temp); 30 | } 31 | return result; 32 | } 33 | 34 | public static int[] BubbleSortList(List input) 35 | { 36 | for (int i = 0; i < input.Count; i++) 37 | { 38 | for (int j = 0; j < input.Count; j++) 39 | { 40 | if (input[i] < input[j]) 41 | { 42 | int temp = input[i]; 43 | input[i] = input[j]; 44 | input[j] = temp; 45 | } 46 | } 47 | } 48 | return input.ToArray(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /algorithms/sorting/cocktailsort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class C_S 4 | { 5 | public static void cocktailSort(int[] arr) 6 | { 7 | int n = arr.Length; 8 | for (int i = n - 1; i > 0; i--) 9 | { 10 | bool swapped = false; 11 | 12 | for (int j = i; j > 0; j--) 13 | { 14 | if (arr[j] < arr[j - 1]) 15 | { 16 | // swap temp and arr[j] 17 | int temp = arr[j]; 18 | arr[j] = arr[j - 1]; 19 | arr[j - 1] = temp; 20 | swapped = true; 21 | } 22 | } 23 | 24 | for (int j = 0; j < i; j++) 25 | { 26 | if (arr[j] > arr[j + 1]) 27 | { 28 | // swap temp and arr[j] 29 | int temp = arr[j]; 30 | arr[j] = arr[j + 1]; 31 | arr[j + 1] = temp; 32 | swapped = true; 33 | } 34 | } 35 | 36 | if (!swapped) 37 | break; 38 | } 39 | } 40 | 41 | /* Prints the array */ 42 | static void printArray(int[] arr) 43 | { 44 | int n = arr.Length; 45 | for (int i = 0; i < n; ++i) 46 | Console.Write(arr[i] + " "); 47 | Console.WriteLine(); 48 | } 49 | 50 | // Driver method 51 | public static void Main(string[] args) 52 | { 53 | int[] arr = { 64, 34, 25, 12, 22, 11, 90 }; 54 | cocktailSort(arr); 55 | Console.WriteLine("Sorted array"); 56 | printArray(arr); 57 | Console.ReadKey(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /algorithms/sorting/countingsort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace countingsort 4 | { 5 | public class CountingSort 6 | { 7 | 8 | static public void Sort(int[] unsorted) 9 | { 10 | int[] sorted = new int[unsorted.Length]; 11 | 12 | int min = unsorted[0]; 13 | int max = unsorted[0]; 14 | 15 | foreach (int val in unsorted) 16 | { 17 | if (val < min) min = val; 18 | if (val > max) max = val; 19 | } 20 | 21 | int[] counts = new int[max - min + 1]; 22 | 23 | for (int i = 0; i < unsorted.Length; i++) 24 | { 25 | counts[unsorted[i] - min] += 1; 26 | } 27 | 28 | counts[0]--; 29 | for (int i = 1; i < counts.Length; i++) 30 | { 31 | counts[i] += counts[i - 1]; 32 | } 33 | 34 | for (int i = unsorted.Length - 1; i >= 0; i--) 35 | { 36 | sorted[counts[unsorted[i] - min]--] = unsorted[i]; 37 | } 38 | 39 | for (int i = 0; i < unsorted.Length; i++) unsorted[i] = sorted[i]; 40 | } 41 | 42 | /* Prints the array */ 43 | static void printArray(int[] arr) 44 | { 45 | int n = arr.Length; 46 | for (int i = 0; i < n; ++i) 47 | Console.Write(arr[i] + " "); 48 | Console.WriteLine(); 49 | } 50 | 51 | // Driver method 52 | public static void Main() 53 | { 54 | int[] arr = { 64, 34, 25, 12, 22, 11, 90 }; 55 | Sort(arr); 56 | Console.WriteLine("Sorted array"); 57 | printArray(arr); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /algorithms/sorting/heapsort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class GFG { 4 | static void heapSort (int[] arr) { 5 | heapSize = arr.Length - 1; 6 | for (int i = heapSize / 2; i >= 0; i--) { 7 | Heapify (arr, i); 8 | } 9 | for (int i = arr.Length - 1; i >= 0; i--) { 10 | Swap (arr, 0, i); 11 | heapSize--; 12 | Heapify (arr, 0); 13 | } 14 | 15 | void Swap (int[] arr, int x, int y) //function to swap elements 16 | { 17 | int temp = arr[x]; 18 | arr[x] = arr[y]; 19 | arr[y] = temp; 20 | } 21 | void Heapify (int[] arr, int index) { 22 | int left = 2 * index; 23 | int right = 2 * index + 1; 24 | int largest = index; 25 | 26 | if (left <= heapSize && arr[left] > arr[index]) { 27 | largest = left; 28 | } 29 | 30 | if (right <= heapSize && arr[right] > arr[largest]) { 31 | largest = right; 32 | } 33 | 34 | if (largest != index) { 35 | Swap (arr, index, largest); 36 | Heapify (arr, largest); 37 | } 38 | } 39 | } 40 | 41 | /* Prints the array */ 42 | static void printArray (int[] arr) { 43 | int n = arr.Length; 44 | for (int i = 0; i < n; ++i) 45 | Console.Write (arr[i] + " "); 46 | Console.WriteLine (); 47 | } 48 | 49 | // Driver method 50 | public static void Main () { 51 | int[] arr = { 64, 34, 25, 12, 22, 11, 90 }; 52 | heapSort (arr); 53 | Console.WriteLine ("Sorted array"); 54 | printArray (arr); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /algorithms/sorting/insertionsort.cs: -------------------------------------------------------------------------------- 1 | namespace Algorithms 2 | { 3 | class insertionsort 4 | { 5 | public static void insertionSort(int[] values) 6 | { 7 | int val = 0; 8 | 9 | for(int i = 1; i < values.Length; ++i) 10 | { 11 | val = values[i]; 12 | int j; 13 | for (j = i - 1; j >= 0 && values[j] > val; --j) 14 | { 15 | values[j + 1] = values[j]; 16 | } 17 | values[j + 1] = val; 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/sorting/mergesort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CsharpAlgo 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Console.WriteLine("Enter the Size of Array"); 10 | int size = int.Parse(Console.ReadLine()); //Read array size. 11 | Console.WriteLine("Enter the Array elements"); 12 | 13 | string[] usrInput = Console.ReadLine().Split(' '); //Read user input. 14 | 15 | int[] inputArray = new int[size]; 16 | 17 | for (int i = 0; i < size; i++) 18 | { 19 | inputArray[i] = int.Parse(usrInput[i]); //Assign user input to array. 20 | } 21 | 22 | MergeSort(inputArray, 0, size - 1); 23 | 24 | Console.WriteLine("The sorted array after applying Merge Sort "); 25 | 26 | for (int k = 0; k < inputArray.Length; k++) 27 | { 28 | Console.Write(inputArray[k] + " "); //Print the sorted array to console. 29 | } 30 | Console.ReadLine(); 31 | } 32 | 33 | public static void MergeSort(int[] arr, int start, int end) 34 | { 35 | if (start < end) 36 | { 37 | int mid = (start + end) / 2; // divide the current array in 2 subarrays. 38 | MergeSort(arr, start, mid); // sort the first part of array. 39 | MergeSort(arr, mid + 1, end); // sort the second part of array. 40 | 41 | // merge the subarrays by comparing elements in them. 42 | Merge(arr, start, mid, end); 43 | } 44 | } 45 | 46 | public static void Merge(int[] arr, int start, int mid, int end) 47 | { 48 | int p = start, q = mid + 1; 49 | 50 | int[] tempArr = new int[end - start + 1]; 51 | int k = 0; 52 | 53 | for (int i = start; i <= end; i++) 54 | { 55 | if (p > mid) //checks if first part comes to an end or not . 56 | tempArr[k++] = arr[q++]; 57 | 58 | else if (q > end) //checks if second part comes to an end or not 59 | tempArr[k++] = arr[p++]; 60 | 61 | else if (arr[p] < arr[q]) //compare elements from both parts. 62 | tempArr[k++] = arr[p++]; 63 | 64 | else 65 | tempArr[k++] = arr[q++]; 66 | } 67 | for (int j = 0; j < k; j++) 68 | { 69 | //Now the original array has elements in sorted manner including both parts. 70 | arr[start++] = tempArr[j]; 71 | } 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /algorithms/sorting/quicksort.cs: -------------------------------------------------------------------------------- 1 | namespace Algorithms 2 | { 3 | class quicksort 4 | { 5 | public static void quickSort(int[] values) 6 | { 7 | quickSort(values, 0, values.Length - 1); 8 | } 9 | 10 | private static void quickSort(int[] values, int low, int high) 11 | { 12 | if (low < high) 13 | { 14 | int pi = partition(values, low, high); 15 | 16 | quickSort(values, low, pi - 1); 17 | quickSort(values, pi + 1, high); 18 | } 19 | } 20 | 21 | public static int partition(int[] values, int low, int high) 22 | { 23 | int pivot = values[high]; 24 | 25 | int i = (low - 1); 26 | 27 | for (int j = low; j <= high - 1; j++) 28 | { 29 | if (values[j] <= pivot) 30 | { 31 | i++; 32 | int temp = values[i]; 33 | values[i] = values[j]; 34 | values[j] = temp; 35 | } 36 | } 37 | int swap = values[i + 1]; 38 | values[i + 1] = values[high]; 39 | values[high] = swap; 40 | return (i + 1); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /algorithms/sorting/radixsort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Algorithms 4 | { 5 | class radixsort 6 | { 7 | // https://www.w3resource.com/csharp-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-10.php 8 | public static void radixSort(int[] values) 9 | { 10 | int i, j; 11 | int[] temp = new int[values.Length]; 12 | for (int shift = 31; shift > -1; --shift) 13 | { 14 | j = 0; 15 | for (i = 0; i < values.Length; ++i) 16 | { 17 | bool move = (values[i] << shift) >= 0; 18 | if (shift == 0 ? !move : move) 19 | values[i - j] = values[i]; 20 | else 21 | temp[j++] = values[i]; 22 | } 23 | Array.Copy(temp, 0, values, values.Length - j, j); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /algorithms/sorting/selectionsort.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace Algorithms 3 | { 4 | class selectionsort 5 | { 6 | public static void selectionSort(int[] values) 7 | { 8 | for(int i = 0; i < values.Length; ++i) 9 | { 10 | int minIndex = i; 11 | for(int j = i + 1; j < values.Length; ++j) 12 | { 13 | if(values[j] < values[minIndex]) 14 | { 15 | minIndex = j; 16 | } 17 | } 18 | int temp = values[i]; 19 | values[i] = values[minIndex]; 20 | values[minIndex] = temp; 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /algorithms/sorting/shellsort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace csharp.sorting 4 | { 5 | class Program 6 | { 7 | 8 | static void shellSort(int[] array) 9 | { 10 | int i, j, inc, temp; 11 | var array_size = array.Length; 12 | inc = 3; 13 | 14 | while (inc > 0) 15 | { 16 | for (i = 0; i < array_size; i++) 17 | { 18 | j = i; 19 | temp = array[i]; 20 | while ((j >= inc) && (array[j - inc] > temp)) 21 | { 22 | array[j] = array[j - inc]; 23 | j = j - inc; 24 | } 25 | array[j] = temp; 26 | } 27 | if (inc / 2 != 0) 28 | inc = inc / 2; 29 | else if (inc == 1) 30 | inc = 0; 31 | else 32 | inc = 1; 33 | } 34 | } 35 | static void printArray(int[] arr) 36 | { 37 | int n = arr.Length; 38 | for (int i = 0; i < n; ++i) 39 | Console.Write(arr[i] + " "); 40 | Console.WriteLine(); 41 | } 42 | 43 | public static void Main() 44 | { 45 | int[] arr = { 25, 17, 49, 1, 195, 58 }; 46 | heapSort(arr); 47 | Console.WriteLine("Sorted array"); 48 | printArray(arr); 49 | } 50 | 51 | } 52 | } -------------------------------------------------------------------------------- /algorithms/sorting/timsort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Algorithms 3 | { 4 | /// 5 | /// This is a C# port of the timsort algorithm 6 | /// described at https://www.geeksforgeeks.org/timsort/ 7 | /// This is merely a demonstration of TimSort and not an optimised 8 | /// implementation that should be used in production environments. 9 | /// 10 | public class timsort 11 | { 12 | private const int RUN = 32; 13 | 14 | // TimSort uses a combination of InsertionSort and MergeSort 15 | public static void TimSort(int[] array) 16 | { 17 | int n = array.Length; 18 | for (int i = 0; i < n; i += RUN) 19 | { 20 | InsertionSort(array, i, Math.Min((i + 31), (n - 1))); 21 | } 22 | 23 | for (int size = RUN; size < n; size = 2 * size) 24 | { 25 | for (int left = 0; left < n; left += 2 * size) 26 | { 27 | int mid = left + size - 1; 28 | int right = Math.Min((left + 2 * size - 1), (n - 1)); 29 | 30 | MergeSort(array, left, mid, right); 31 | } 32 | } 33 | } 34 | 35 | // Insertion Sort Implementation 36 | private static void InsertionSort(int[] array, int left, int right) 37 | { 38 | for (int i = left + 1; i <= right; i++) 39 | { 40 | int temp = array[i]; 41 | int j = i - 1; 42 | while (array[j] > temp && j >= left) 43 | { 44 | array[j + 1] = array[j]; 45 | j--; 46 | } 47 | array[j + 1] = temp; 48 | } 49 | } 50 | 51 | // Merge sort implementation 52 | private static void MergeSort(int[] array, int l, int m, int r) 53 | { 54 | int len1 = m - l + 1, len2 = r - m; 55 | 56 | int[] left = new int[len1], right = new int[len2]; 57 | 58 | int i = 0; 59 | 60 | for (i = 0; i < len1; i++) 61 | { 62 | left[i] = array[l + i]; 63 | } 64 | 65 | for (i = 0; i < len2; i++) 66 | { 67 | right[i] = array[m + 1 + i]; 68 | } 69 | 70 | i = 0; 71 | int j = 0; 72 | int k = l; 73 | 74 | while (i < len1 && j < len2) 75 | { 76 | if (left[i] <= right[j]) 77 | { 78 | array[k] = left[i]; 79 | i++; 80 | } 81 | else 82 | { 83 | array[k] = right[j]; 84 | j++; 85 | } 86 | k++; 87 | } 88 | 89 | while (i < len1) 90 | { 91 | array[k] = left[i]; 92 | k++; 93 | i++; 94 | } 95 | 96 | while (j < len2) 97 | { 98 | array[k] = right[j]; 99 | k++; 100 | j++; 101 | } 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /algorithms/strings/FilterIntegers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text.RegularExpressions; 4 | 5 | 6 | public class FilterIntegers 7 | { 8 | public static int[] FilterIntsFromString(string input) 9 | { 10 | List integerList = new List(); 11 | var match = Regex.Match(input, @"\d+"); 12 | while(match != null && !String.IsNullOrEmpty(match.Value)) 13 | { 14 | int newInt = int.Parse(match.Value); 15 | if(match.Index > 0 && input[match.Index-1] == '-') 16 | { 17 | newInt *= -1; 18 | } 19 | integerList.Add(newInt); 20 | match = match.NextMatch(); 21 | } 22 | return integerList.ToArray(); 23 | } 24 | 25 | static void PrintIntArray(int[] array) 26 | { 27 | Console.WriteLine("Integer Array: "); 28 | for(int i = 0; i < array.Length; i++) 29 | { 30 | Console.Write(array[i]); 31 | if(i < array.Length-1) 32 | { 33 | Console.Write(", "); 34 | } 35 | } 36 | Console.WriteLine(); 37 | } 38 | 39 | static void Main(string[] args) 40 | { 41 | PrintIntArray(FilterIntsFromString("-85HelloWorl65dTe5-6st")); 42 | PrintIntArray(FilterIntsFromString("-8Tea653218Test--5Piano000")); 43 | PrintIntArray(FilterIntsFromString("CakeNotTea")); 44 | PrintIntArray(FilterIntsFromString("Coffee9")); 45 | Console.ReadKey(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /algorithms/strings/Levenshtein.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Levenshtein 4 | { 5 | // The Levenshtein algorithm is a useful algorithm to determine how similar two strings are. 6 | // The Levenshtein Distance between two strings is essentially defined as the number of 7 | // changes (alterations, insertions or deletions) that need to be made to change one string 8 | // to the other. This is useful when implementing fuzzy logic searches such as book titles 9 | // or online store products where the users are likely to make typing errors which in a 10 | // direct string comparison would yield no results. 11 | 12 | public static int Distance(string source, string target) 13 | { 14 | int srcLen = source.Length; 15 | int trgLen = target.Length; 16 | int[,] matrix = new int[srcLen + 1, trgLen + 1]; 17 | 18 | // Check if either string is empty 19 | if (srcLen == 0) 20 | { 21 | return trgLen; 22 | } 23 | 24 | if (trgLen == 0) 25 | { 26 | return srcLen; 27 | } 28 | 29 | // Seed matrix 30 | for (int i = 0; i <= srcLen; matrix[i, 0] = i++) 31 | { 32 | } 33 | 34 | for (int j = 0; j <= trgLen; matrix[0, j] = j++) 35 | { 36 | } 37 | 38 | // Iterate over source 39 | for (int i = 1; i <= srcLen; i++) 40 | { 41 | // Iterate over target 42 | for (int j = 1; j <= trgLen; j++) 43 | { 44 | // Calculate cost 45 | int cost = (target[j - 1] == source[i - 1]) ? 0 : 1; 46 | 47 | // Populate matrix 48 | matrix[i, j] = Math.Min( 49 | Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1), 50 | matrix[i - 1, j - 1] + cost); 51 | } 52 | } 53 | 54 | // Return shortest distance 55 | return matrix[srcLen, trgLen]; 56 | } 57 | 58 | // Test 59 | static void Main(string[] args) 60 | { 61 | Console.WriteLine("Test Levenshtein:"); 62 | Console.WriteLine("Hello World -> Hello world :" + Levenshtein.Distance("Hello World", "Hello world")); 63 | Console.WriteLine("Gorilla -> Guerilla :" + Levenshtein.Distance("Gorilla", "Guerilla")); 64 | Console.WriteLine("foo -> bar :" + Levenshtein.Distance("foo", "bar")); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /algorithms/strings/RabinKarp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace Algorithms 3 | { 4 | /// 5 | /// This is a C# port of the RabinKarp algorithm 6 | /// described at https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/ 7 | /// This is merely a demonstration of RabinKarp and not an optimised 8 | /// implementation that should be used in production environments. 9 | /// 10 | public class RabinKarp 11 | { 12 | private static readonly int ALPHABET_SIZE = 256; 13 | 14 | public static void Search(string pattern, string text, int prime) 15 | { 16 | int M = pattern.Length; 17 | int N = text.Length; 18 | int patternHash = 0; 19 | int textHash = 0; 20 | int h = 1; 21 | 22 | for (int i = 0; i < M - 1; i++) 23 | { 24 | h = (h * ALPHABET_SIZE) % prime; 25 | } 26 | 27 | for (int i = 0; i < M; i++) 28 | { 29 | patternHash = (ALPHABET_SIZE * patternHash + pattern[i]) % prime; 30 | textHash = (ALPHABET_SIZE * textHash + text[i]) % prime; 31 | } 32 | 33 | for (int i = 0; i <= N - M; i++) 34 | { 35 | if (patternHash == textHash) 36 | { 37 | int j; 38 | for (j = 0; j < M; j++) 39 | { 40 | if (text[i + j] != pattern[j]) 41 | break; 42 | } 43 | 44 | if (j == M) 45 | { 46 | Console.WriteLine($"Pattern found at index {i}"); 47 | } 48 | } 49 | 50 | if (i < N - M) 51 | { 52 | textHash = (ALPHABET_SIZE * (textHash - text[i] * h) + text[i + M]) % prime; 53 | 54 | if (textHash < 0) 55 | { 56 | textHash = (textHash + prime); 57 | } 58 | } 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /algorithms/strings/StringReverse.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | namespace Algorithms 3 | { 4 | public class StringReverse 5 | { 6 | public static string Reverse(string text) 7 | { 8 | int textEnd = text.Length - 1; 9 | int iterateEnd = text.Length / 2; 10 | StringBuilder textCopy = new StringBuilder(text); 11 | for(int i = 0; i < iterateEnd; ++i) 12 | { 13 | char front = textCopy[i]; 14 | char back = textCopy[textEnd - i]; 15 | textCopy[i] = back; 16 | textCopy[textEnd - i] = front; 17 | } 18 | return textCopy.ToString(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/strings/palindrome.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Palindrome 4 | { 5 | static bool isPalindrome(string input) 6 | { 7 | if (input.Length == 0) return false; 8 | if (input.Length == 1) return true; 9 | 10 | for(int i = 0; i < input.Length/2; i++) 11 | { 12 | if(char.ToLower(input[i]) != char.ToLower(input[input.Length-1-i])) 13 | { 14 | return false; 15 | } 16 | } 17 | return true; 18 | } 19 | 20 | // Test 21 | static void Main(string[] args) 22 | { 23 | Console.WriteLine("Test Palindrome:"); 24 | Console.WriteLine("Fun :" + isPalindrome("Fun")); 25 | Console.WriteLine("Anna :" + isPalindrome("Anna")); 26 | Console.WriteLine("Anhna :" + isPalindrome("Anhna")); 27 | Console.WriteLine("Test :" + isPalindrome("Test")); 28 | Console.ReadKey(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /algorithms/strings/reversestring.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class ReverseString 4 | { 5 | static string ReverseString(string inputString){ 6 | char[] charArray = inputString.ToCharArray(); 7 | Array.Reverse( charArray ); 8 | return new string( charArray ); 9 | } 10 | } 11 | 12 | static void Main(string[] args){ 13 | Console.WriteLine("Test ReverseString:"); 14 | Console.WriteLine("Reverse of war is" + ReverseString(war)); 15 | Console.WriteLine("Reverse of bat is" + ReverseString(bat)); 16 | } -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 All Algorithms and its contributors (allalgorithms.com) 4 | Copyright (c) 2018 Carlos Abraham (abranhe.com) 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | We are accepting all pull requests. [Read More](https://github.com/AllAlgorithms/algorithms/issues/40) 2 | 3 |
4 |
5 |
6 |
7 |
8 | Algorithms Logo 9 |
10 |
11 |
12 |
13 |
14 |
15 | 16 |

17 | What is an algorithm?    18 | Contributing    19 | Stickers & T-Shirts 20 |

21 | 22 | 23 |

24 | 25 | Twitter 26 |     27 | 28 | Instagram 29 |     30 | 31 | Github 32 |     33 |

34 | 35 |
36 |

37 | Huge collection of All ▲lgorithms implemented in multiple languages 38 |

39 |
40 | 41 | 42 | 43 | 44 | 45 | 46 |
47 | 48 | ## See 49 | 50 | - [What is an algorithm](#what-is-an-algorithm) 51 | - [Contributing](https://github.com/AllAlgorithms/algorithms/blob/master/.github/contributing.md) 52 | - [Code of Conduct](https://github.com/AllAlgorithms/algorithms/blob/master/.github/code-of-conduct.md) 53 | - [Stickers and T-Shirts](https://www.redbubble.com/people/abranhe/works/34285088) 54 | - [Twitter](https://twitter.com/AllAlgorithms) 55 | - [Instagram](https://instagram.com/AllAlgorithms) 56 | - [Algorithms Categories](#categories) 57 | - [Maintainers](#maintainers) 58 | - [License](#license) 59 | 60 | 61 | ## What is an algorithm? 62 | 63 | Informally, an algorithm is any well-defined computational procedure that takes 64 | some value, or set of values, as input and produces some value, or set of values, as 65 | output. An algorithm is thus a sequence of computational steps that transform the 66 | input into the output. 67 | 68 | An algorithm should have three important characteristics to be considered valid: 69 | 70 | - **It should be finite**: If your algorithm never ends trying to solve the problem 71 | it was designed to solve then it is useless 72 | - **It should have well defined instructions**: Each step of the algorithm has to 73 | be precisely defined; the instructions should be unambiguously specified for each case. 74 | - **It should be effective**: The algorithm should solve the problem it was designed 75 | to solve. And it should be possible to demonstrate that the algorithm converges with 76 | just a paper and pencil. 77 | 78 | ## Categories 79 | 80 | > Structure of The All ▲lgoritms project 81 | 82 | - [Artificial Intelligence](#artificial-intelligence) 83 | - [Backtracking](#backtracking) 84 | - [Bit Manipulation](#bit-manipulation) 85 | - [Cellular Automaton](#cellular-automaton) 86 | - [Ciphers](#ciphers) 87 | - [Computational Geometry](#computational-geometry) 88 | - [Cryptography](#cryptography) 89 | - [Data Structures](#data-structures) 90 | - [Divide and conquer](#divide-and-conquer) 91 | - [Dynamic Programming](#dynamic-programming) 92 | - [Gaming Theory](#gaming-theory) 93 | - [Graphs](#graphs) 94 | - [Greedy Algorithms](#greedy-algorithms) 95 | - [Math](#math) 96 | - [Networking](#networking) 97 | - [Numerical Analysis](#numerical-analysis) 98 | - [Operating system](#operating-system) 99 | - [Randomized Algorithms](#randomized-algorithms) 100 | - [Searches](#searches) 101 | - [Selections Algorithms](#selections-algorithms) 102 | - [Sorting](#sorting) 103 | - [Strings](#strings) 104 | - [Online Challenges](#online-challenges) 105 | - [Others](#others) 106 | 107 | ## [Artificial Intelligence](artificial-intelligence) 108 | 109 | - [Density-based spatial clustering of applications with noise (DBSCAN Clustering)](https://allalgorithms.com/docs/dbscan) 110 | - [Interactive Self-Organizing Data Analysis Technique yAy! (ISODATA Clustering)](https://allalgorithms.com/docs/isodata) 111 | - [Linear Regression](https://allalgorithms.com/docs/linear-regression) 112 | - [Logistic Regression](https://allalgorithms.com/docs/logistic-regression) 113 | - [Neutral Style Transfer](https://allalgorithms.com/docs/neutral-style-transfer) 114 | - [SATisfiable (SAT)](https://allalgorithms.com/docs/sat) 115 | - [Travelling salesman problem (TSP)](https://allalgorithms.com/docs/tsp) 116 | - [A* (A Star)](https://allalgorithms.com/docs/a-star) 117 | - [Artificial Neutral Network](https://allalgorithms.com/docs/artificial-neutral-network) 118 | - [Convolutional Neutral Network](https://allalgorithms.com/docs/convolutional-neutral-network) 119 | - [Decision Tree](https://allalgorithms.com/docs/decision-tree) 120 | - [Factorization Machines](https://allalgorithms.com/docs/factorization-machines) 121 | - [Gaussian Mixture Model](https://allalgorithms.com/docs/gaussian-mixtrue-model) 122 | - [Gradient Boosting Trees](https://allalgorithms.com/docs/gradient-boostring-trees) 123 | - [Hierachical Clustering](https://allalgorithms.com/docs/hierachical-clustering) 124 | - [Image Processing](https://allalgorithms.com/docs/image-processing) 125 | - [K Nearest Neighbors](https://allalgorithms.com/docs/k-nearest-neighbors) 126 | - [K Means](https://allalgorithms.com/docs/k-means) 127 | - [Minimax](https://allalgorithms.com/docs/minimax) 128 | - [Native Bayes](https://allalgorithms.com/docs/native-bayes) 129 | - [Nearest Sequence Memory](https://allalgorithms.com/docs/nearest-sequence-memory) 130 | - [Neutral Network](https://allalgorithms.com/docs/neutral-network) 131 | - [Perceptron](https://allalgorithms.com/docs/perceptron) 132 | - [Principal Component Analysis](https://allalgorithms.com/docs/principal-component-analysis) 133 | - [Q Learing](https://allalgorithms.com/docs/q-learning) 134 | - [Random Forests](https://allalgorithms.com/docs/random-forest) 135 | - [Restricted Boltzman Machine](https://allalgorithms.com/docs/restricted-boltzman-machine) 136 | 137 | ## [Backtracking](backtracking) 138 | 139 | - [Algorithm X](backtracking/algorithm-x) 140 | - [Crossword Puzzle](backtracking/crossword-Puzzle) 141 | - [Knight Tour](backtracking/knight-tour) 142 | - [M Coloring Problem](backtracking/m-coloring-problem) 143 | - [N Queen](backtracking/n-queen) 144 | - [Number of ways in Maze](backtracking/number-of-ways-in-maze) 145 | - [Partitions of set](backtracking/partitions-of-set) 146 | - [Permutation of Strings](backtracking/permutation-of-strings) 147 | - [Powerset](backtracking/powerset) 148 | - [Rat in maze](backtracking/rat-in-maze) 149 | - [Subset Sum](backtracking/subset-sum) 150 | - [Sudoku Solve](backtracking/sudoku-solve) 151 | 152 | ## [Bit Manipulation](bit-manipulation) 153 | 154 | - [Addition using bits](bit-manipulation/adding-using-bits) 155 | - [Bit divisor](bit-manipulation/bit-divisor) 156 | - [Byte swapper](bit-manipulation/byte-swapper) 157 | - [Convert numbers to binary](bit-manipulation/convert-numbers-to-binary) 158 | - [Count set bits](bit-manipulation/count-set-bits) 159 | - [Flip bits](bit-manipulation/flip-bits) 160 | - [Hamming distance](bit-manipulation/hamming-distace) 161 | - [Invert bit](bit-manipulation/invert-bit) 162 | - [Lonely integer](bit-manipulation/lonely-integer) 163 | - [Magic Number](bit-manipulation/magic-number) 164 | - [Maximum XOR Value](bit-manipulation/maximun-xor-value) 165 | - [Power of 2](bit-manipulation/power-of-2) 166 | - [Subset Generation](bit-manipulation/subset-generation) 167 | - [Sum binary numbers](bit-manipulation/sum-binary-numbers) 168 | - [Sum equals XOR](bit-manipulation/sum-equals-xor) 169 | - [Thrice unique number](bit-manipulation/thrice-unique-number) 170 | - [Twice unique number](bit-manipulation/twice-unique-number) 171 | - [XOR Swap](bit-manipulation/xor-swap) 172 | 173 | ## [Cellular Automaton](cellular-automaton) 174 | 175 | - [Brians Brain](cellular-automaton/brians-brain) 176 | - [Conways Game of life](cellular-automaton/conways-game-of-life) 177 | - [Elementary Cellular Automata](cellular-automaton/elementary-cellular-automata) 178 | - [Generic Algorithm](cellular-automaton/generic-algorithm) 179 | - [Langtons Ant](cellular-automaton/langtons-ant) 180 | - [Nobili Cellular Automata](cellular-automaton/nobili-cellular-automata) 181 | - [Von Neoumann Cellular Automata](cellular-automaton/von-neoumann-cellular-automata) 182 | 183 | ## [Computational Geometry](computational-geometry) 184 | 185 | - [2D Line intersection](computational-geometry/) 186 | - [2D Separating Axis test](computational-geometry/) 187 | - [Area of polygon](computational-geometry/) 188 | - [Area of triangle](computational-geometry/) 189 | - [Axis aligned bounding box collision](computational-geometry/) 190 | - [Bresenham Line](computational-geometry/) 191 | - [Chans Algorithm](computational-geometry/) 192 | - [Cohen Sutherland Lineclip](computational-geometry/) 193 | - [Distance between points](computational-geometry/) 194 | - [Graham Scan](computational-geometry/) 195 | - [Halfplane intersection](computational-geometry/) 196 | - [Jarvis March](computational-geometry/) 197 | - [Quickull](computational-geometry/) 198 | - [Sphere tetrahedron intersection](computational-geometry/) 199 | - [Sutherland Hodgeman clipping](computational-geometry/) 200 | 201 | ## [Cryptography](cryptography) 202 | 203 | - [Affine Cipher](cryptography/) 204 | - [Atbash Cipher](cryptography/) 205 | - [Autokey Cipher](cryptography/) 206 | - [Baconian Cipher](cryptography/) 207 | - [Caesar Cipher](cryptography/) 208 | - [Colummnar Cipher](cryptography/) 209 | - [Vigenere Cipher](cryptography/) 210 | 211 | ## [Data Structures](data-structures) 212 | 213 | - [Bag](data-structures/bag/) 214 | - [Hashes](data-structures/hashes/) 215 | - [Linked List](data-structures/linked-list/) 216 | - [List](data-structures/list/) 217 | - [Queue](data-structures/queue/) 218 | - [Stack](data-structures/stack/) 219 | - [Tree](data-structures/tree/) 220 | 221 | ## [Divide and conquer](divide-and-conquer) 222 | 223 | - [Strassen Matrix Manipulation](divide-and-conquer/) 224 | - [Closest Pair of Point](divide-and-conquer/) 225 | - [Inversion Count](divide-and-conquer/) 226 | - [Karatsuba Multiplication](divide-and-conquer/) 227 | - [Maximum Contiguous subsequence sum](divide-and-conquer/) 228 | - [Merge Sort using divide and conquer](divide-and-conquer/) 229 | - [Quick Sort using divide and conquer](divide-and-conquer/) 230 | - [Tournament Method to find min max](divide-and-conquer/) 231 | - [Warnock Algorithm](divide-and-conquer/) 232 | - [X Power Y](divide-and-conquer/) 233 | 234 | ## [Dynamic Programming](dynamic-programming) 235 | 236 | - [Array Median](dynamic-programming) 237 | - [Optima Binary Search Tree](dynamic-programming) 238 | - [Binomial Coefficient](dynamic-programming) 239 | 240 | ## [Gaming Theory](gaming-theory) 241 | 242 | - [Nim Next Best Move Game](gaming-theory/) 243 | - [Nim Win Loss Game](gaming-theory/) 244 | - [Grundy Numbers Kayle Game](gaming-theory/) 245 | 246 | ## [Graphs](graphs) 247 | 248 | - [Bipartite Check](graphs/) 249 | - [Adjacency Lists graphs representation](graphs/) 250 | - [A* (A Star)](https://allalgorithms.com/docs/a-star) 251 | 252 | ## [Greedy Algorithms](greedy-algorithms) 253 | 254 | - [Activity Selection](greedy-algorithms) 255 | - [Dijkstra Shortest Path](greedy-algorithms) 256 | - [Egyptian Fraction](greedy-algorithms) 257 | 258 | ## [Math](math) 259 | 260 | - [2 Sum](math/) 261 | - [Add Polynomials](math/) 262 | - [Amicable Numbers](math/) 263 | - [Armstrong Numbers](math/) 264 | - [Automorphic Numbers](math/) 265 | - [Average Stream Numbers](math/) 266 | - [Babylonian Method](math/) 267 | - [Binomial Coefficient](math/) 268 | - [Catalan Number](math/) 269 | - [Check is Square](math/) 270 | - [Convolution](math/) 271 | - [Coprime Numbers](math/) 272 | - [Count Digits](math/) 273 | - [Count Trailing Zeroes](math/) 274 | - [Decoding of String](math/) 275 | - [Delannoy Number](math/) 276 | - [Derangements](math/) 277 | - [DFA Division](math/) 278 | - [Diophantine](math/) 279 | - [Divided Differences](math/) 280 | - [Euler Totient](math/) 281 | - [Exponentiation Power](math/) 282 | - [Factorial](math/factorial) 283 | - [Fast Fourier transform](math/) 284 | - [Fast inverse (sqrt) Square Root](math/) 285 | 286 | ## [Networking](networking) 287 | 288 | - [Packet Sniffer](networking/) 289 | - [Determine Endianess](networking/) 290 | - [Validate IP](networking/) 291 | 292 | ## [Numerical Analysis](numerical-analysis) 293 | 294 | - [Integral](numerical-analysis/integral) 295 | - [Monte Carlo](numerical-analysis/monte-carlo) 296 | - [Runge Kutt](numerical-analysis/runge-kutt) 297 | 298 | ## [Operating system](operating-system) 299 | 300 | - [Currency](operating-system/) 301 | - [Deadlocks](operating-system/) 302 | - [Memory Management](operating-system/) 303 | - [Scheduling](operating-system/) 304 | - [Shell](operating-system/) 305 | 306 | ## [Randomized Algorithms](randomized-algorithms) 307 | 308 | - [Birthday Paradox](randomized-algorithms) 309 | - [Karger Minimum Cut Algorithm](randomized-algorithms) 310 | - [Kth Smallest Element Algorithm](randomized-algorithms) 311 | - [Random from Stream](randomized-algorithms) 312 | - [Random Node Linked list](randomized-algorithms) 313 | - [Randomized Quicksort](randomized-algorithms) 314 | - [Reservoir Sampling](randomized-algorithms) 315 | - [Shuffle an Array](randomized-algorithms) 316 | 317 | ## [Searches](searches) 318 | 319 | - [Binary Search](searches) 320 | - [Exponential Search](searches) 321 | - [Fibonacci Search](searches) 322 | - [Fuzzy Search](searches) 323 | - [Interpolation Search](searches) 324 | - [Jump Search](searches) 325 | - [Linear Search](searches) 326 | - [Ternay Search](searches) 327 | 328 | ## [Selections Algorithms](selections-algorithms) 329 | 330 | - [Median of Medians](selections-algorithms) 331 | - [Quick Select](selections-algorithms) 332 | 333 | ## [Sorting](sorting) 334 | 335 | - [Bead Sort](sorting/) 336 | - [Bogo Sort](sorting/) 337 | - [Bubble Sort](sorting/) 338 | - [Bucket Sort](sorting/) 339 | - [Circle Sort](sorting/) 340 | - [Comb Sort](sorting/) 341 | - [Counting Sort](sorting/) 342 | - [Cycle Sort](sorting/) 343 | - [Flash Sort](sorting/) 344 | - [Gnome Sort](sorting/) 345 | - [Heap Sort](sorting/) 346 | - [Insertion Sort](sorting/) 347 | - [Intro Sort](sorting/) 348 | - [Median Sort](sorting/) 349 | - [Merge Sort](sorting/) 350 | - [Pipeonhole Sort](sorting/) 351 | - [Quick Sort](sorting/) 352 | - [Radix Sort](sorting/) 353 | - [Selection Sort](sorting/) 354 | - [Shaker Sort](sorting/) 355 | - [Shell Sort](sorting/) 356 | - [Sleep Sort](sorting/) 357 | - [Stooge Sort](sorting/) 358 | - [Topological Sort](sorting/) 359 | - [Tree Sort](sorting/) 360 | 361 | ## [Strings](strings) 362 | 363 | - [Aho Corasick Algorithm](strings) 364 | - [Anagram Search](strings) 365 | - [Arithmetic on large numbers](strings) 366 | - [Boyer Moore Algorithm](strings) 367 | - [Finite Automata](strings) 368 | - [Kasai Algorithm](strings) 369 | - [Kmp Algorithm](strings) 370 | - [Levenshteing Distance](strings) 371 | - [Lipogram Checker](strings) 372 | 373 | ## [Online Challenges](online-challenges) 374 | 375 | - [Coderbyte](online-challenges/coderbyte) 376 | - [Code Chef](online-challenges/code-chef) 377 | - [Code Eval](online-challenges/code-eval) 378 | - [Hackerearth](online-challenges/hackerearth) 379 | - [Hackerrank](online-challenges/hackerrank) 380 | - [LeetCode](online-challenges/leetcode) 381 | - [Project Euler](online-challenges/project-euler) 382 | - [Rosalind](online-challenges/rosalind) 383 | - [SPOJ](online-challenges/spoj) 384 | - [Top Coder](online-challenges/top-coder)` 385 | 386 | ## [Others](others) 387 | 388 | - [Average](others/) 389 | - [Biggest of n numbers](others/) 390 | - [Biggest Suffix](others/) 391 | - [Fifteen Puzzle](others/) 392 | - [Jaccard Similarity](others/) 393 | - [Jose Phus Problem](others/) 394 | - [Lapindrom Checker](others/) 395 | - [Leap Year](others/) 396 | - [Magic Square](others/) 397 | - [Majority Element](others/) 398 | - [Minimum subarray size with degree](others/) 399 | - [No operator addition](others/) 400 | - [Paint fill](others/) 401 | - [Split list](others/) 402 | - [Tokenizer](others/) 403 | - [Unique number](others/) 404 | 405 | ## License 406 | 407 | This work is released under MIT License. 408 | 409 | To the extent possible under law, [Abraham Hernandez (@abranhe)](https://go.abranhe.com/github) has waived all copyright and related or neighboring rights to this work. 410 | 411 |
412 | 413 | 414 | 415 |
416 |
-------------------------------------------------------------------------------- /src/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllAlgorithms/csharp/6bd98128603ed152db29f66365f61a65f957ce79/src/.gitkeep --------------------------------------------------------------------------------