Question

Please answer in Java for an immediate upvote :) Given the following array of 8 elements,...

Please answer in Java for an immediate upvote :)

  1. Given the following array of 8 elements, trace the merge sort algorithm. Assume the array is to be sorted in ascending order.

                       81          16          4        6             34          11          23        67

ANSWER: (Hint 6 – lines):

  1. Why don’t we select the median element of all the n-entries in the array to be our pivot point?

ANSWER:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION

TRACING ALGORITHM

81

16

4

6

34

11

23

67

16

81

4

6

34

11

23

67

4

6

16

81

34

11

23

67

4

6

16

81

11

34

23

67

4

6

16

81

11

23

34

67

4

6

11

16

23

34

67

81

Why don’t we select the median element of all the n-entries in the array to be our pivot point?

It will needrsorting the array & getting the value in the middle.

The main goal is to sorting the array,

If we select the element we have a chance to end up with circular logic

Program


class Main {
  
void merge(int array[], int left, int m, int right)
{
  
int num1 = m - left + 1;
int num2 = right - m;
  
  
int Left[] = new int[num1];
int Right[] = new int[num2];
  
  
for (int i = 0; i < num1; ++i)
Left[i] = array[left + i];
for (int j = 0; j < num2; ++j)
Right[j] = array[m + 1 + j];
  
  
  

int i = 0, j = 0;
  
  
int k = left;
while (i < num1 && j < num2) {
if (Left[i] <= Right[j]) {
array[k] = Left[i];
i++;
}
else {
array[k] = Right[j];
j++;
}
k++;
}
  

while (i < num1) {
array[k] = Left[i];
i++;
k++;
}
  
/* Copy remaining elements of Right[] if any */
while (j < num2) {
array[k] = Right[j];
j++;
k++;
}
}
  

void sort(int array[], int left, int right)
{
if (left < right) {
  
int m = (left + right) / 2;
  
  
sort(array, left, m);
sort(array, m + 1, right);
  

merge(array, left, m, right);
}
}
  
  
static void printarrayay(int array[])
{
int n = array.length;
for (int i = 0; i < n; ++i)
System.out.print(array[i] + " ");
System.out.println();
}
  

public static void main(String args[])
{
int array[] = { 81,16,4,6,34,11,23,67 };
  
System.out.println("Given arrayay");
printarrayay(array);
  
Main obj = new Main();
obj.sort(array, 0, array.length - 1);
  
System.out.println("\nSorted arrayay");
printarrayay(array);
}
}

Output

Add a comment
Know the answer?
Add Answer to:
Please answer in Java for an immediate upvote :) Given the following array of 8 elements,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • _______________________________________________________________________________________________ java language-trace instructions". (20 points) Show the contents of the array below, once the contents...

    _______________________________________________________________________________________________ java language-trace instructions". (20 points) Show the contents of the array below, once the contents of the array below, once the "pivot" element is placed at its location after each call of the "Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in ascending order (Trom Arrange the data in ascending order (from smallest to largest value). Always select the first element of the partition as "pivot" in data cat B. Apply sorting on...

  • Assume that you are sorting an array of 8 elements with quick sort. You just finished...

    Assume that you are sorting an array of 8 elements with quick sort. You just finished the first pass and the array looks like below. Which statement is true for the pivot value? 4 8 12 16 18 20 22 24 QUICKSORT ALGORITHM Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning). If the array is randomly ordered, it does not matter which element is the pivot. For simplicity, the first element...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Assume that the following 8 numbers are elements of an array (7pt). 8, 7, 6, 5,...

    Assume that the following 8 numbers are elements of an array (7pt). 8, 7, 6, 5, 4, 3, 2, 1. Sort the array in ascending order by applying Merge sort, and show how the array looks after each step. Is merge sort has every-case time complexity? If your answer is yes, what is its every-case time complexity? If your answer is no, what is its worst case time complexity and at what condition it will have worst-case time complexity? If...

  • Please answer by mathematical language: An array of n elements is almost sorted if and only...

    Please answer by mathematical language: An array of n elements is almost sorted if and only if every element is at most k spots away from its actual location. Assuming that you can only perform pairwise comparisons, formally prove that any algorithm which can sort almost sorted arrays must have running time Ω(n log k), You may assume that n is a multiple of k.

  • Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points;...

    Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points; 2 points for each part individual-only Important: When answering these questions, make sure to apply the versions of these algorithms that were discussed in lecture. The Java code for each algorithm can be found in our Sort class. Given the following array: {14, 7, 27, 13, 24, 20, 10, 33 1. If the array were sorted using selection sort, what would the array look...

  • the programming language is in java Problem 2 You are given an array A with n...

    the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...

  • JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...

    JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to...

  • Let us suppose that there are n elements in the un-sorted array. Answer the following? q1:...

    Let us suppose that there are n elements in the un-sorted array. Answer the following? q1: How is merge sort different from quick sort? q2: What is the split ratio in merge sort? q3: What is the worst-case/average-case/best-case running time of Merge Sort? q4: Why is the worst case running time of Merge sort O(n log n) always? q5: Why does Merge Sort use a static tree in the recursion process? (It is worth noting that the Quick Sort use...

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT