SlideShare a Scribd company logo
1 of 5
Download to read offline
In inline heapsort we use only single array throughout the program to decrease the running time,
where as, in simple heap sort we use external data structure memory usage of more than O(1) at
a time.
Please consider the below inline heap program which prints out the running time, which is lesser
to the the simple heap sort.
//program
class heapsort
{
private final int array[]; // The array being sorted
heapsort(int array[], int N)
{
this.array = array;
int Last = N-1;
// its children are at elements 2*I+1 and 2*I+2.
// phase 1: form heap
// Construct heap bottom-up, starting with small trees just above leaves
// and coalescing into larger trees near the root.
for( int Top = Last/2; Top >= 0; Top-- )
{
adjust(Top, Last);
}
// phase 2: use heap to sort
// Move top element (largest) out of heap, swapping with last element
// and changing the heap boundary, until only one element remains.
while( Last > 0 )
{
swap(0, Last);
adjust(0, --Last);
}
}
/**
* adjust(Top, Last) adjusts the tree between Top and Last
**/
void adjust(int Top, int Last)
{
int TopVal = array[Top]; // Set aside top of heap
int Parent, Child;
for( Parent = Top; ; Parent = Child ) // Iterate down through tree
{
Child = 2*Parent+1; // Child means left child
if( Child > Last )
break; // Left child non-existent
if( Child+1 <= Last // Right child exists
&& array[Child] < array[Child+1] ) // and right child is larger
Child++; // Child is the larger child
if( TopVal >= array[Child] )
break; // Location for TopVal found
array[Parent] = array[Child]; // Move larger child up in tree
}
array[Parent] = TopVal; // Install TopVal in place
}
//swapping
void swap(int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args)
{
int N = 0; // number of elements in array
int array[] =
{43,65,12,89,11,5,1,88,123,999,233,18,553,332,144,98,452,7663,111,23,654,8735,998};
//initialising array
N=array.length;
System.err.println("Sorting started");
long startTime = System.nanoTime();
new heapsort(array, N); // sorting
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
for( int i = 0; i < N; i++ )
{
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Time in nanos"+totalTime);
}
}
//runn the above program for inline heap sort.
Solution
In inline heapsort we use only single array throughout the program to decrease the running time,
where as, in simple heap sort we use external data structure memory usage of more than O(1) at
a time.
Please consider the below inline heap program which prints out the running time, which is lesser
to the the simple heap sort.
//program
class heapsort
{
private final int array[]; // The array being sorted
heapsort(int array[], int N)
{
this.array = array;
int Last = N-1;
// its children are at elements 2*I+1 and 2*I+2.
// phase 1: form heap
// Construct heap bottom-up, starting with small trees just above leaves
// and coalescing into larger trees near the root.
for( int Top = Last/2; Top >= 0; Top-- )
{
adjust(Top, Last);
}
// phase 2: use heap to sort
// Move top element (largest) out of heap, swapping with last element
// and changing the heap boundary, until only one element remains.
while( Last > 0 )
{
swap(0, Last);
adjust(0, --Last);
}
}
/**
* adjust(Top, Last) adjusts the tree between Top and Last
**/
void adjust(int Top, int Last)
{
int TopVal = array[Top]; // Set aside top of heap
int Parent, Child;
for( Parent = Top; ; Parent = Child ) // Iterate down through tree
{
Child = 2*Parent+1; // Child means left child
if( Child > Last )
break; // Left child non-existent
if( Child+1 <= Last // Right child exists
&& array[Child] < array[Child+1] ) // and right child is larger
Child++; // Child is the larger child
if( TopVal >= array[Child] )
break; // Location for TopVal found
array[Parent] = array[Child]; // Move larger child up in tree
}
array[Parent] = TopVal; // Install TopVal in place
}
//swapping
void swap(int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args)
{
int N = 0; // number of elements in array
int array[] =
{43,65,12,89,11,5,1,88,123,999,233,18,553,332,144,98,452,7663,111,23,654,8735,998};
//initialising array
N=array.length;
System.err.println("Sorting started");
long startTime = System.nanoTime();
new heapsort(array, N); // sorting
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
for( int i = 0; i < N; i++ )
{
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Time in nanos"+totalTime);
}
}
//runn the above program for inline heap sort.

More Related Content

Similar to In inline heapsort we use only single array throughout the program t.pdf

4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
arshin9
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
Bianca Teşilă
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdf
arihantstoneart
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
lohithkart
 
please tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdfplease tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdf
agarshailenterprises
 
Merge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdfMerge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdf
feelinggifts
 
Here is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfHere is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdf
manjan6
 

Similar to In inline heapsort we use only single array throughout the program t.pdf (20)

In Java code, implement the HeapSort algorithm. Your function must t.pdf
In Java code, implement the HeapSort algorithm. Your function must t.pdfIn Java code, implement the HeapSort algorithm. Your function must t.pdf
In Java code, implement the HeapSort algorithm. Your function must t.pdf
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 
Ds sorting
Ds sortingDs sorting
Ds sorting
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
please tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdfplease tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdf
 
Merge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdfMerge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdf
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Heapsort using Heap
Heapsort using HeapHeapsort using Heap
Heapsort using Heap
 
Heaps
HeapsHeaps
Heaps
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
Here is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfHere is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdf
 

More from anuragperipheral

what ways are quorum sensing and the two component signaling system .pdf
what ways are quorum sensing and the two component signaling system .pdfwhat ways are quorum sensing and the two component signaling system .pdf
what ways are quorum sensing and the two component signaling system .pdf
anuragperipheral
 
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
 Average.javaimport java.util.Scanner;   import java.util.Arra.pdf Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
anuragperipheral
 
Environmental contaminants of recent concern are .pdf
                     Environmental contaminants of recent concern are .pdf                     Environmental contaminants of recent concern are .pdf
Environmental contaminants of recent concern are .pdf
anuragperipheral
 
To insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdfTo insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdf
anuragperipheral
 

More from anuragperipheral (20)

yesbnoSolutionyesbno.pdf
yesbnoSolutionyesbno.pdfyesbnoSolutionyesbno.pdf
yesbnoSolutionyesbno.pdf
 
y = 22Solutiony = 22.pdf
y = 22Solutiony = 22.pdfy = 22Solutiony = 22.pdf
y = 22Solutiony = 22.pdf
 
what ways are quorum sensing and the two component signaling system .pdf
what ways are quorum sensing and the two component signaling system .pdfwhat ways are quorum sensing and the two component signaling system .pdf
what ways are quorum sensing and the two component signaling system .pdf
 
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
 Average.javaimport java.util.Scanner;   import java.util.Arra.pdf Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
Average.javaimport java.util.Scanner;   import java.util.Arra.pdf
 
C. 0.20 M NaCl Solution C..pdf
  C. 0.20 M NaCl                                      Solution  C..pdf  C. 0.20 M NaCl                                      Solution  C..pdf
C. 0.20 M NaCl Solution C..pdf
 
yep in water which is higly polar will dissolve i.pdf
                     yep in water which is higly polar will dissolve i.pdf                     yep in water which is higly polar will dissolve i.pdf
yep in water which is higly polar will dissolve i.pdf
 
When thinking of E or Z, the first thing that com.pdf
                     When thinking of E or Z, the first thing that com.pdf                     When thinking of E or Z, the first thing that com.pdf
When thinking of E or Z, the first thing that com.pdf
 
Since it contains oxygen double bonded to a carbo.pdf
                     Since it contains oxygen double bonded to a carbo.pdf                     Since it contains oxygen double bonded to a carbo.pdf
Since it contains oxygen double bonded to a carbo.pdf
 
salicylic acid alcohol (R-OH) and carboxylic aci.pdf
                     salicylic acid alcohol (R-OH) and carboxylic aci.pdf                     salicylic acid alcohol (R-OH) and carboxylic aci.pdf
salicylic acid alcohol (R-OH) and carboxylic aci.pdf
 
no it doesnot .pdf
                     no it doesnot                                    .pdf                     no it doesnot                                    .pdf
no it doesnot .pdf
 
It would be Ruthenium (II) Sulfide Ruthenium is .pdf
                     It would be Ruthenium (II) Sulfide  Ruthenium is .pdf                     It would be Ruthenium (II) Sulfide  Ruthenium is .pdf
It would be Ruthenium (II) Sulfide Ruthenium is .pdf
 
ketones do not give tollens test.. hence no react.pdf
                     ketones do not give tollens test.. hence no react.pdf                     ketones do not give tollens test.. hence no react.pdf
ketones do not give tollens test.. hence no react.pdf
 
for IONIC the metal name followed by nonmetal io.pdf
                     for IONIC the metal name followed by nonmetal io.pdf                     for IONIC the metal name followed by nonmetal io.pdf
for IONIC the metal name followed by nonmetal io.pdf
 
pH = 4-log2.4 = 3.62 pOH 14 - pH = 14-3.62 = 10.3.pdf
                     pH = 4-log2.4 = 3.62 pOH 14 - pH = 14-3.62 = 10.3.pdf                     pH = 4-log2.4 = 3.62 pOH 14 - pH = 14-3.62 = 10.3.pdf
pH = 4-log2.4 = 3.62 pOH 14 - pH = 14-3.62 = 10.3.pdf
 
This suggest that dissociation of FeCl3 is 100 .pdf
                     This suggest that dissociation of FeCl3 is 100  .pdf                     This suggest that dissociation of FeCl3 is 100  .pdf
This suggest that dissociation of FeCl3 is 100 .pdf
 
Total probability of dot received P = (37)(78).pdf
                     Total probability of dot received P = (37)(78).pdf                     Total probability of dot received P = (37)(78).pdf
Total probability of dot received P = (37)(78).pdf
 
Environmental contaminants of recent concern are .pdf
                     Environmental contaminants of recent concern are .pdf                     Environmental contaminants of recent concern are .pdf
Environmental contaminants of recent concern are .pdf
 
C-H polar covalent bond Solution .pdf
                     C-H polar covalent bond Solution             .pdf                     C-H polar covalent bond Solution             .pdf
C-H polar covalent bond Solution .pdf
 
To insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdfTo insert an element at given position in arrayInsert(stack s1, s.pdf
To insert an element at given position in arrayInsert(stack s1, s.pdf
 
Their physical properties are so dofferent just because theintermole.pdf
Their physical properties are so dofferent just because theintermole.pdfTheir physical properties are so dofferent just because theintermole.pdf
Their physical properties are so dofferent just because theintermole.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 

In inline heapsort we use only single array throughout the program t.pdf

  • 1. In inline heapsort we use only single array throughout the program to decrease the running time, where as, in simple heap sort we use external data structure memory usage of more than O(1) at a time. Please consider the below inline heap program which prints out the running time, which is lesser to the the simple heap sort. //program class heapsort { private final int array[]; // The array being sorted heapsort(int array[], int N) { this.array = array; int Last = N-1; // its children are at elements 2*I+1 and 2*I+2. // phase 1: form heap // Construct heap bottom-up, starting with small trees just above leaves // and coalescing into larger trees near the root. for( int Top = Last/2; Top >= 0; Top-- ) { adjust(Top, Last); } // phase 2: use heap to sort // Move top element (largest) out of heap, swapping with last element // and changing the heap boundary, until only one element remains. while( Last > 0 ) { swap(0, Last); adjust(0, --Last); } } /** * adjust(Top, Last) adjusts the tree between Top and Last
  • 2. **/ void adjust(int Top, int Last) { int TopVal = array[Top]; // Set aside top of heap int Parent, Child; for( Parent = Top; ; Parent = Child ) // Iterate down through tree { Child = 2*Parent+1; // Child means left child if( Child > Last ) break; // Left child non-existent if( Child+1 <= Last // Right child exists && array[Child] < array[Child+1] ) // and right child is larger Child++; // Child is the larger child if( TopVal >= array[Child] ) break; // Location for TopVal found array[Parent] = array[Child]; // Move larger child up in tree } array[Parent] = TopVal; // Install TopVal in place } //swapping void swap(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String[] args) { int N = 0; // number of elements in array int array[] = {43,65,12,89,11,5,1,88,123,999,233,18,553,332,144,98,452,7663,111,23,654,8735,998}; //initialising array N=array.length; System.err.println("Sorting started");
  • 3. long startTime = System.nanoTime(); new heapsort(array, N); // sorting long endTime = System.nanoTime(); long totalTime = endTime - startTime; for( int i = 0; i < N; i++ ) { System.out.print(array[i] + " "); } System.out.println(); System.out.println("Time in nanos"+totalTime); } } //runn the above program for inline heap sort. Solution In inline heapsort we use only single array throughout the program to decrease the running time, where as, in simple heap sort we use external data structure memory usage of more than O(1) at a time. Please consider the below inline heap program which prints out the running time, which is lesser to the the simple heap sort. //program class heapsort { private final int array[]; // The array being sorted heapsort(int array[], int N) { this.array = array; int Last = N-1; // its children are at elements 2*I+1 and 2*I+2. // phase 1: form heap // Construct heap bottom-up, starting with small trees just above leaves // and coalescing into larger trees near the root.
  • 4. for( int Top = Last/2; Top >= 0; Top-- ) { adjust(Top, Last); } // phase 2: use heap to sort // Move top element (largest) out of heap, swapping with last element // and changing the heap boundary, until only one element remains. while( Last > 0 ) { swap(0, Last); adjust(0, --Last); } } /** * adjust(Top, Last) adjusts the tree between Top and Last **/ void adjust(int Top, int Last) { int TopVal = array[Top]; // Set aside top of heap int Parent, Child; for( Parent = Top; ; Parent = Child ) // Iterate down through tree { Child = 2*Parent+1; // Child means left child if( Child > Last ) break; // Left child non-existent if( Child+1 <= Last // Right child exists && array[Child] < array[Child+1] ) // and right child is larger Child++; // Child is the larger child if( TopVal >= array[Child] ) break; // Location for TopVal found array[Parent] = array[Child]; // Move larger child up in tree } array[Parent] = TopVal; // Install TopVal in place }
  • 5. //swapping void swap(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String[] args) { int N = 0; // number of elements in array int array[] = {43,65,12,89,11,5,1,88,123,999,233,18,553,332,144,98,452,7663,111,23,654,8735,998}; //initialising array N=array.length; System.err.println("Sorting started"); long startTime = System.nanoTime(); new heapsort(array, N); // sorting long endTime = System.nanoTime(); long totalTime = endTime - startTime; for( int i = 0; i < N; i++ ) { System.out.print(array[i] + " "); } System.out.println(); System.out.println("Time in nanos"+totalTime); } } //runn the above program for inline heap sort.