Likhitha Sreeja Dandu

All generations.

generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> using namespace std; void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (

C++
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; }

C++
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> using namespace std; void merge(int* arr, int low, int mid, int high) { int* temp = new int[high - low + 1]; int i = low, j = mid + 1; int k = 0; while(i <= mid && j <= high) { if(arr[i] <= arr[j]) temp[k++] = arr[i++]; else temp[k++] = arr[j++]; } while(i <= mid) temp[k++] = arr[i++]; while(j <= high) temp[k++] = arr[j++]; for(i = low, k = 0; i <= high; i++, k++) arr[i] = temp[k]; delete[] temp; } void mergeSort(int* arr, int low, int high) { if(low < high) { int mid = (low + high

C++
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

public class MergeSort { public static void main(String[] args) { int[] arr = { 5, 1, 6, 2, 3, 4 }; mergeSort(arr); for(int i = 0; i < arr.length; i++) { System.out.print(arr[i]); } } public static void mergeSort(int[] arr) { int size = arr.length; if(size < 2) { return; } int mid = size / 2; int leftSize = mid; int rightSize = size - mid; int[] left = new int[leftSize]; int[] right = new int[rightSize]; for(int i = 0; i < mid; i++) { left[i] = arr[i]; } for(int i = mid; i < size; i++) { right[i - mid] = arr[i]; } mergeSort(left); mergeSort(right); merge(left

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <stdio.h> #include <iostream> using namespace std; void merge(int *array, int l, int m, int r) { int i, j, k, nl, nr; //size of left and right sub-arrays nl = m-l+1; nr = r-m; int larr[nl], rarr[nr]; //fill left and right sub-arrays for(i = 0; i<nl; i++) larr[i] = array[l+i]; for(j = 0; j<nr; j++) rarr[j] = array[m+1+j]; i = 0; j = 0; k = l; //marge temp arrays to real array while(i < nl && j<nr) { if(larr[i] <= rarr[j]) { array[k] = larr[i]; i++; } else

C++
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class MergeSort { private int[] array; private int[] tempMergArr; private int length; public static void main(String a[]){ int[] inputArr = {45,23,11,89,77,98,4,28,65,43}; MergeSort mms = new MergeSort(); mms.sort(inputArr); for(int i:inputArr){ System.out.print(i); System.out.print(" "); } } public void sort(int inputArr[]) { this.array = inputArr; this.length = inputArr.length; this.tempMergArr = new int[length]; doMergeSort(0, length - 1); } private void doMergeSort(int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lowerIndex) / 2; // Below step sorts the

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> #include <vector> #include <algorithm> using namespace std; void merge_sort(int *, int, int); void merge(int *, int, int, int); void print(int *, int); void merge_sort(int *arr, int l, int r) { if (l < r) { int m = l+(r-l)/2; merge_sort(arr, l, m); merge_sort(arr, m+1, r); merge(arr, l, m, r); } } void merge(int *arr, int l, int m, int r) { vector<int> L; vector<int> R; int i, j, k; for (i = l; i <= m; i++) { L.push_back(arr[i]); } for (i = m+1; i <= r; i++) { R.push_back(arr[i]);

C++
View

Improve the user registration system to incorporate password hashing and salting techniques for enhanced security of user credentials.

<form method="POST" action="register.php"> <input type="hidden" name="register" value="true"> <input type="text" name="username" value="<?php if (isset($_SESSION['username'])) echo $_SESSION['username'] ?>" placeholder="username"> <input type="password" name="password" value="" placeholder="password"> <input type="password" name="confirm_password" value="" placeholder="confirm password"> <input type="submit" name="register" value="Register"> </form>

generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

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) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp 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 temp arrays */ // Initial indexes of first and second subarr

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program implementing an optimization for the Merge Sort algorithm that aims to improve the performance for small subarrays. When the size of a subarray falls below a certain threshold, use a more efficient sorting algorithm like Insertion Sort instead of recursively applying Merge Sort.

#include <iostream> #include <vector> #define THRESHOLD 10 using namespace std; void printArray(vector<int> A){ for (int i : A) cout << i << " "; cout << endl; } vector<int> merge(vector<int> left, vector<int> right){ int i = 0; int j = 0; vector<int> res; while (i < left.size() && j < right.size()){ if (left[i] < right[j]){ res.push_back(left[i]); i++; }else{ res.push_back(right[j]); j++; } } while (i < left.size()){ res.push_back(left[i]); i++; } while (j < right.size()){ res.push_back(right[j]); j++; } return res

C++
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

// C++ program to implement Merge Sort algorithm #include <bits/stdc++.h> using namespace std; // A function to merge the two half into a sorted data. void Merge(int *a, int low, int high, int mid) { // We have low to mid and mid+1 to high already sorted. int i, j, k, temp[high-low+1]; i = low; k = 0; j = mid + 1; // Merge the two parts into temp[]. while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; } } // Insert all the remaining

C++
View

Enhance the web application to handle various error scenarios during the registration process, such as duplicate email addresses or weak passwords, displaying appropriate error messages to users.

<div id="register"> <div class="container"> <form action="/signup" method="post"> <h2>Register</h2> <div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control" id="email" name="email" placeholder="Enter email"> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" id="password" name="password" placeholder="Password"> </div> <button type="submit" class="btn btn-primary">Register</button> </form> </div> </div>

Extend the web application to include user authentication, allowing registered users to log in and access personalized content on subsequent visits.

<div> <h3>Login</h3> <form [formGroup]="loginForm" (ngSubmit)="onSubmit()"> <div class="form-group"> <label for="username">Username</label> <input type="text" formControlName="username" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.username.errors }" /> <div *ngIf="submitted && f.username.errors" class="invalid-feedback"> <div *ngIf="f.username.errors.required">Username is required</div> </div> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.password.errors }" /> <div *ngIf="submitted && f.password.errors" class="invalid-feedback

generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> class mergeSort { private: int *arr; int size; public: mergeSort(int *arr, int size); void sort(); void print(); void merge(int l, int m, int r); }; mergeSort::mergeSort(int *arr, int size) { this->arr = arr; this->size = size; } void mergeSort::sort() { mergeSort::merge(0, size/2, size-1); } void mergeSort::merge(int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0;

C++
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class MergeSort { public static void mergeSort(int[] array) { if (array.length > 1) { int[] left = leftHalf(array); int[] right = rightHalf(array); mergeSort(left); mergeSort(right); merge(array, left, right); } } public static int[] leftHalf(int[] array) { int size1 = array.length / 2; int[] left = new int[size1]; for (int i = 0; i < size1; i++) { left[i] = array[i]; } return left; } public static int[] rightHalf(int[] array) { int size1 = array.length / 2; int size2 = array.length - size1; int[] right = new int[size2]; for (int i = 0; i < size2; i++) { right[i] = array[i + size1]; }

Java
View
generate functionTue, 25 Jul 2023

Create a JavaScript program that checks if a given number is a prime number and handles cases where the input is not a valid integer gracefully, displaying an appropriate error message. The program should take input from the user and display the result.

#include <iostream> using namespace std; int main() { int number, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> number; for(i = 2; i <= number / 2; ++i) { if(number % i == 0) { isPrime = false; break; } } if (isPrime) cout << "This is a prime number"; else cout << "This is not a prime number"; return 0; }

C++
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

public class Main { public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5, 6 }; int[] b = { 2, 3, 4, 7, 8 }; int n = a.length + b.length; int[] c = new int[n]; int i = 0; int j = 0; int k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { c[k] = a[i]; i++; } else { c[k] = b[j]; j++; } k++; } while (i < a.length) { c[k] = a[i]; i++; k++; } while (j < b.length) { c[k] = b[j]; j++; k++; } for (int x = 0

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> #include <vector> using namespace std; void mergeWithoutRepetition(vector<int>& v, int start, int middle, int end) { vector<int> left(v.begin() + start, v.begin() + middle + 1); vector<int> right(v.begin() + middle + 1, v.begin() + end + 1); int i = 0, j = 0, k = start; while(i < left.size() && j < right.size()) { if(left[i] <= right[j]) { v[k++] = left[i++]; } else { v[k++] = right[j++]; } } while(i < left.size()) { v[k++] = left[i++]; } while(j < right.size()) { v[k++] = right[j++]; } }

C++
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

import java.util.Arrays; public class InPlaceMergeSort { public static void main(String args[]){ int arr[] = {2,3,4,5,1}; int n = arr.length; System.out.println("Given Array"); System.out.println(Arrays.toString(arr)); sort(arr, 0, n-1); System.out.println("\nSorted array"); System.out.println(Arrays.toString(arr)); } public static void sort(int arr[], int l, int r){ if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static void merge(int arr[], int l, int m, int r){ int i = 0; int j = 0; int k = l;

Java
View

Improve the user registration system to incorporate password hashing and salting techniques for enhanced security of user credentials.

<?php if (isset($_POST['submit'])) { require("connection.php"); $email = $_POST['email']; $password = $_POST['password']; $re_password = $_POST['re-password']; $salt = "48@!alsd"; $hash_password = hash_hmac('sha256', $password, $salt); if ($password == $re_password) { $sql = "INSERT INTO user_accounts (email, password) VALUES ('$email', '$hash_password')"; $conn->query($sql); header("Location: login.php"); } else { echo "Password does not match"; } $conn->close(); } ?>

generate functionTue, 25 Jul 2023

Develop a C++ program to find all prime numbers within a given range using parallel processing, making use of multiple cores to speed up the computation. The program should take the start and end integers of the range as input and display all prime numbers within that range.

#include <iostream> #include<time.h> #include <cstdlib> #include <pthread.h> #include <fstream> #include <chrono> using namespace std; struct thread_data { int start; int end; }; void *calculatePrimes(void *threadarg) { // extract the struct data from the void pointer struct thread_data *my_data; my_data = (struct thread_data *) threadarg; // get the start and end numbers from the parameter int start = my_data->start; int end = my_data->end; // this is where the primes will be displayed ofstream output; output.open("output.txt", ios::app); // calculate all the primes within the range for(int i = start; i <= end; i++) { int flag = 0; for (int j = 2; j <= i/2; j++) { if(i

C++
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> using namespace std; void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) {

C++
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class MergeSort { private int[] numbers; private int[] helper; private int number; public void sort(int[] values) { this.numbers = values; number = values.length; this.helper = new int[number]; mergesort(0, number - 1); } private void mergesort(int low, int high) { // check if low is smaller then high, if not then the array is sorted if (low < high) { // Get the index of the element which is in the middle int middle = low + (high - low) / 2; // Sort the left side of the array mergesort(low, middle); // Sort the right side of the array mergesort(middle + 1, high); // Combine them both merge(low, middle, high); } } private void merge(int low, int middle, int high) { // Copy both parts into the helper array for (int i = low

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> using namespace std; void merge(int a[], int low, int mid, int high) { int i = low; int j = mid + 1; int k = low; int temp[20]; while(i <= mid && j <= high) { if(a[i] <= a[j]) { temp[k] = a[i]; k++; i++; } else { temp[k] = a[j]; k++; j++; } } while(i <= mid) { temp[k] = a[i]; k++; i++; } while(j <= high) { temp[k] = a[j]; k++; j++; } for(i = low; i < k

C++
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

public static void merge(int[] arr, int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; /* create temp arrays */ int[] L= new int[n1]; int[] R= new int[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; /* Merge the temp arrays back into arr[l..r]*/ i = 0; // Initial index of first subarray j = 0; // Initial index of second subarray k = l; // Initial index of merged subarray while (i < n1 && j < n2) { if (L[i] <= R[j])

Java
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

public class MergeSort { public static void main(String[] args) { int[] data = {5, 2, 4, 7, 1, 3, 2, 6}; System.out.println("Before: " + Arrays.toString(data)); mergeSort(data); System.out.println("After: " + Arrays.toString(data)); } public static void mergeSort(int[] data) { if (data.length > 1) { int mid = data.length / 2; int[] leftData = Arrays.copyOfRange(data, 0, mid); int[] rightData = Arrays.copyOfRange(data, mid, data.length); mergeSort(leftData); mergeSort(rightData); merge(data, leftData, rightData); } } public static void merge(int[] data, int[] leftData, int[] rightData) { int i = 0; int li = 0; int ri = 0; while

Java
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

public static void main(String[] args) { int[] nums = {0, 1, 2, 3, 4, 5, 6}; mergeSort(nums, 0, nums.length - 1); System.out.println(Arrays.toString(nums)); } private static void mergeSort(int[] nums, int left, int right) { if (left < right) { int mid = (left + right) / 2; mergeSort(nums, left, mid); mergeSort(nums, mid + 1, right); merge(nums, left, mid, right); } } private static void merge(int[] nums, int left, int mid, int right) { while (left <= mid && mid + 1 <= right) { if (nums[left] <= nums[mid + 1]) { left++; } else { int temp = nums[mid + 1]; for (int i = mid + 1; i > left; i--)

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++;

C++
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class MergeSort { 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); } void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for

Java
View
generate functionTue, 25 Jul 2023

Develop a C++ program to find all prime numbers within a given range using parallel processing, making use of multiple cores to speed up the computation. The program should take the start and end integers of the range as input and display all prime numbers within that range.

#include <iostream> #include <thread> using namespace std; void checkPrime(int num){ bool isPrime = true; int sqrtNum = (int) sqrt(num); for(int i = 2; i <= sqrtNum; i++){ if(num % i == 0){ isPrime = false; break; } } if(isPrime){ cout << num << " is a prime number" << endl; }else{ cout << num << " is not a prime number" << endl; } } int main(){ int start, end; cout << "Input start and end: "; cin >> start >> end; thread t1(checkPrime, start); thread t2(checkPrime, end); t1.join(); t2.join(); return 0; }

C++
View

Create a web application using HTML, CSS, and JavaScript that allows users to register with their email addresses and passwords.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <form> <input type="email" name="email" placeholder="Email" required> <input type="password" name="password" placeholder="Password" required> <input type="submit" value="Register"> </form> </body> </html>

generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include <iostream> using namespace std; void merge(int *a, int n, int m) { int i, j, k; int *x = new int[n]; for(i = 0, j = m, k = 0; k < n; k++) { x[k] = j == n ? a[i++] : i == m ? a[j++] : a[j] < a[i] ? a[j++] : a[i++]; } for(i = 0; i < n; i++) { a[i] = x[i]; } delete[] x; } void merge_sort(int *a, int n) { if(n < 2) return; int m = n / 2; merge_sort(a, m); merge_sort(a + m, n - m); merge(a, n, m); } int main() {

C++
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class MergeSort { public static void sort(int[] a, int n) { if (n < 2) { return; } int mid = n / 2; int[] l = new int[mid]; int[] r = new int[n - mid]; for (int i = 0; i < mid; i++) { l[i] = a[i]; } for (int i = mid; i < n; i++) { r[i - mid] = a[i]; } sort(l, mid); sort(r, n - mid); merge(a, l, r, mid, n - mid); } public static void merge( int[] a, int[] l, int[] r, int left, int right) { int i = 0, j = 0, k = 0; while (i < left && j < right) { if (l[i] <= r[j]) { a

Java
View
generate functionWed, 26 Jul 2023

Write a Java program modifying the standard Merge Sort algorithm to perform the merging of the two subarrays in-place, without using any additional memory for creating temporary arrays. The goal is to reduce the overall space complexity of the algorithm.

public class MergeSort { public static void sort(int[] arr) { sort(arr, 0, arr.length-1); } private static void sort(int[] arr, int low, int high) { if (high <= low) { return; } int mid = low + (high - low) / 2; sort(arr, low, mid); sort(arr, mid+1, high); merge(arr, low, mid, high); } private static void merge(int[] arr, int low, int mid, int high) { int i = low; int j = mid+1; while (i <= mid && j <= high) { if (arr[i] <= arr[j]) { i++; } else { int temp = arr[j]; System.arraycopy(arr, i, arr, i+1, j-i); arr[i] = temp; i++; mid++; j++; }

Java
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class MergeSort { private int[] array; private int[] tempMergArr; private int length; public static void main(String a[]){ int[] inputArr = {45,23,11,89,77,98,4,28,65,43}; MergeSort mms = new MergeSort(); mms.sort(inputArr); for(int i:inputArr){ System.out.print(i); System.out.print(" "); } } public void sort(int inputArr[]) { this.array = inputArr; this.length = inputArr.length; this.tempMergArr = new int[length]; doMergeSort(0, length - 1); } private void doMergeSort(int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lower

Java
View
generate functionTue, 25 Jul 2023

Develop a C++ program to find all prime numbers within a given range using parallel processing, making use of multiple cores to speed up the computation. The program should take the start and end integers of the range as input and display all prime numbers within that range.

def isPrime(n): if n==1: return False if n==2: return True if n%2==0 and n>2: return False i=3 while i*i<=n: if n%i==0: return False i=i+2 return True x=int(input('Enter From: ')) y=int(input('Enter To: ')) for num in range(x,y): if isPrime(num): print(num)

Python
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

int a[100], n; int main() { int i, j, k, mid, temp; cout<<"enter the number of elements\n"; cin>>n; cout<<"enter the elements\n"; for(i=0; i<n; i++) { cin>>a[i]; } merge_sort(0, n-1); cout<<"sorted array\n"; for(i=0; i<n; i++) { cout<<a[i]<<"\t"; } return 0; } void merge_sort(int low, int high) { int mid; if(low<high) { mid=(low+high)/2; merge_sort(low, mid); merge_sort(mid+1, high); merge(low, mid, high); } } void merge(int low, int mid, int high) { int h, i,

C++
View
generate functionWed, 26 Jul 2023

Generate a Java program to implement Merge Sort algorithm to efficiently sort an array of elements in ascending order.

public class Solution { public int[] mergeSort(int[] arr) { // Write your solution here. if(arr == null || arr.length <= 1){ return arr; } int[] helper = new int[arr.length]; mergeSort(arr, helper, 0, arr.length - 1); return arr; } private void mergeSort(int[] arr, int[] helper, int left, int right){ if(left >= right){ return; } int mid = left + (right - left) / 2; mergeSort(arr, helper, left, mid); mergeSort(arr, helper, mid + 1, right); merge(arr, helper, left, mid, right); } private void merge(int[] arr, int[] helper, int left, int mid, int right){ for(int i = left; i <= right; i++){ helper[i] = arr[i]; } int leftIndex = left; int

Java
View
generate functionWed, 26 Jul 2023

Write a C ++ program to implement Merge Sort algorithm to efficiently handle an input array that may contain duplicate elements. The goal is to modify the algorithm in such a way that duplicate elements appear in the sorted array in the same order as they appear in the input.

#include<iostream> #include<cstdlib> using namespace std; void merge(int *,int, int , int ); void mergesort(int *a, int low, int high) { int mid; if (low < high) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return; } void merge(int *a, int low, int high, int mid) { int i, j, k, c[50]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { c[k] = a[j];

C++
View

Questions about programming?Chat with your personal AI assistant