Question

(In Java) Do Shellsort twice, first using the suboptimal sequence for h which starts at half...

(In Java) Do Shellsort twice, first using the suboptimal sequence for h which starts at half the length of the array and is halved for each pass. Then use Knuth’s interval sequence 1, 4, 13, 40 … for values of h.

For each sort, print the results in a table for:

a. How much time it took.

b. How many comparisons it made between two values.

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

am providing only sorting of elements code

public class ShellSort
{
private long[] item;

private int length;

public ShellSort(int max)
{
item = new long[max];
length = 0;
}

public void insert(long value)
{ item[length] = value;
length++;
}

public void display() {
System.out.print("Item:");
for (int j = 0; j < length; j++)
System.out.print(item[j] + " ");
System.out.println("");
}

public void shellSort() {
int inner, outer;
long temp;

int h = 1;
while (h <= length / 3)
h = h * 3 + 1;

while (h > 0)
{
  
for (outer = h; outer < length; outer++) {
temp = data[outer];
inner = outer;
  
while (inner > h - 1 && item[inner - h] >= temp) {
item[inner] = item[inner - h];
inner -= h;
}
item[inner] = temp;
}
h = (h - 1) / 3;
}
}

public static void main(String[] args)
{
int maxSize = 10;
ShellSort arr = new ShellSort(maxSize);

for (int j = 0; j < maxSize; j++) {
long n = (int) (java.lang.Math.random() * 99);
arr.insert(n);
}
arr.display();
arr.shellSort();
arr.display();
}
}

Add a comment
Know the answer?
Add Answer to:
(In Java) Do Shellsort twice, first using the suboptimal sequence for h which starts at half...
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
  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Someone help me with the following questions using java Canvas x New Tab Help Center Submitted...

    Someone help me with the following questions using java Canvas x New Tab Help Center Submitted Dec 3 at T0:06pm This attempt took 3 minutes. Question 1 0/0.5 Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorit Index 0 Value 12 18 4, 9, 12.2.6,8.18 9, 4, 12, 2,6,18,8 2,4,9,12, 6, 8. 18 2. 4, 6, 8, 9,12,18 2. 4, 12,9.6,8,18 0/0.5 pts Question 2 the array after...

  • Molecular Bio lab. HELP!! Here is the first part: the sequence traces and the entire sequence....

    Molecular Bio lab. HELP!! Here is the first part: the sequence traces and the entire sequence. i just need the last 3 tasks. i color coded the ends so you can see where it overlaps and connects In the files section for your group there is a simulated output from an automated DNA sequencer using a variation of the classic Sanger method. (If you want to print it, it is formatted for legal sized paper.) This sequence encodes a protein...

  • ANSWER USING JAVA CODE (1)The sum of the squares of the first ten natural numbers is,...

    ANSWER USING JAVA CODE (1)The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. Find the difference between the...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • Part 1 The function 'vbrf' (for very bad recursive function) is defined as follows: vbrf(0) =...

    Part 1 The function 'vbrf' (for very bad recursive function) is defined as follows: vbrf(0) = 2 vbrf(1) = 3 vbrf(2) = 4 vbrf(3) = 5 vbrf(n) = vbrf(n-1) - vbrf(n-2) + 3*vbrf(n-3) - 2*vbrf(n-4) if n > 3 Your job for this problem is to implement the function vbrf as a method in Java. Then write a program that uses this method to compute the function for various values of the argument and time how long it takes to...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an...

    using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an array manipulation program that allows the user is to pre much whatever meant to ananas Wananching the program the user will passat are the contains a set of value and that w 2 Check to see the could be opened at the program was not opened the continue 3. Create an array and with the values from Presente en de h and powermany reded...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    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)...

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