SlideShare a Scribd company logo
1 of 4
Download to read offline
//Please fill in the code, which only need to add code in the "adjustHeap" function.
#ifndef HEAP_H
#define HEAP_H
#include
#include // std::out_of_range
using namespace std;
template
class Heap
{
private:
vector _items;
void buildHeap()
{
for (int i = _items.size() / 2; i >= 0; i--)
{
adjustHeap(i);
}
}
//MA TODO: Implement adjustHeap!
//Percolates the item specified at by index down into its proper location within a heap.
// Used for dequeue operations and array to heap conversions
void adjustHeap(int index)
{
// The ONLY code you'd need to do for this MA would be in this function call
}
public:
Heap()
{
}
Heap(const vector &unsorted)
{
for (int i = 0; i < unsorted.size(); i++)
{
_items.push(unsorted[i]);
}
buildHeap();
}
//Adds a new item to the heap
void insert(T item)
{
//calculate positions
int current_position = _items.size();
int parent_position = (current_position - 1) / 2;
//insert element (note: may get erased if we hit the WHILE loop)
_items.push_back(item);
//get parent element if it exists
T *parent = nullptr;
if (parent_position >= 0)
{
parent = &_items[parent_position];
}
//only continue if we have a non-null parent
if (parent != nullptr)
{
//bubble up
while (current_position > 0 && item < *parent)
{
_items[current_position] = *parent;
current_position = parent_position;
parent_position = (current_position - 1) / 2;
if (parent_position >= 0)
{
parent = &_items[parent_position];
}
}
//after finding the correct location, we can finally place our item
_items[current_position] = item;
}
}
//Returns the top-most item in our heap without actually removing the item from the heap
T& getFirst()
{
if( size() > 0 )
return _items[0];
else
throw std::out_of_range("No elements in Heap.");
}
//Removes the top-most item from the heap and returns it to the caller
T deleteMin()
{
int last_position = _items.size() - 1;
T last_item = _items[last_position];
T top = _items[0];
_items[0] = last_item;
_items.erase(_items.begin() + last_position);
//percolate down
adjustHeap(0);
return top;
}
// Returns true if heap is empty, false otherwise
bool isEmpty() const
{
return _items.size() == 0;
}
// Return size (N) of the Heap
int size() const
{
return _items.size();
}
// Simple debugging print out
void printAll() const
{
for(int i = 0; i < _items.size(); i++)
{
cout << " [x] Heap element [" << i << "]. key=" << _items[i] << endl;
}
}
};
#endif
Solution
void adjustHeap(int index) { int largest,left,right; Map.Entry temp = null;
while(true){ left = 2*index+1; right = 2*index+2; if(left < size &&
elements[index].getValue() < elements[left].getValue()){ largest = left;
if(right < size && elements[left].getValue() < elements[right].getValue()){ largest =
right; } } else if(right < size && elements[index].getValue() <
elements[right].getValue()){ largest = right; } else break; // swapping
temp = elements[index]; elements[index] = elements[largest];
elements[largest] = temp; index = largest; } } } }

More Related Content

Similar to Please fill in the code, which only need to add code in the adju.pdf

PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 PriorityQueue.cs   Jim Mischel using System; using Sy.pdf PriorityQueue.cs   Jim Mischel using System; using Sy.pdf
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
rajat630669
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
ssuser454af01
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfComplete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
americanopticalscbe
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
DIPESH30
 
This is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfThis is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdf
info334223
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
ItemNodeh include ltiostreamgt include ltstring.pdf
ItemNodeh    include ltiostreamgt include ltstring.pdfItemNodeh    include ltiostreamgt include ltstring.pdf
ItemNodeh include ltiostreamgt include ltstring.pdf
acmefit
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
ashokadyes
 

Similar to Please fill in the code, which only need to add code in the adju.pdf (20)

PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 PriorityQueue.cs   Jim Mischel using System; using Sy.pdf PriorityQueue.cs   Jim Mischel using System; using Sy.pdf
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdfComplete the classes shown below 1. The MinHeap Class Write necessa.pdf
Complete the classes shown below 1. The MinHeap Class Write necessa.pdf
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
 
This is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfThis is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdf
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
(C++ exercise) 3. Implement a circular, doubly linked list with a ha.docx
(C++ exercise) 3. Implement a circular, doubly linked list with a ha.docx(C++ exercise) 3. Implement a circular, doubly linked list with a ha.docx
(C++ exercise) 3. Implement a circular, doubly linked list with a ha.docx
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docx
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
ItemNodeh include ltiostreamgt include ltstring.pdf
ItemNodeh    include ltiostreamgt include ltstring.pdfItemNodeh    include ltiostreamgt include ltstring.pdf
ItemNodeh include ltiostreamgt include ltstring.pdf
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
 

More from footsmart1

Student Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdfStudent Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdf
footsmart1
 
Post your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdfPost your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdf
footsmart1
 
Linear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdfLinear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdf
footsmart1
 
Many nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdfMany nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdf
footsmart1
 
i need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdfi need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdf
footsmart1
 

More from footsmart1 (20)

Which of the following properties best describes pentoses Pentoses .pdf
Which of the following properties best describes pentoses  Pentoses .pdfWhich of the following properties best describes pentoses  Pentoses .pdf
Which of the following properties best describes pentoses Pentoses .pdf
 
What is going to be the output of the following program #include .pdf
What is going to be the output of the following program  #include   .pdfWhat is going to be the output of the following program  #include   .pdf
What is going to be the output of the following program #include .pdf
 
What competencies underpin successful management in implementing org.pdf
What competencies underpin successful management in implementing org.pdfWhat competencies underpin successful management in implementing org.pdf
What competencies underpin successful management in implementing org.pdf
 
What is frictionSolutionThe force that resist relative motion .pdf
What is frictionSolutionThe force that resist relative motion .pdfWhat is frictionSolutionThe force that resist relative motion .pdf
What is frictionSolutionThe force that resist relative motion .pdf
 
What happens to the nominal exchange rate when the USs rate increa.pdf
What happens to the nominal exchange rate when the USs rate increa.pdfWhat happens to the nominal exchange rate when the USs rate increa.pdf
What happens to the nominal exchange rate when the USs rate increa.pdf
 
What are the recent developments in corporate reporting practice .pdf
What are the recent developments in corporate reporting practice .pdfWhat are the recent developments in corporate reporting practice .pdf
What are the recent developments in corporate reporting practice .pdf
 
We have not seen any humming birds in this forest todayWe have not.pdf
We have not seen any humming birds in this forest todayWe have not.pdfWe have not seen any humming birds in this forest todayWe have not.pdf
We have not seen any humming birds in this forest todayWe have not.pdf
 
Verbal statementscannot be a probabilitythe event is very unlik.pdf
Verbal statementscannot be a probabilitythe event is very unlik.pdfVerbal statementscannot be a probabilitythe event is very unlik.pdf
Verbal statementscannot be a probabilitythe event is very unlik.pdf
 
The three major domains of li.pdf
The three major domains of li.pdfThe three major domains of li.pdf
The three major domains of li.pdf
 
There are two major groups of seed plants, gymnosperms (Exercise 24) .pdf
There are two major groups of seed plants, gymnosperms (Exercise 24) .pdfThere are two major groups of seed plants, gymnosperms (Exercise 24) .pdf
There are two major groups of seed plants, gymnosperms (Exercise 24) .pdf
 
The steps in the closing process are (1) close credit balances in re.pdf
The steps in the closing process are (1) close credit balances in re.pdfThe steps in the closing process are (1) close credit balances in re.pdf
The steps in the closing process are (1) close credit balances in re.pdf
 
The positron decay of ^15 O goes directly to the ground state of ^15 .pdf
The positron decay of ^15 O goes directly to the ground state of ^15 .pdfThe positron decay of ^15 O goes directly to the ground state of ^15 .pdf
The positron decay of ^15 O goes directly to the ground state of ^15 .pdf
 
