SlideShare a Scribd company logo
1 of 7
Download to read offline
Implement a function in c++ which takes in a vector of integers and also takes in another integer
d which we will refer to as depth. The function will recursively call quicksort while
decrementing depth until the depth has reached 0. Thenpoithe algorithm will switch over to
heapsort to finish the sorting of the vector. For this problem also comment in the code what the
value of d needs to with respect to the size of the vector n for the algorithm to have worst case
run time of O(n log(n)).
#include
#include
using namespace std;
void quicksort(vector & items)
{
if(items.size() > 1)
{
vector small;
vector same;
vector large;
int pivot = items[0];
for(int i = 1; i < items.size(); i++)
{
if(items[i] < pivot)
small.push_back(items[i]);
else if(items[i] > pivot)
large.push_back(items[i]);
else
same.push_back(items[i]);
}
quicksort(small);
quicksort(large);
std::move(begin(small),end(small),begin(items)); //moving the piece of memory that is
small to the items memory
std::move(begin(same),end(same),begin(items) + small.size());
std::move(begin(large),end(large),end(items) - large.size());
}
else
{
//Do nothing
}
}
int leftChild(int i)
{
return 2*i + 1;
}
void percDown(vector & v, int i, int n)
{
int child;
//int leftChild;
int temp;
for(temp = v[i]; leftChild(i) < n; i = child)
{
child = leftChild(i);
if(child != n-1 && v[child] < v[child + 1]) //compare children
{
child++; //We want our right child
}
if(temp < v[child])
{
v[i] = v[child]; //Swapping
v[child] = temp;
}
else
{
break;
}
}
}
void heapsort(vector & v)
{
for(int i = v.size() /2 -1; i >= 0; --i)
{
percDown(v, i, v.size()); //Build Heap
}
for(int j = v.size() - 1; j > 0; --j)
{
swap(v[0],v[j]);
percDown(v, 0, j); //Remove max
}
}
int main()
{
int a[] = {1,10,3,2,5,8};
vector v(a,a + sizeof(a)/sizeof(int));
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
cout << "Sorted " << endl;
quicksort(v);
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
heapsort(v);
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
mixSort(v)
for(int i = 0 i < v.size(); i++)
{
cout < }
int x;
cin >> x;
}
Solution
#include
#include
using namespace std;
void quicksort(vector & items)
{
if(items.size() > 1)
{
vector small;
vector same;
vector large;
int pivot = items[0];
for(int i = 1; i < items.size(); i++)
{
if(items[i] < pivot)
small.push_back(items[i]);
else if(items[i] > pivot)
large.push_back(items[i]);
else
same.push_back(items[i]);
}
quicksort(small);
quicksort(large);
std::move(begin(small),end(small),begin(items)); //moving the piece of memory that is
small to the items memory
std::move(begin(same),end(same),begin(items) + small.size());
std::move(begin(large),end(large),end(items) - large.size());
}
else
{
//Do nothing
}
}
int leftChild(int i)
{
return 2*i + 1;
}
//comment this method for your solution.....
void percDown(vector & v, int i, int n)
{
int child;
//int leftChild;
int temp;
for(temp = v[i]; leftChild(i) < n; i = child)
{
child = leftChild(i);
if(child != n-1 && v[child] < v[child + 1]) //compare children
{
child++; //We want our right child
}
if(temp < v[child])
{
v[i] = v[child]; //Swapping
v[child] = temp;
}
else
{
break;
}
}
}
void heapsort(vector & v)
{
for(int i = v.size() /2 -1; i >= 0; --i)
{
percDown(v, i, v.size()); //Build Heap
}
for(int j = v.size() - 1; j > 0; --j)
{
swap(v[0],v[j]);
percDown(v, 0, j); //Remove max
}
}
int main()
{
int a[] = {1,10,3,2,5,8};
vector v(a,a + sizeof(a)/sizeof(int));
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
cout << "Sorted " << endl;
quicksort(v);
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
heapsort(v);
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
mixSort(v)
for(int i = 0 i < v.size(); i++)
{
cout < }
int x;
cin >> x;
}

More Related Content

Similar to Implement a function in c++ which takes in a vector of integers and .pdf

Mythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDBMythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDBMongoDB
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptSaadAsim11
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfaathiauto
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++Jawad Khan
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsVishvjeet Yadav
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Presentation 2
Presentation 2Presentation 2
Presentation 2s2team
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codePVS-Studio LLC
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++nsm.nikhil
 

Similar to Implement a function in c++ which takes in a vector of integers and .pdf (20)

Mythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDBMythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDB
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Vector3
Vector3Vector3
Vector3
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Notes
NotesNotes
Notes
 
Array notes
Array notesArray notes
Array notes
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
 
C++ references
C++ referencesC++ references
C++ references
 

