Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed pre course 2 #1632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
45 changes: 45 additions & 0 deletions Exercise_1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity :O(logN) where N = number of elements in array
// Space Complexity : O(1)
using System;

class BinarySearch
{
// Returns index of x if it is present in arr[l.. r], else return -1
int binarySearch(int[] arr, int l, int r, int x)
{
//Write your code here
while (l <= r)
{
int mid = (l + r) / 2;
if (arr[mid] == x)
{
return mid;
}
if (x >= arr[l] && x < arr[mid])
{
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);
Console.ReadLine();
}
}
79 changes: 79 additions & 0 deletions Exercise_2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Time Complexity :O(NlogN) where N = number of elements in array
// Space Complexity : O(logN)
using System;


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)
{
//Your code here
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 - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(arr, i, j);

}
}
swap(arr, i + 1, high);
return i + 1;
}
/* 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] + " ");
}
Console.WriteLine();
}

// 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);
Console.ReadLine();
}
}
65 changes: 65 additions & 0 deletions Exercise_3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Time Complexity :O(logN) where N = number of elements in list
// Space Complexity : O(1)
using System;

class LinkedList
{
Node head; // head of linked list

/* Linked list node */
class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}

/* Function to print middle of linked list */
//Complete this function
void printMiddle()
{
//Write your code here
// Implement using Fast and slow pointers
Node slowPointer = head;
Node fastPointer = head;
while (fastPointer.next !=null && fastPointer.next.next != null)
{
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
}
Console.WriteLine("Middle of linked list is " + slowPointer.data);
}

public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}

public void printList()
{
Node tnode = head;
while (tnode != null)
{
Console.WriteLine(tnode.data+"->");
tnode = tnode.next;
}
Console.WriteLine("NULL");
}

public static void Main(string[] args)
{
LinkedList llist = new LinkedList();
for (int i=15; i>0; --i)
{
llist.push(i);
llist.printList();
llist.printMiddle();
}
}
}
99 changes: 99 additions & 0 deletions Exercise_4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Time Complexity :O(NlogN) where N = number of elements in array
// Space Complexity : O(N) - extra space to store the temp left and right array
using System;

class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int[] arr, int l, int m, int r)
{
//Your code here
int l1 = (m - l + 1);
int l2 = (r - m);
int[] leftArray = new int[l1];
int[] rightArray = new int[l2];

for (int i = 0; i < l1; i++)
{
leftArray[i] = arr[l + i];
}
for (int i = 0; i < l2; i++)
{
rightArray[i] = arr[m+1+i];
}

int leftIndex = 0;
int rightIndex = 0;
int k = l;

while (leftIndex < l1 && rightIndex < l2)
{
if (leftArray[leftIndex] <= rightArray[rightIndex])
{
arr[k] = leftArray[leftIndex];
k++;
leftIndex++;
}
else
{
arr[k] = rightArray[rightIndex];
k++;
rightIndex++;
}
}
while (leftIndex < l1)
{
arr[k] = leftArray[leftIndex];
k++;
leftIndex++;
}
while (rightIndex < l2)
{
arr[k] = rightArray[rightIndex];
k++;
rightIndex++;
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int[] arr, int l, int r)
{
//Write your code here
//Call mergeSort from here
if (l < r)
{
int mid = (l + r) / 2;
sort(arr, l, mid);
sort(arr, mid + 1, r);
merge(arr, l, mid, r);
}
}

/* 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] + " ");
Console.WriteLine();
}

// Driver method
public static void Main(string[] args)
{
int[] arr = { 12, 11, 13, 5, 6, 7 };

Console.WriteLine("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.Length - 1);

Console.WriteLine("\nSorted array");
printArray(arr);
Console.ReadLine();
}
}
81 changes: 81 additions & 0 deletions Exercise_5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Time Complexity :O(NlogN) where N = number of elements in array
// Space Complexity : O(logN)
using System;

class IterativeQuickSort
{
void swap(int[] arr, int i, int j)
{
//Try swapping without extra variable
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

/* This function is same in both iterative and
recursive*/
int partition(int[] arr, int l, int h)
{
//Compare elements and swap.
int pivot = arr[h];
int i = l - 1;
for (int j = l; j <= h-1 ; j++)
{
if (arr[j] < pivot)
{
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, h);
return i+1;
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int[] arr, int l, int h)
{
//Try using Stack Data Structure to remove recursion.
int[] stack = new int[h - l + 1];
int top = -1;

stack[++top] = l;
stack[++top] = h;

while (top >= 0)
{
h = stack[top--];
l = stack[top--];

int pivot = partition(arr, l, h);

if (pivot - 1 > l)
{
stack[++top] = l;
stack[++top] = pivot - 1;
}
if (pivot + 1 < h)
{
stack[++top]= pivot + 1;
stack[++top] = h;
}
}
}

// A utility function to print contents of arr
void printArr(int[] arr, int n)
{
int i;
for (i = 0; i < n; ++i)
Console.WriteLine(arr[i] + " ");
}

// Driver code to test above
public static void Main(string[] args)
{
IterativeQuickSort ob = new IterativeQuickSort();
int[] arr = { 4, 3, 5, 2, 1, 3, 2, 3 };
ob.QuickSort(arr, 0, arr.Length - 1);
ob.printArr(arr, arr.Length);
Console.ReadLine();
}
}