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 Submission #1617

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
19 changes: 18 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
class BinarySearch {
/*
Space Complexity O(1) no extra space used
Time Complexity O(log(n)) where n is the no of elements in the array
*/
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
int mid=0;
while(l<=r) {
mid=l+(r-l)/2;
if(arr[mid]==x)
return mid;
if(x<arr[mid]){
r=mid-1;
}
else {
l=mid+1;
}
}
return -1;
}

// Driver method to test above
Expand Down
111 changes: 68 additions & 43 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,73 @@
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
/*
Space Complexity O(n) - recursive stack call
Time Complexity average nlog(n)
worst n^2
*/
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
//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 partition = partition(arr,low,high);
sort(arr,low,partition-1);
sort(arr,partition+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);
}
}
105 changes: 58 additions & 47 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,64 @@
class LinkedList
{
Node head; // head of linked list

/*
Time Complexity O(n) n is no of nodes in the linked list
Space Complexity O(1)
*/
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()
{
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;
}
Node slow=head;
Node fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
System.out.println(slow.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 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();
}
}
}
122 changes: 82 additions & 40 deletions Exercise_4.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,84 @@
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
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{

/*
Time Complexity n(log(n))
Space Complexity O(n) temporary array
*/
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 len1=m-l+1;
int len2=r-m;

int[] left=new int[len1];
int[] right=new int[len2];

for(int i=0;i<len1;i++)
left[i]=arr[l+i];
for(int i=0;i<len2;i++)
right[i]=arr[m+1+i];

int i=0,j=0;
int k=l;
while(i<len1 && j<len2){
if(left[i]<=right[j]){
arr[k++]=left[i++];
}
else{
arr[k++]=right[j++];
}
}

while(i<len1){
arr[k++]=left[i++];
}

while(j<len2){
arr[k++]=right[j++];
}
}

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

sort(arr,l,mid);
sort(arr,mid+1,r);

//Call mergeSort from here
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)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("Given Array");
printArray(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);
System.out.println("\nSorted 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 method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};

System.out.println("Given Array");
printArray(arr);

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

System.out.println("\nSorted array");
printArray(arr);
}
}
Loading