
Concept explainers
To understand the value of recursion in a

First ,some fundamentals about quicksort,
1. It is similar to merge sort algorithm in which the array is being partitioned with respect to a pivot element
and the sorted recursively to get the final result.
2. I have used the function quickSort to recursively sort ,then I have used the partition function to implement the pivot concept ,then inbuilt swap function of C++.
3. I have given description on each side of code to get the idea.
#include <bits/stdc++.h>
using namespace std;
//this function takes last element as pivot element and keep it at its correct position in sorted array,then after this it places all smaller eleemnts to the left of pivot and greater to right of pivot
int partition (int arr[], int l, int h)
{
int pivot = arr[h]; // pivot
int i = (l - 1); // to get current right position of pivot
for (int j = l; j <= h- 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
swap(arr[i], arr[j]);//inbuilt swap function of C++
}
}
swap(arr[i + 1], arr[h]);//inbuilt swap function of C++
return (i + 1);
}
void quickSort(int arr[], int l, int h)
{
if (l < h)
{
//p is partitioning index from where the array will be divided into two parts
int p = partition(arr, l, h);
// Separately sort elements before partition
quickSort(arr, l, p - 1);
quickSort(arr, p + 1, h);// sort after partition
}
}
//to print the array
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};//example to make it clear
int n = 6;
quickSort(arr, 0, 5);
cout << "Quick Sort Implementation recursively : Following is the sorted array \n";
printArray(arr, n);
return 0;
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images

- How does the concept of recursion work in computer programming?arrow_forwardCreate a C++ programme that uses this recursive method to answer the Towers of Hanoi puzzle. It conveys the movements by showing them; this needs far less code than graphically presenting the towers. It is up to the person perusing the list to carry out the change.arrow_forwardHow much more time and memory does it take for a computer to execute a recursive function compared to a non-recursive one?arrow_forward
- What is the difference between a recursive and an iterative algorithm?arrow_forwardWhat is the overhead associated with the execution of a recursive function, both in terms of the amount of memory space and the amount of time needed by the computer, and how is it represented as a percentage?arrow_forwardPython language only Suppose you are working in the pizza company named Dominoes. Dominoes provides the best in class pizza in the world so While dealing with the coustomer you got a number on every billed amount. SO here your task is to identifiy that the billing amount is a magic number or not. A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number. Input : 1234Output : Yes it isInput : 12345Output : No it is notarrow_forward
- The term "dynamic programming" refers to a mathematical optimisation method that involves breaking down a complex problem into smaller subproblems and solving them recursively.arrow_forwardUnfortunately, the code itself executes but recursively in infinite loop. Is there a reason why it is doing that?arrow_forwardWhat are the benefits of using tail recursion?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





