Question

q: Write a java code to Test the heap sort algorithm Test the algorithms on arrays...

q: Write a java code to Test the heap sort algorithm

  • Test the algorithms on arrays of size:

  • .100,000 , 90,000 ,80,000 ,70,000 ,60,000 ,50,000 ,40,000 ,30,000 ,20,000 ,10,000

  • Plot the execution time vs array size. without using excel

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

This is how time took to sort, time may vary based on system/platform and random number nad also below code provided where you can find TODO section for changing the array size and run the program.

100,000 -> 31 milliseconds

90,000 -> 28 milliseconds

80,000-> 26 milliseconds

70,000 -> 25 milliseconds

60,000 -> 24 milliseconds

50,000-> 21 milliseconds

40,000 -> 15 milliseconds

30,000 -> 13 milliseconds

20,000-> 10 milliseconds

10,000 -> 6 milliseconds

Plot generated

HeapSortTest.java

import java.util.Random;


public class HeapSortTest {
   
   /**
    * function to heapify the array
    * @param arr
    * @param i
    * @param sz
    */
   public static void _heapify_data(int arr[], int i, int sz) {
      
      int l = 2 * i + 1;
      int r = 2 * i + 2;
      
      
      int temp, temp_largest;
      
      if (l < sz && arr[l] > arr[i])
         temp_largest = l;
      else
         temp_largest = i;
      
      if (r < sz && arr[r] > arr[temp_largest])
         temp_largest = r;
      
      if (temp_largest != i) {
         temp = arr[temp_largest];
         arr[temp_largest] = arr[i];
         arr[i] = temp;
         
         _heapify_data(arr, temp_largest, sz);
      }
      
      
   }
   
   /**
    * helper function for build heap
    * @param arr
    */
   
   public static void _build_heap(int arr[]) {
      
      for (int i = (arr.length / 2) - 1; i >= 0; i--) {
         
         _heapify_data(arr, i, arr.length);
         
      }
      
   }
   
   /**
    * heap sort to data
    * @param arr
    */
   public static void heapSort(int arr[]) {
      int temp, j, i;
      
      _build_heap(arr);
      
      for (i = (arr.length) - 1; i > 0; ) {
         temp = arr[0];
         arr[0] = arr[i];
         arr[i] = temp;
         _heapify_data(arr, 0, i--);
         
      }
      
   }
   
   public static void main(String[] args) {
      // TODO: change the size of array as you want to test
      int size=10000;
      int arr[] = getArray(size);
      System.out.println("Sorting started .....");
      long starttime = System.currentTimeMillis();
      heapSort(arr);
      long endtime = System.currentTimeMillis();
      System.out.println("Sorting finished .....");
      System.out.printf("Time take taken for sorting "+size+" integers : %d milliseconds",(endtime-starttime));
   }
   
   /**
    * function to generate random array of given size
    * @param size
    * @return
    */
   public static int[] getArray(int size){
      System.out.println("Generating array of size : "+size);
      Random r = new Random();
      int arr[] = new int[size];
      for(int i=0;i<size;i++){
         arr[i] = r.nextInt(1000);
      }
      System.out.println("array generated.....\n");
      return arr;
   }
}

Please do let me know if u have any concern..

Add a comment
Know the answer?
Add Answer to:
q: Write a java code to Test the heap sort algorithm Test the algorithms on arrays...
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
  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

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

  • ---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers,...

    ---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers, strings and an array of Cars using each technique. At least seven items in each case.

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • Please write C++ code Description: As a senior software engineer, I would like to determine and...

    Please write C++ code Description: As a senior software engineer, I would like to determine and prove which sorting algorithm is the most efficient between quick sort, merge sort, and heap sort so that I can recommend it to the development team for inclusion in the code base of an upcoming code optimization project. Acceptance Criteria: Well documented, executable C++ source files including classes for each of the aforementioned sorting algorithms. In addition, each sorting algorithm class implementation should be...

  • Write a program in Java that performs the Counting Sort algorithm. a) Create the countingSort method....

    Write a program in Java that performs the Counting Sort algorithm. a) Create the countingSort method. Use the pseudocode shown in the lecture. b) Test your codes by writing a program that uses the following input array: int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2}; c) Your output should display the following versions of the arrays: I. The original input array A II. The counting array C after the counting (lines 4-5 in pseudocode)...

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

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