1)
can any one please givem the code for this
a) If n is a power of 2, as it is in Figure 9-3, you would merge
pairs of individual entries, starting at the
beginning of the array. Then you would return to the beginning of
the array and merge pairs of twoentry
subarrays. Finally, you would merge one pair of four-entry
subarrays. Notice that the subarrays
in each pair of subarrays contain the same number of entries.
In general, n might not be a power of 2. After merging a certain
number of pairs of subarrays, you
might have too few entries left to make up a complete pair of
subarrays. In Figure 9-13a, after merging
pairs of single entries, one entry is left over. You then merge one
pair of two-entry subarrays, and merge
the leftover two-entry subarray with the leftover single entry.
Parts b and c of Figure 9-13 show two
other possibilities.
Implement an iterative merge sort. Use the algorithm merge that was
given in Segment 9.3. A private
method that uses merge to merge the pairs of subarrays is useful.
After the method completes its task, you
can handle the leftovers that we just described.
Sol: a)
this is te code i have but am getting lot of eerrors can anyone edit this plesasee?
public class MergeSortA
{
public static<T> <T extends Comparable super T>>
void
mergeSortA(T[]a, int n)
{
T[]tempArray=(T[])new comparable[a.lenghth];
Int startingLeftovers=n;
For(int segmetLength =1;segmentLenght <=n/2;
segmentLength =2*segementLength
{
startingeftovers=segmentPairs(a,tempArray,n,segmentLength);
int endingSegment=startingLeftovers+segmentLength-1;
if(endingSegment<n-1)
merge(a,tempArray,startingLeftovers,endingSegment,n-1);
}
if(startingLeftovers<n)
merge(a,tempArray,0,startingLeftovers-1,n-1);
}
private static<T extends Comparable super T>>int
segmentPairs(T[]a,T[]tempArray,int n,int segmentlength)
{
int mergedpairLength=2*segmentLength;
int numberOfpairs=n/mergedPairLength;
int starting Segment1=0;
for(int count =1; count<=numberOfPairs;count++)
{
int endingSegment1=startingSegment1+segmentLength-1;
int startingSegment2=endingSegment1+1;
int ending Segment2=startingSegment2+segmentLength-1;
merge(a,tempArray,startingSegment1,endingSegment1,endingSegment2);
startingSegment1=endingSegment2+1;
}
return starting Segment1;
}
private static<T extends Comparable super T>>
void
merge(T[]a, T[]tempArray, int first, int mid,int last,)
{
int startingHalf1=first;
int endHalf1=mid;
int startinghalf2=mid+1;
int endHalf2=last;
int index=startingHalf1;
for(;(startingHalf1<=endHalf1)&&(startinghalf2<=endHalf2);index++)
{
if(a[startingHalf1].compare To(a[startinghalf2])<0))
{
tempArray[index]=a[startinghalf1];
startingHalf1++;
}
else
{
tempArray[index]=a[startinghalf2];
startingHalf2++;
}
}
for(;startingHalf1<=endHalf1;startingHalf1++,index++)
tempArray[index]=a[startinghalf1];
for(;startingHalf2<=endHalf2;startingHalf2++,index++)
tempArray[index]=a[startinghalf2];
for(index-first;index<=last;index++){
}
public static void main(String[] args)
{
Integer[]a={50,45,20,40,10,15,5,25,30,35,55};
System.out.println("The array before sorting is ");
for(int i = 0; i<a.length;i++)
System.out.print(a[i]+"");
System.out.print(false);
mergeSortA(a,a.length);
System.out.println("\n The array after sorting is ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+"");
System.out.print(false);
}
}
//Please see the source code below I have done modification in your code to make it working please do thumbs up // if you like the solution:
public class MergeSortA
{
public static <T extends Comparable<? super
T>>
void mergeSortA(T[] a, int n)
{
T[] tempArray = (T[])new
Comparable<?>[a.length];
mergeSort(a, tempArray, 0,
n-1);
}
private static <T extends Comparable<? super
T>>
void mergeSort(T[] a, T[] tempArray, int
startIndex, int lastIndex)
{
if (startIndex <
lastIndex)
{
int mid =
(startIndex +
lastIndex)/2; // find the
mid point in array
mergeSort(a,
tempArray, startIndex, mid); // sorting left half
array[startIndex..mid]
mergeSort(a,
tempArray, mid + 1, lastIndex); // sorting right half
array[mid+1..lastIndex]
if
(a[mid].compareTo(a[mid + 1]) > 0)
merge(a, tempArray, startIndex, mid, lastIndex);
// merge the two arrays left and right
}
}
private static <T extends Comparable<? super
T>>
void merge(T[] a, T[] tempArray, int first, int mid,
int last)
{
// Two arrays left and right are
a[startingHalf1....endHalf1] and
a[startinghalf2....endHalf2].
int startingHalf1 = first;
int endHalf1 = mid;
int startinghalf2 = mid + 1;
int endHalf2 = last;
int index = startingHalf1;
for (; (startingHalf1 <=
endHalf1) && (startinghalf2 <= endHalf2); index++)
{
if
(a[startingHalf1].compareTo(a[startinghalf2]) < 0)
{
tempArray[index] = a[startingHalf1];
startingHalf1++;
}
else
{
tempArray[index] = a[startinghalf2];
startinghalf2++;
}
}
for (; startingHalf1 <=
endHalf1; startingHalf1++, index++)
tempArray[index] = a[startingHalf1];
for (; startinghalf2 <=
endHalf2; startinghalf2++, index++)
tempArray[index]
= a[startinghalf2];
// Finally copying the result to
original array
for (index = first; index <=
last; index++)
a[index] =
tempArray[index];
}
public static void main(String[] args)
{
Integer[]a={50,45,20,40,10,15,5,25,30,35,55};
System.out.println("The array before sorting is
");
for(int i = 0; i<a.length;i++)
System.out.print(a[i]+" ");
mergeSortA(a,a.length);
System.out.println("\nThe array after sorting is
");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
}
OUTPUT:
The array before sorting is
50 45 20 40 10 15 5 25 30 35 55
The array after sorting is
5 10 15 20 25 30 35 40 45 50 55
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...
Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...
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...
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++)...
Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...
4. (5 Points) The following is another merge sort top down implementation, what is the running time and space complexity for this implementation in big-0? Briefly explain your answer. public static 〈T extends Comparable〈 T> > void sort2(T[] a) { sort2(a, , a.length - 1); @Suppresswarnings ("unchecked") private static <T extends Comparable<T>> void sort2(ΤΠ a, intlo, int hi) { if (hi (z lo) return; T[] aux- (TLD new comparable[a,length]; int mid- (10 + hi) / 2; sort2 (a, lo, mid);...
I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...
Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...