Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i want flowchart of this code import java.util.Scanner; public class MinHeap { public static void main ( String [ ] args ) { int arrSize

i want flowchart of this code import java.util.Scanner;
public class MinHeap
{
public static void main(String[] args)
{
int arrSize =0;
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the array you want to construct: ");
arrSize = in.nextInt();
int A[]= new int[arrSize];
while(true)
{
System.out.println("1. Insert
"+
"2. Delete
"+
"3. Search
"+
"4. Print ( sorted array , the array size and sort time to the screen)
"+
"5. Quit");
long start =0;
int data =0;
System.out.print("Enter your choice: ");
switch(in.nextInt())
{
case 1:
start = System.currentTimeMillis();
for(int i = arrSize -1; i >=0; i--)
{
A[i]= i *10;
}
System.out.println("It took you "+(double)(System.currentTimeMillis()- start)/1000+" seconds to insert data.");
break;
case 2:
System.out.print("Enter the data to delete: ");
data = in.nextInt();
start = System.currentTimeMillis();
arrSize = delete(A, arrSize, data);
System.out.println("It took you "+(double)(System.currentTimeMillis()- start)/1000+" seconds to delete data.");
break;
case 3:
System.out.print("Enter the data to search: ");
data = in.nextInt();
start = System.currentTimeMillis();
search(A, arrSize, data);
System.out.println("It took you "+(double)(System.currentTimeMillis()- start)/1000+" seconds to search data.");
break;
case 4:
start = System.currentTimeMillis();
MinHeap ob = new MinHeap();
ob.sort(A);
System.out.println("It took you "+(double)(System.currentTimeMillis()- start)/1000+" seconds to sort data.");
start = System.currentTimeMillis();
printSortedArray(A);
System.out.println("It took you "+(double)(System.currentTimeMillis()- start)/1000+" seconds to print data.");
break;
case 5:
default:
System.exit(0);
}
}
}
private static void search(int[] A, int arrSize, int data)
{
for(int i =0; i < arrSize; i++)
{
if(A[i]== data)
{
System.out.println("Data["+ data +"] found at index["+ i +"].");
return;
}
}
}
private static int delete(int[] A, int arrSize, int data)
{
boolean isDelete = false;
for(int i =0; i < arrSize; i++)
{
if(A[i]== data)
{
isDelete = true;
arrSize--;
}
private void sort(int A[])
{
int n = A.length;
for (int i = n /2-1; i >=0; i--)
{
makeHeap(A, n, i);
}
for (int i = n -1; i >=0; i--)
{
int temporary = A[0];
A[0]= A[i];
A[i]= temporary;
makeHeap(A, i,0);
}
}
private void makeHeap(int A[], int n, int i)
{
int largest = i;
int left_child =2*i +1;
int right_child =2*i +2;
if (left_child < n && A[left_child]> A[largest])
{
largest = left_child;
}
if (right_child < n && A[right_child]> A[largest])
{
largest = right_child;
}
if (largest != i)
{
int swap = A[i];
A[i]= A[largest];
A[largest]= swap;
makeHeap(A, n, largest);
}
}
private static void printSortedArray(int A[])
{
System.out.print("Sorted array: ");
int n = A.length;
for (int i =0; i < n; ++i)
{
System.out.print(A[i]+"");
}
System.out.println("");
}
}

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

Acceptance of the key role of people in this process of adaptation.

Answered: 1 week ago

Question

preference for well defined job functions;

Answered: 1 week ago