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

Done Precourse2 #1633

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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jpa-buddy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 32 additions & 20 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
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");
class BinarySearch {
// Returns index of x if it is present in arr[l.. r], else return -1

// Time Complexity :O(logn)
// Space Complexity :O(1)
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l+(r-l)/2;
if(arr[m] == x)
return m;
if(arr[m]<x)
l=m+1;
else
r = m - 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);
}
}
106 changes: 63 additions & 43 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,68 @@
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
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 */
// Time Complexity :O(nlogn)
// Space Complexity :O(logn)
void swap(int arr[],int i,int j){
//Your code here
//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,

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
}

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);
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);
}
}
103 changes: 54 additions & 49 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
class LinkedList
{
Node head; // head of linked list

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()
{
//Complete this function
// Time Complexity : O(n)
// Space Complexity :O(1)
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;
}
//Implement using Fast and slow pointers
if (head == null) {
System.out.println("The list is empty");
return;
}
Node slow = head;
Node fast = head;

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
}

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 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();
}
}
}
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