
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
How would I implement the sort()?
For implementing a max heap, in Java?
![```java
public Heapsort(int arr[])
{
int n = arr.length;
this.arr=new int[n];
for (int i = 0; i<n; i++)
this.arr[i]=arr[i];
}
public void sort()
{
int n = arr.length;
// Build heap (rearrange array)
int startIndex = (n / 2) - 1;
// fill statements
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
//fill
heapify(arr, n, i);
//fill
// call max heapify on the reduced heap
//fill
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
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 save array of size n as a String */
public String toString()
{
String s=new String();
for (int i=0; i<arr.length; i++)
s=s + " " + arr[i];
return s;
}
```
**Explanation:**
This code defines a simple implementation of the Heap Sort algorithm in Java. Here’s a breakdown of the key components:
1. **Constructor `Heapsort`:**
- Initializes a new array `arr` from the input array.
2. **Method `sort`:**
-](https://content.bartleby.com/qna-images/question/346b36e0-a4d8-47d7-8bd1-1ad776cb1cf3/d2025966-4e8f-4356-bc69-35bc58981208/h5exqr_thumbnail.png)
Transcribed Image Text:```java
public Heapsort(int arr[])
{
int n = arr.length;
this.arr=new int[n];
for (int i = 0; i<n; i++)
this.arr[i]=arr[i];
}
public void sort()
{
int n = arr.length;
// Build heap (rearrange array)
int startIndex = (n / 2) - 1;
// fill statements
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
//fill
heapify(arr, n, i);
//fill
// call max heapify on the reduced heap
//fill
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
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 save array of size n as a String */
public String toString()
{
String s=new String();
for (int i=0; i<arr.length; i++)
s=s + " " + arr[i];
return s;
}
```
**Explanation:**
This code defines a simple implementation of the Heap Sort algorithm in Java. Here’s a breakdown of the key components:
1. **Constructor `Heapsort`:**
- Initializes a new array `arr` from the input array.
2. **Method `sort`:**
-
Expert Solution

arrow_forward
Step 1
Solution:
Given,
- Implementation of Head Sort in java and also implement the -
- heapSort, buildmax heap, and heapify functionality.
Step by stepSolved in 3 steps with 1 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Implement the solution for the N-Queens problem using stacks (required), Let the user define N (input) <= some Nmax (this should not affect the algorithm or its implementation details). You do not need to implement your own stack. Your code should have sufficient comments so a reader can understand your logic. The output should either be the queens locations {rows, columns}, or a graphical interface of the chess board with the queen locations (which can also be dynamic, during execution, showing the location changes). I will need the Your code files (.cpp, .h, etc.) A README.txt file showing how the users should execute/run your program A Makefile file that contains all the commands needed to compile your code on Tesla@cs. All the code will be tested on Tesla@cs with its g++. Your code should be able to be compiled by executing makearrow_forwardWrite a basic algorithm in Java that will use recursion to print out all the even elements in a stack.arrow_forwardWrite a Java program to implement a stack using an array. The program should have methods to push an element onto the stack, pop an element from the stack, and check if the stack is empty. Additionally, implement a method to return the size of the stack. Provide an explanation of the code and its time and space complexity.arrow_forward
- Whereas the unbounded stack has no such limits, could you perhaps elaborate on how this works?arrow_forwardImplement Stack (LIFO) using double linked list. Code in C.arrow_forwardHello, i need help with understanding Big-O. What is the execution time Big-O for each of the methods in the Queue-1.java interface for the ArrayQueue-1.java and CircularArrayQueue-1.java implementations? Short Big-O reminders: Big-O is a measure of the worst case performance (in this case execution time). O(1) is constant time (simple assignment statements) O(n) is a loop that iterates over all of the elements. O(n2) is one loop nested in another loop, each of which iterates over all the elements. If a method calls another method, what happens in the called method must be taken into consideration. Queue.java public interface Queue<E> { /** * The element enters the queue at the rear. */ public void enter(E element); /** * The front element leaves the queue and is returned. * @throws java.util.NoSuchElementException if queue is empty. */ public E leave(); /** * Returns True if the queue is empty. */ public boolean…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education