SlideShare a Scribd company logo
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 MongoDB
MongoDB
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
Yung-Yu Chen
 
Vector3
Vector3Vector3
Vector3
Rajendran
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
SaadAsim11
 
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
aathiauto
 
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 commands
Vishvjeet 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 C
Steffen Wenz
 
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++
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
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++ code
PVS-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 - Reference
Mohammed 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
 
C++ references
C++ referencesC++ references
C++ references
corehard_by
 

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.pdf
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 
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
feelingspaldi
 

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

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

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; }