In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays.
You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
Sort an array when two halves are sorted
Given an integer array of which both first half and second half are sorted. Task is to merge two sorted halves of array into single sorted array.
Examples:
Input : A[] = { 2, 3, 8, -1, 7, 10 }
Output : -1, 2, 3, 7, 8, 10
Input : A[] = {-4, 6, 9, -1, 3 }
Output : -4, -1, 3, 6, 9
A Simple Solution is to sort the array.
Below is the implementation of above approach :
// Java program to Merge two sorted halves of
// array Into Single Sorted Array
import java.io.*;
import java.util.*;
class GFG {
static void mergeTwoHalf(int[] A, int n)
{
// Sort the given array using sort
STL
Arrays.sort(A);
}
// Driver program to test above function
static public void main(String[] args)
{
int[] A = { 2, 3, 8, -1, 7, 10
};
int n = A.length;
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n;
i++)
System.out.print(A[i] + " ");
}
}
// java program to Merge Two Sorted Halves Of
// Array Into Single Sorted Array
import java.io.*;
class Merge {
// Merge two sorted halves of Array
// into single sorted array
static void mergeTwoHalf(int[] A, int n)
{
int half_i = 0; // starting index of second half
int i;
// Temp Array store sorted resultant array
int[] temp = new int[n];
// First Find the point where array is divide
// into two half
for (i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
half_i = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (half_i == 0)
return;
// Merge two sorted arrays in single sorted array
i = 0;
int j = half_i;
int k = 0;
while (i < half_i && j < n) {
if (A[i] < A[j])
temp[k++] = A[i++];
else
temp[k++] = A[j++];
}
// Copy the remaining elements of A[i to half_! ]
while (i < half_i)
temp[k++] = A[i++];
// Copy the remaining elements of A[ half_! to n ]
while (j < n)
temp[k++] = A[j++];
for (i = 0; i < n; i++)
A[i] = temp[i];
}
// Driver program to test above function
static public void main(String[] args)
{
int[] A = {2, 3, 8, -1, 7, 10};
int n = A.length;
mergeTwoHalf(A, n);
// Print sorted Array
for (int i = 0; i < n; i++)
System.out.print(A[i] + " ");
}
}
Output:
-1 2 3 7 8 10
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...
Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...
Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below Languaga is C platform Ubuntu
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below Languaga is C platform Ubuntu
Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++)...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
Implement Merge Sort and test it on a small list of 10 random integer values. c++ please template // merge-sort S void mergeSort(list& S, const C& less) { typedef typename list::iterator Itor; // sequence of elements int n = S.size(); if (n <= 1) return; // already sorted list S1, S2; Itor p = S.begin(); for (int i = 0; i < n / 2; i++) S1.push_back(*p++); // copy first half to...
use the same code. but the code needs some modifications. so
use this same code and modify it and provide a output
Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...
Find the space complexity of Merge Sort below as a function of n (the length of A). Assume: • The elements of A require (1) space. • Merge takes 2 sorted arrays as input and merges them into one sorted array containing both inputs' elements in (n) space. A (there is no index trickery allowing Al and A2 Note that Al and A2 are independent arrays from to be "in" A; A is not being sorted in place). Merge Sort...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...