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 with the prec 2(please review) #1603

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
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "macos-clang-arm64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [
""
]
}
],
"version": 4
}
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "/Users/shivangisingh/Desktop/PreCourse-2",
"program": "/Users/shivangisingh/Desktop/PreCourse-2/build/Debug/outDebug"
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "clang",
"C_Cpp_Runner.cppCompilerPath": "clang++",
"C_Cpp_Runner.debuggerPath": "lldb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
Binary file added BinarySearch.class
Binary file not shown.
23 changes: 20 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
//time-complexity: O(logn)
//space-complexity: O(1)
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 binarySearch(int arr[], int l, int r, int x) {
while (l<= r)

{
int mid = l + (r - l) / 2; // finding the middle element of the array
if(arr[mid] == x) {
return mid;
}
if(arr[mid] > x) {
r=mid-1;
}
else {
l=mid+1;
}
// if the middle element is the element we are looking for, return the index of the middle element
}

return -1;} // if the element is not found, return -1


// Driver method to test above
public static void main(String args[])
Expand Down
48 changes: 37 additions & 11 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//time-complexity: O(nlogn)
//space-complexity: O(1)
class QuickSort
{
/* This function takes last element as pivot,
Expand All @@ -6,23 +8,47 @@ class QuickSort
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
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) {
int pivot = arr[high]; // Pivot is the last element
int i = (low - 1); // Index of smaller element

for (int j = low; j < high; j++) {
// If the current element is smaller than or equal to the pivot
if (arr[j] <= pivot) {
i++;
// Swap arr[i] and arr[j]
swap(arr, i, j);
}
}

// Swap the pivot element with the element at i+1
swap(arr, i + 1, high);

return i + 1; // Return the partition index
}

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
}
void sort(int arr[], int low, int high) {
if (low < high) {
// Partition the array and get the pivot index
int pi = partition(arr, low, high);

// Recursively sort elements before and after partition
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}


/* A utility function to print array of size n */
static void printArray(int arr[])
Expand Down
26 changes: 21 additions & 5 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//time-complexity: O(n)
//space-complexity: O(1)
class LinkedList
{
Node head; // head of linked list
Expand All @@ -16,11 +18,25 @@ class Node

/* Function to print middle of linked list */
//Complete this function
void printMiddle()
{
//Write your code here
//Implement using Fast and slow pointers
}
void printMiddle()
{
// Implement using fast and slow pointers
Node slow = head;
Node fast = head;

// Traverse the list with two pointers
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}

// Print the middle element
if (slow != null) {
System.out.println("The middle element is: " + slow.data);
} else {
System.out.println("The list is empty.");
}
}

public void push(int new_data)
{
Expand Down
57 changes: 56 additions & 1 deletion Exercise_4.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,68 @@ class MergeSort
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
//Your code here
// Sizes of the two subarrays
int n1 = m - l + 1;
int n2 = r - m;

// Temporary arrays
int L[] = new int[n1];
int R[] = new int[n2];

// Copy data to temporary arrays
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

// Merge the temporary arrays

int i = 0, j = 0;

// Initial index of merged subarray
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

// Copy remaining elements of L[], if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy remaining elements of R[], if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r) {
// Find the middle point
int m = l + (r - l) / 2;

// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);

// Merge the sorted halves
merge(arr, l, m, r);
}
//Write your code here
//Call mergeSort from here
}
Expand Down
45 changes: 45 additions & 0 deletions Exercise_5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,63 @@ class IterativeQuickSort {
void swap(int arr[], int i, int j)
{
//Try swapping without extra variable
if (i != j) {
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}

/* This function is same in both iterative and
recursive*/
int partition(int arr[], int l, int h)
{
int pivot = arr[h]; // Last element as pivot
int i = l - 1; // Index for the smaller element

for (int j = l; j <= h - 1; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr, i, j); // Swap elements smaller than pivot
}
}
swap(arr, i + 1, h); // Place pivot in the correct position
return i + 1;
//Compare elements and swap.
}

// Sorts arr[l..h] using iterative QuickSort
void QuickSort(int arr[], int l, int h)
{
// Create an auxiliary stack
int[] stack = new int[h - l + 1];
int top = -1;

// Push initial values of l and h to stack
stack[++top] = l;
stack[++top] = h;

// Keep popping from stack while it is not empty
while (top >= 0) {
// Pop h and l
h = stack[top--];
l = stack[top--];

// Set pivot element at its correct position
int pivotIndex = partition(arr, l, h);

// If there are elements on the left of the pivot, push them onto the stack
if (pivotIndex - 1 > l) {
stack[++top] = l;
stack[++top] = pivotIndex - 1;
}

// If there are elements on the right of the pivot, push them onto the stack
if (pivotIndex + 1 < h) {
stack[++top] = pivotIndex + 1;
stack[++top] = h;
}
}
//Try using Stack Data Structure to remove recursion.
}

Expand Down