Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

To understand the value of recursion in a programming language, write a program that implements quicksort, first using recursion and then without recursion.

Expert Solution
Check Mark
Step 1

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;
}Computer Science homework question answer, step 1, image 1

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education