diff --git a/CSharp/BinarySearchUsingRecursion.cs b/CSharp/BinarySearchUsingRecursion.cs new file mode 100644 index 0000000..4bde9ed --- /dev/null +++ b/CSharp/BinarySearchUsingRecursion.cs @@ -0,0 +1,49 @@ +using System; + + /* Recursive implementation of Binary Search*/ +namespace BinarySearch +{ + internal static class Program + { + private static void Main() + { + // Console.WriteLine("Please enter the number of element in array"); + // var n = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); + // //int[] arr = {6, 7, 3, 8, 2, 9, 1}; + // var arr = new int[n]; + // Console.WriteLine("Please enter the element of array"); + // for (var i = 0; i < n; i++) + // { + // arr[i] = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); + // } + // + // Console.WriteLine("Sorted Array is :: "); + int[] arr = {2, 3, 4, 10, 40}; + int n = arr.Length; + int x = 10; + var t = binarySearch(arr, 0, n - 1, x); + Console.WriteLine(t); + } + + private static int binarySearch(int[] arr, int l, int r, int x) + { + if (r >= l) + { + int mid = l + (r - 1) / 2; + if (arr[mid] == x) + { + return mid; + } + + if (arr[mid] > x) + { + return binarySearch(arr, l, mid - 1, x); + } + + return binarySearch(arr, mid + 1, r, x); + } + + return -1; + } + } +} diff --git a/CSharp/BubbleSort.cs b/CSharp/BubbleSort.cs new file mode 100644 index 0000000..eaeefad --- /dev/null +++ b/CSharp/BubbleSort.cs @@ -0,0 +1,44 @@ +using System; + +namespace ImplementingBubbleSortAlgo +{ + internal static class Program + { + private static void Main() + { + Console.WriteLine("Please enter the number of element in array"); + var n = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); + + //int[] arr = {6, 7, 3, 8, 2, 9, 1}; + var arr = new int[n]; + Console.WriteLine("Please enter the element of array"); + for (var i = 0; i 1) + { + Quick_Sort(arr, i, pivot - 1); + } + + if (pivot + 1 < arrLength) + { + i = pivot + 1; + continue; + } + } + + break; + } + } + + private static int Partition(int[] arr, int i, int arrLength) + { + var pivot = arr[i]; + while (true) + { + while (arr[i] < pivot) + { + i++; + } + + while (arr[arrLength] > pivot) + { + arrLength--; + } + + if (i < arrLength) + { + if (arr[i] == arr[arrLength]) return arrLength; + var temp = arr[i]; + arr[i] = arr[arrLength]; + arr[arrLength] = temp; + } + else + { + return arrLength; + } + } + } + } +} diff --git a/README.md b/README.md index 15e8530..e4a090a 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Reference purposes . 40. Longest Common Subsequence [python](https://github.com/saru95/DSA/blob/master/Python/LCS.py) 41. Kruskal Algorithm [cpp](https://github.com/saru95/DSA/blob/master/CPP/Kruskal.cpp) [java](https://github.com/saru95/DSA/blob/master/Java/Kruskal.java) [kotlin](https://github.com/saru95/DSA/blob/master/Kotlin/Kruskal.kt) 42. Bubble Sort [cpp](https://github.com/saru95/DSA/blob/master/CPP/Bubblesort.cpp) [python](https://github.com/saru95/DSA/blob/master/Python/Bubblesort.py) [java](https://github.com/saru95/DSA/blob/master/Java/BubbleSort.java) [kotlin](https://github.com/saru95/DSA/blob/master/Kotlin/BubbleSort.kt) [scala](https://github.com/saru95/DSA/blob/master/Scala/BubbleSort.scala) +[CSharp](https://github.com/saru95/DSA/blob/master/CSharp/BubbleSort.cs) 43. Stacks - Array implementation [cpp](https://github.com/saru95/DSA/blob/master/CPP/Stacks-ArrayImp.cpp) - Linked List Implementation [cpp](https://github.com/saru95/DSA/blob/master/CPP/Stacks-LinkLImp.cpp) 44. Queue [cpp](https://github.com/saru95/DSA/blob/master/CPP/Queue.cpp) [c](https://github.com/theasianpianist/DSA/blob/master/C/queue.c) [java](https://github.com/saru95/DSA/blob/master/Java/Queue.java) [kotlin](https://github.com/saru95/DSA/blob/master/Kotlin/Queue.kt) 45. Comb Sort [java](https://github.com/saru95/DSA/blob/master/Java/CombSort.java) [kotlin](https://github.com/saru95/DSA/blob/master/Kotlin/CombSort.kt)