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

PreCourse-2 Completed #1598

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
61 changes: 41 additions & 20 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
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
}

// 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)
System.out.println("Element not present");
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

class BinarySearch {
// Returns index of x if it is present in arr[l.. r], else return -1
// In binary search we divide the array into half repeatedly to search for the
// target value
// Works for sorted list only
int binarySearch(int arr[], int l, int r, int x) {
// Write your code here
while (l <= r) {
int midPosition = (l + r) / 2;
int mid = arr[midPosition];
if (x == mid) {
// If target value is equal to the mid value return index of mid value
return midPosition;
} else if (x < mid) {
// If target value is less than the mid value, search in the left sub array
r = midPosition - 1;
} else if (x > mid) {
// If target value is greater than the mid value, search in the right sub array
l = midPosition + 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)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
System.out.println("Element found at index " + result);
}
}
127 changes: 81 additions & 46 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,83 @@
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
// Time Complexity : Not sure
// Space Complexity : Not sure
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this : Referred some documention and videos and coded. Not getting what will be time complexity

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
}
/* 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
}


int partition(int arr[], int low, int high) {

// Write code here for Partition and Swap
// Taking the last element as pivot
int pivot = arr[high];
// Pointer for swap position
int i = low - 1;
// Another pointer j that will start from first element, comparing each element
// with pivot
for (int j = low; j < high; j++) {
// If any element less than pivot, swap it the element at index i
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
// Swap pivot and place it in correct position
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 pi = partition(arr, low, high);
// System.out.println(pi);
// Recursively call sort
sort(arr, low, pi - 1);
sort(arr, pi + 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)
System.out.print(arr[i]+" ");
System.out.println();
}

// 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);

System.out.println("sorted array");
printArray(arr);
}
}
static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// 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);

System.out.println("sorted array");
printArray(arr);
}
}
111 changes: 60 additions & 51 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
class LinkedList
{
Node head; // head of linked list

// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Referred some documention and videos and coded

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

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

class Node {
int data;
Node next;

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
}

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)
{
System.out.print(tnode.data+"->");
tnode = tnode.next;
}
System.out.println("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();
}
}
}
// Complete this function
void printMiddle() {
// Start both slow and fast pointer from head
Node slp = head;
Node fp = head;

while (fp != null && fp.next != null) {
// Increment fast pointer by 2 till last element
fp = fp.next.next;
// Increment slow pointer by 1
slp = slp.next;

}
// As soon as the fast pointer reaches the end, the slow pointer will reach at
// the mid position
System.out.println("Middle " + slp.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) {
System.out.print(tnode.data + "->");
tnode = tnode.next;
}
System.out.println("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();
}
}
}
Loading