Using Java Eclipse with comments:
The main purpose of this programming assignment is for students to have a deep understanding of how to implement Heapsort algorithm.
1.1 Details
l Implement Heapsort algorithm; run the program on two different manners:
1) Ask the user to select a number (the array size x); also ask the user to provide all x elements in the user interface; display this user-supplied array BOTH before AND after sorting.
2) Ask the user to select an array from a list of pre-defined arrays (generated by your program); display this pre-defined array BOTH before AND after sorting.
3) The interface will keep waiting for the user selection (between the above two manners) until the user chooses to exit.
Hi friend, Question is long.
I have answered Part I
import java.util.Arrays;
import java.util.Scanner;
//Java program for implementation of Heap Sort
public class HeapSort
{
public static void sort(int arr[])
{
int n = arr.length;
// Build heap (rearrange
array)
for (int i = n / 2 - 1; i >= 0;
i--)
heapify(arr, n,
i);
// One by one extract an element
from heap
for (int i=n-1; i>=0; i--)
{
// Move current
root to end
int temp =
arr[0];
arr[0] =
arr[i];
arr[i] =
temp;
// call max
heapify on the reduced heap
heapify(arr, i,
0);
}
}
// To heapify a subtree rooted with node i which
is
// an index in arr[]. n is size of heap
static void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize
largest as root
int l = 2*i + 1; // left = 2*i +
1
int r = 2*i + 2; // right = 2*i +
2
// If left child is larger than
root
if (l < n && arr[l] >
arr[largest])
largest = l;
// If right child is larger than
largest so far
if (r < n && arr[r] >
arr[largest])
largest = r;
// If largest is not root
if (largest != i)
{
int swap =
arr[i];
arr[i] =
arr[largest];
arr[largest] =
swap;
//
Recursively heapify the affected sub-tree
heapify(arr, n,
largest);
}
}
/* A utility function to print array of size n
*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
Scanner sc = new
Scanner(System.in);
System.out.print("How many
integers want to enter: ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter "+n+" numbers: ");
for(int i=0; i<n; i++)
arr[i] =
sc.nextInt();
System.out.println("Array before
sorting: ");
System.out.println(Arrays.toString(arr));
sort(arr);
System.out.println("Array after
sorting: ");
System.out.println(Arrays.toString(arr));
}
}
![Problems @ Javadoc DeclarationSearchoProgressRemote Systems TeStNG «terminated» Heapsort [Java Application] /Library/Java/Jav](http://img.homeworklib.com/questions/6cd16f40-8372-11eb-8b9c-759e5e5ef725.png?x-oss-process=image/resize,w_560)
Please rate my answer if it helped you!!
Using Java Eclipse with comments: The main purpose of this programming assignment is for students to...
does anyone know how to do this? C++
Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...
MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...
I have to use java programs using netbeans. this
course is introduction to java programming so i have to write it in
a simple way using till chapter 6 (arrays) you can use (loops ,
methods , arrays)
You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...
Using Java
In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...
ll
this is a java code do it in eclipse please. thank you.
Lab Objectives This lab was designed to reinforce programming concepts from this lab, you will practice: • Declaring and initializing arrays. • Comparing input to array elements. • Preventing array out-of-bounds errors. The follow-up questions and activities will also give you practice: • Initializing array sizes during program execution. • Generalizing programs. Description Use a one-dimensional array to solve the following problem: Write an application that inputs...
[JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and methods must be used for this program. Process: •Read the number of students in a class. Make sure that the user enters 1 or more students. •Create an array with the number of students entered by the user. •Then, read the name and the test score for each student. •Calculate the best or highest score. •Then, Calculate letter grades based on the following criteria:...
Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...
in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...