More from feelingspaldi

You have just been hired as an information security engineer for a l.pdf
You have just been hired as an information security engineer for a l.pdfYou have just been hired as an information security engineer for a l.pdf
You have just been hired as an information security engineer for a l.pdffeelingspaldi
 
Write an essay consists of 5 paragraphs (comparing between a city and.pdf
Write an essay consists of 5 paragraphs (comparing between a city and.pdfWrite an essay consists of 5 paragraphs (comparing between a city and.pdf
Write an essay consists of 5 paragraphs (comparing between a city and.pdffeelingspaldi
 
Why is Antitrust activity so harmful to consumers and the economy W.pdf
Why is Antitrust activity so harmful to consumers and the economy W.pdfWhy is Antitrust activity so harmful to consumers and the economy W.pdf
Why is Antitrust activity so harmful to consumers and the economy W.pdffeelingspaldi
 
Why should Emperor Conrad lead the crusades Use bible verse.Sol.pdf
Why should Emperor Conrad lead the crusades Use bible verse.Sol.pdfWhy should Emperor Conrad lead the crusades Use bible verse.Sol.pdf
Why should Emperor Conrad lead the crusades Use bible verse.Sol.pdffeelingspaldi
 
WHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdf
WHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdfWHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdf
WHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdffeelingspaldi
 
Which of the following are widely recognized disadvantages to the Eur.pdf
Which of the following are widely recognized disadvantages to the Eur.pdfWhich of the following are widely recognized disadvantages to the Eur.pdf
Which of the following are widely recognized disadvantages to the Eur.pdffeelingspaldi
 
What is the function of a rhizoid Describe asexual and sexual repro.pdf
What is the function of a rhizoid  Describe asexual and sexual repro.pdfWhat is the function of a rhizoid  Describe asexual and sexual repro.pdf
What is the function of a rhizoid Describe asexual and sexual repro.pdffeelingspaldi
 
We define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdf
We define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdfWe define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdf
We define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdffeelingspaldi
 
What does the cutting-plane line represent List and explain seven d.pdf
What does the cutting-plane line represent  List and explain seven d.pdfWhat does the cutting-plane line represent  List and explain seven d.pdf
What does the cutting-plane line represent List and explain seven d.pdffeelingspaldi
 
True or False The employment relationship that is typical of modern.pdf
True or False The employment relationship that is typical of modern.pdfTrue or False The employment relationship that is typical of modern.pdf
True or False The employment relationship that is typical of modern.pdffeelingspaldi
 
The following monthly data are taken from Ramirez Company at July 31.pdf
The following monthly data are taken from Ramirez Company at July 31.pdfThe following monthly data are taken from Ramirez Company at July 31.pdf
The following monthly data are taken from Ramirez Company at July 31.pdffeelingspaldi
 
State the reasons for the current drought in South Africa. Describe .pdf
State the reasons for the current drought in South Africa. Describe .pdfState the reasons for the current drought in South Africa. Describe .pdf
State the reasons for the current drought in South Africa. Describe .pdffeelingspaldi
 
Select the true statements concerning seeds.A Seeds allow for dor.pdf
Select the true statements concerning seeds.A Seeds allow for dor.pdfSelect the true statements concerning seeds.A Seeds allow for dor.pdf
Select the true statements concerning seeds.A Seeds allow for dor.pdffeelingspaldi
 
Russian dude. Formulated the ____ hypothesis regarding prebioisSo.pdf
Russian dude. Formulated the ____ hypothesis regarding prebioisSo.pdfRussian dude. Formulated the ____ hypothesis regarding prebioisSo.pdf
Russian dude. Formulated the ____ hypothesis regarding prebioisSo.pdffeelingspaldi
 
Robin Hartshorne• a biography of the mathematician• a descriptio.pdf
Robin Hartshorne• a biography of the mathematician• a descriptio.pdfRobin Hartshorne• a biography of the mathematician• a descriptio.pdf
Robin Hartshorne• a biography of the mathematician• a descriptio.pdffeelingspaldi
 
Question 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdf
Question 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdfQuestion 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdf
Question 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdffeelingspaldi
 
Position of birds eating sunflower seed is the niche dimension we.pdf
Position of birds eating sunflower seed is the niche dimension we.pdfPosition of birds eating sunflower seed is the niche dimension we.pdf
Position of birds eating sunflower seed is the niche dimension we.pdffeelingspaldi
 
Physical Security Plan1.Identify the role of environmental control.pdf
Physical Security Plan1.Identify the role of environmental control.pdfPhysical Security Plan1.Identify the role of environmental control.pdf
Physical Security Plan1.Identify the role of environmental control.pdffeelingspaldi
 
Part I. True or False Place a T or an F, whichever you consider cor.pdf
Part I. True or False Place a T or an F, whichever you consider cor.pdfPart I. True or False Place a T or an F, whichever you consider cor.pdf
Part I. True or False Place a T or an F, whichever you consider cor.pdffeelingspaldi
 
Module 03 Discussion - Musics Impact on Baby Boomers.pdf
Module 03 Discussion - Musics Impact on Baby Boomers.pdfModule 03 Discussion - Musics Impact on Baby Boomers.pdf
Module 03 Discussion - Musics Impact on Baby Boomers.pdffeelingspaldi
 

More from feelingspaldi (20)

You have just been hired as an information security engineer for a l.pdf
You have just been hired as an information security engineer for a l.pdfYou have just been hired as an information security engineer for a l.pdf
You have just been hired as an information security engineer for a l.pdf
 
Write an essay consists of 5 paragraphs (comparing between a city and.pdf
Write an essay consists of 5 paragraphs (comparing between a city and.pdfWrite an essay consists of 5 paragraphs (comparing between a city and.pdf
Write an essay consists of 5 paragraphs (comparing between a city and.pdf
 
Why is Antitrust activity so harmful to consumers and the economy W.pdf
Why is Antitrust activity so harmful to consumers and the economy W.pdfWhy is Antitrust activity so harmful to consumers and the economy W.pdf
Why is Antitrust activity so harmful to consumers and the economy W.pdf
 
Why should Emperor Conrad lead the crusades Use bible verse.Sol.pdf
Why should Emperor Conrad lead the crusades Use bible verse.Sol.pdfWhy should Emperor Conrad lead the crusades Use bible verse.Sol.pdf
Why should Emperor Conrad lead the crusades Use bible verse.Sol.pdf
 
WHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdf
WHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdfWHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdf
WHICH PERSON WOULD GENERLLY BE TREATED AS A MATERIAL PARTICIPANT IN .pdf
 
Which of the following are widely recognized disadvantages to the Eur.pdf
Which of the following are widely recognized disadvantages to the Eur.pdfWhich of the following are widely recognized disadvantages to the Eur.pdf
Which of the following are widely recognized disadvantages to the Eur.pdf
 
What is the function of a rhizoid Describe asexual and sexual repro.pdf
What is the function of a rhizoid  Describe asexual and sexual repro.pdfWhat is the function of a rhizoid  Describe asexual and sexual repro.pdf
What is the function of a rhizoid Describe asexual and sexual repro.pdf
 
We define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdf
We define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdfWe define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdf
We define a relation on set of Whole NumbersW={0,1,2,3…} As follow.pdf
 
What does the cutting-plane line represent List and explain seven d.pdf
What does the cutting-plane line represent  List and explain seven d.pdfWhat does the cutting-plane line represent  List and explain seven d.pdf
What does the cutting-plane line represent List and explain seven d.pdf
 
True or False The employment relationship that is typical of modern.pdf
True or False The employment relationship that is typical of modern.pdfTrue or False The employment relationship that is typical of modern.pdf
True or False The employment relationship that is typical of modern.pdf
 
The following monthly data are taken from Ramirez Company at July 31.pdf
The following monthly data are taken from Ramirez Company at July 31.pdfThe following monthly data are taken from Ramirez Company at July 31.pdf
The following monthly data are taken from Ramirez Company at July 31.pdf
 
State the reasons for the current drought in South Africa. Describe .pdf
State the reasons for the current drought in South Africa. Describe .pdfState the reasons for the current drought in South Africa. Describe .pdf
State the reasons for the current drought in South Africa. Describe .pdf
 
Select the true statements concerning seeds.A Seeds allow for dor.pdf
Select the true statements concerning seeds.A Seeds allow for dor.pdfSelect the true statements concerning seeds.A Seeds allow for dor.pdf
Select the true statements concerning seeds.A Seeds allow for dor.pdf
 
Russian dude. Formulated the ____ hypothesis regarding prebioisSo.pdf
Russian dude. Formulated the ____ hypothesis regarding prebioisSo.pdfRussian dude. Formulated the ____ hypothesis regarding prebioisSo.pdf
Russian dude. Formulated the ____ hypothesis regarding prebioisSo.pdf
 
Robin Hartshorne• a biography of the mathematician• a descriptio.pdf
Robin Hartshorne• a biography of the mathematician• a descriptio.pdfRobin Hartshorne• a biography of the mathematician• a descriptio.pdf
Robin Hartshorne• a biography of the mathematician• a descriptio.pdf
 
Question 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdf
Question 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdfQuestion 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdf
Question 7 Write the expression in expanded form. 81n x + In y O4 Inx.pdf
 
Position of birds eating sunflower seed is the niche dimension we.pdf
Position of birds eating sunflower seed is the niche dimension we.pdfPosition of birds eating sunflower seed is the niche dimension we.pdf
Position of birds eating sunflower seed is the niche dimension we.pdf
 
Physical Security Plan1.Identify the role of environmental control.pdf
Physical Security Plan1.Identify the role of environmental control.pdfPhysical Security Plan1.Identify the role of environmental control.pdf
Physical Security Plan1.Identify the role of environmental control.pdf
 
Part I. True or False Place a T or an F, whichever you consider cor.pdf
Part I. True or False Place a T or an F, whichever you consider cor.pdfPart I. True or False Place a T or an F, whichever you consider cor.pdf
Part I. True or False Place a T or an F, whichever you consider cor.pdf
 
Module 03 Discussion - Musics Impact on Baby Boomers.pdf
Module 03 Discussion - Musics Impact on Baby Boomers.pdfModule 03 Discussion - Musics Impact on Baby Boomers.pdf
Module 03 Discussion - Musics Impact on Baby Boomers.pdf
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Implement a function in c++ which takes in a vector of integers and .pdf

  • 1. Implement a function in c++ which takes in a vector of integers and also takes in another integer d which we will refer to as depth. The function will recursively call quicksort while decrementing depth until the depth has reached 0. Thenpoithe algorithm will switch over to heapsort to finish the sorting of the vector. For this problem also comment in the code what the value of d needs to with respect to the size of the vector n for the algorithm to have worst case run time of O(n log(n)). #include #include using namespace std; void quicksort(vector & items) { if(items.size() > 1) { vector small; vector same; vector large; int pivot = items[0]; for(int i = 1; i < items.size(); i++) { if(items[i] < pivot) small.push_back(items[i]); else if(items[i] > pivot) large.push_back(items[i]); else same.push_back(items[i]); } quicksort(small); quicksort(large); std::move(begin(small),end(small),begin(items)); //moving the piece of memory that is small to the items memory std::move(begin(same),end(same),begin(items) + small.size()); std::move(begin(large),end(large),end(items) - large.size());
  • 2. } else { //Do nothing } } int leftChild(int i) { return 2*i + 1; } void percDown(vector & v, int i, int n) { int child; //int leftChild; int temp; for(temp = v[i]; leftChild(i) < n; i = child) { child = leftChild(i); if(child != n-1 && v[child] < v[child + 1]) //compare children { child++; //We want our right child } if(temp < v[child]) { v[i] = v[child]; //Swapping v[child] = temp; } else { break; } } }
  • 3. void heapsort(vector & v) { for(int i = v.size() /2 -1; i >= 0; --i) { percDown(v, i, v.size()); //Build Heap } for(int j = v.size() - 1; j > 0; --j) { swap(v[0],v[j]); percDown(v, 0, j); //Remove max } } int main() { int a[] = {1,10,3,2,5,8}; vector v(a,a + sizeof(a)/sizeof(int)); for(int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; cout << "Sorted " << endl; quicksort(v); for(int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; heapsort(v); for(int i = 0; i < v.size(); i++) { cout << v[i] << endl; }
  • 4. mixSort(v) for(int i = 0 i < v.size(); i++) { cout < } int x; cin >> x; } Solution #include #include using namespace std; void quicksort(vector & items) { if(items.size() > 1) { vector small; vector same; vector large; int pivot = items[0]; for(int i = 1; i < items.size(); i++) { if(items[i] < pivot) small.push_back(items[i]); else if(items[i] > pivot) large.push_back(items[i]); else same.push_back(items[i]); } quicksort(small); quicksort(large); std::move(begin(small),end(small),begin(items)); //moving the piece of memory that is
  • 5. small to the items memory std::move(begin(same),end(same),begin(items) + small.size()); std::move(begin(large),end(large),end(items) - large.size()); } else { //Do nothing } } int leftChild(int i) { return 2*i + 1; } //comment this method for your solution..... void percDown(vector & v, int i, int n) { int child; //int leftChild; int temp; for(temp = v[i]; leftChild(i) < n; i = child) { child = leftChild(i); if(child != n-1 && v[child] < v[child + 1]) //compare children { child++; //We want our right child } if(temp < v[child]) { v[i] = v[child]; //Swapping v[child] = temp; } else
  • 6. { break; } } } void heapsort(vector & v) { for(int i = v.size() /2 -1; i >= 0; --i) { percDown(v, i, v.size()); //Build Heap } for(int j = v.size() - 1; j > 0; --j) { swap(v[0],v[j]); percDown(v, 0, j); //Remove max } } int main() { int a[] = {1,10,3,2,5,8}; vector v(a,a + sizeof(a)/sizeof(int)); for(int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; cout << "Sorted " << endl; quicksort(v); for(int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; heapsort(v);
  • 7. for(int i = 0; i < v.size(); i++) { cout << v[i] << endl; } mixSort(v) for(int i = 0 i < v.size(); i++) { cout < } int x; cin >> x; }