Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions CSharp/BinarySearchUsingRecursion.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
44 changes: 44 additions & 0 deletions CSharp/BubbleSort.cs
Original file line number Diff line number Diff line change
@@ -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 <n; i++)
{
arr[i] = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
}
Console.WriteLine("Sorted Array is :: ");
BubbleSort(arr);
}

private static void BubbleSort(int[] arr)
{
for (var i = 0; i < arr.Length; i++)
{
for (var j = i; j < arr.Length; j++)
{
if (arr[j] < arr[i])
{
var temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}

foreach (var variable in arr)
{
Console.WriteLine(variable);
}
}
}
}
81 changes: 81 additions & 0 deletions CSharp/QuickSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
/*
* Quick Sort : Quick sort is Divide and Conquer algorithm.
* It picks an element as pivot and partitions the given array around the piked pivot.
*
*/
namespace ConsoleApp169
{
internal static class Program
{
public static void Main()
{
var arr = new int[] { 2, 5, -4, 11, 0, 18, 22, 67, 51, 6 };
foreach (var variable in arr)
{
Console.WriteLine(variable);
}

Console.WriteLine(" Sorted Array ");

Quick_Sort(arr, 0, arr.Length - 1);
foreach (var variable in arr)
{
Console.WriteLine(variable);
}
}

private static void Quick_Sort(int[] arr, int i, int arrLength)
{
while (true)
{
if (i < arrLength)
{
var pivot = Partition(arr, i, arrLength);

if (pivot > 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;
}
}
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down