The disease bullous pemphigoid results in the destruction of protein.pdf
The disease bullous pemphigoid results in the destruction of protein.pdfThe disease bullous pemphigoid results in the destruction of protein.pdf
The disease bullous pemphigoid results in the destruction of protein.pdf
 
Student Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdfStudent Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdf
 
QUESTION 8 1.00000 P Financial intermediaries are different from inve.pdf
QUESTION 8 1.00000 P Financial intermediaries are different from inve.pdfQUESTION 8 1.00000 P Financial intermediaries are different from inve.pdf
QUESTION 8 1.00000 P Financial intermediaries are different from inve.pdf
 
Post your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdfPost your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdf
 
Linear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdfLinear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdf
 
Many nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdfMany nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdf
 
implement the add() method for min-heappublic class Heap {    .pdf
implement the add() method for min-heappublic class Heap {    .pdfimplement the add() method for min-heappublic class Heap {    .pdf
implement the add() method for min-heappublic class Heap {    .pdf
 
i need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdfi need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
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
 
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
 

Recently uploaded (20)

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
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
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell Tolls
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
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...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

Please fill in the code, which only need to add code in the adju.pdf

  • 1. //Please fill in the code, which only need to add code in the "adjustHeap" function. #ifndef HEAP_H #define HEAP_H #include #include // std::out_of_range using namespace std; template class Heap { private: vector _items; void buildHeap() { for (int i = _items.size() / 2; i >= 0; i--) { adjustHeap(i); } } //MA TODO: Implement adjustHeap! //Percolates the item specified at by index down into its proper location within a heap. // Used for dequeue operations and array to heap conversions void adjustHeap(int index) { // The ONLY code you'd need to do for this MA would be in this function call } public: Heap() { } Heap(const vector &unsorted) { for (int i = 0; i < unsorted.size(); i++) {
  • 2. _items.push(unsorted[i]); } buildHeap(); } //Adds a new item to the heap void insert(T item) { //calculate positions int current_position = _items.size(); int parent_position = (current_position - 1) / 2; //insert element (note: may get erased if we hit the WHILE loop) _items.push_back(item); //get parent element if it exists T *parent = nullptr; if (parent_position >= 0) { parent = &_items[parent_position]; } //only continue if we have a non-null parent if (parent != nullptr) { //bubble up while (current_position > 0 && item < *parent) { _items[current_position] = *parent; current_position = parent_position; parent_position = (current_position - 1) / 2; if (parent_position >= 0) { parent = &_items[parent_position]; } } //after finding the correct location, we can finally place our item _items[current_position] = item; } }
  • 3. //Returns the top-most item in our heap without actually removing the item from the heap T& getFirst() { if( size() > 0 ) return _items[0]; else throw std::out_of_range("No elements in Heap."); } //Removes the top-most item from the heap and returns it to the caller T deleteMin() { int last_position = _items.size() - 1; T last_item = _items[last_position]; T top = _items[0]; _items[0] = last_item; _items.erase(_items.begin() + last_position); //percolate down adjustHeap(0); return top; } // Returns true if heap is empty, false otherwise bool isEmpty() const { return _items.size() == 0; } // Return size (N) of the Heap int size() const { return _items.size(); } // Simple debugging print out void printAll() const
  • 4. { for(int i = 0; i < _items.size(); i++) { cout << " [x] Heap element [" << i << "]. key=" << _items[i] << endl; } } }; #endif Solution void adjustHeap(int index) { int largest,left,right; Map.Entry temp = null; while(true){ left = 2*index+1; right = 2*index+2; if(left < size && elements[index].getValue() < elements[left].getValue()){ largest = left; if(right < size && elements[left].getValue() < elements[right].getValue()){ largest = right; } } else if(right < size && elements[index].getValue() < elements[right].getValue()){ largest = right; } else break; // swapping temp = elements[index]; elements[index] = elements[largest]; elements[largest] = temp; index = largest; } } } }