Skip to content
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
21 changes: 0 additions & 21 deletions Exercise_1.cpp

This file was deleted.

47 changes: 47 additions & 0 deletions Exercise_1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

// Time Complexity
// Best Case - O(1)
// Average Case - O(log n)
// Worst Case - O(log n)

// Space Complexity
// O(1)
public class BinarySearch
{
// Returns index of x if it is present in arr[l.. r], else return -1
public int binarySearch(int[] arr, int l, int r, int x)
{
//Write your code here
while (l <= r)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
{
return mid;
}
else if (arr[mid] > x)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return -1;
}

// Driver method to test above
public static void Main(String[] args)
{
BinarySearch ob = new BinarySearch();
int[] arr = { 2, 3, 4, 10, 40 };
int n = arr.Length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
Console.WriteLine("Element not present");
else
Console.WriteLine("Element found at index " + result);
}
}
21 changes: 0 additions & 21 deletions Exercise_1.java

This file was deleted.

16 changes: 0 additions & 16 deletions Exercise_1.js

This file was deleted.

22 changes: 0 additions & 22 deletions Exercise_1.py

This file was deleted.

47 changes: 0 additions & 47 deletions Exercise_2.cpp

This file was deleted.

72 changes: 72 additions & 0 deletions Exercise_2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//Time Complexity = O(nlogn)
//Space Complexity = O(logn)
class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
void swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

int partition(int[] arr, int low, int high)
{
//Write code here for Partition and Swap
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (arr[j] < pivot)
{
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
//{10, 7, 8, 9, 1, 5};
//1,5,7,8,9,10
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void sort(int[] arr, int low, int high)
{
// Recursively sort elements before
// partition and after partition
if (low < high)
{
int pivot = partition(arr, low, high);
sort(arr, low, pivot - 1);
sort(arr, pivot + 1, high);
}
}

/* A utility function to print array of size n */
static void printArray(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n; ++i)
Console.WriteLine(arr[i] + " ");
}

// Driver program
public static void main(String[] args)
{
int[] arr = { 10, 7, 8, 9, 1, 5 };
int n = arr.Length;

QuickSort ob = new QuickSort();
ob.sort(arr, 0, n - 1);

Console.WriteLine("sorted array");
printArray(arr);
}
}
48 changes: 0 additions & 48 deletions Exercise_2.java

This file was deleted.

41 changes: 0 additions & 41 deletions Exercise_2.js

This file was deleted.

23 changes: 0 additions & 23 deletions Exercise_2.py

This file was deleted.

Loading