Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2 of 2 Q2. Write the following functions: (you can reuse some of the methods in the maxheap class we implemented) 1. BuildMax Heap int

image text in transcribed
2 of 2 Q2. Write the following functions: (you can reuse some of the methods in the maxheap class we implemented) 1. BuildMax Heap int [): which takes an unordered array, convert it to a heap and return the maxheap array. Note that you will not have a current size variable as in the heap class. You array is full with elements (ie.currentSize = array length). 2. heapsort(): which uses buildMax Heap), then sort and return the array 3. simpleSort(): which is a simple sorting algorithm that compares every two adjacent elements (every two elements next to each other) and swap if needed until the array is sorted. It starts with the element at index O up to the last element in the array, Example: given the array(5,1.4.2.8) First Pass: (51428)-> (154 28). Here, algorithm compares the first two elements. and swaps since 5 > 1 (15428)-> (14528). Swap since 5 > 4 (14 528)-> (14258 ) Swap since 5 > 2 (14258->(14258). Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass: (142 58 )-> (14258) (14 258 )-> (12458), Swap since 4 > 2 (12458)-> (12458) (12458)-> (12458) Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted Third Pass: (12458)-> (12458) (12458)-> (12458) (12458)-> (12458) (12458)-> (12458 In your main: Create an array of integers and fill it with random 100,000 numbers. Create a copy of the same array (you can use arraycopyl). For example: import java.lang.Systen; intl) a - (10,15,3,5,12,8,89,55,33); int i lb = new int[a.length]; System.arraycopyca, e, b, e, a.length); Use heapsort() and simpleSort() to sort the array, Pass the original array to the first function and the copy to the second function, Log and print the time taken by each function, Note: Test your code first with a small array size, make sure your sorting functions work by printing the sorted array Remove the printing statements and run your code with a very large array If your machine was not able to handle 100.000 elements, run your code with an array of size 10,000, or 1000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago