Question

In: Computer Science

C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...

C++ Question- Write function templates for 5 sort algorithms: - Quick Sort

Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.

Solutions

Expert Solution

// STAY HOME STAY SAFE

#include <iostream>
#include <stdlib.h>
using namespace std;
// function to partition the array
int partition (int arr[], int l, int h)
{
int p = arr[h];
int i = (l - 1);
// iterate from low to higt
for (int j = l; j <= h - 1; j++)
{
if (arr[j] < p)
{
// increment i
i++;
// swap arr[i] and arr[j]
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
// swap arr[i+1] and arr[h]
int temp = arr[i+1];
arr[i+1]=arr[h];
arr[h]=temp;
return (i + 1);
}
// the quick sort function
void QSort(int arr[], int l, int h)
{
if (l < h)
{
// find the pivot element
int pivot = partition(arr, l, h);
// sort before partition
QSort(arr, l, pivot - 1);
// sort after partition
QSort(arr, pivot + 1, h);
}
}
// main function
int main()
{
// run a loop 10 times for 10 arrays
for(int a=1;a<=10;a++)
{
// declare array os size 100
int arr[100];
// fill with random values
for(int i=0;i<100;i++)
{
arr[i]=rand()%100 + 1;
}
// display array before sorting
cout<<"Array "<<a<<" before quick sort...\n";
for(int i=0;i<100;i++)
{
cout<<arr[i]<<" ";
}
// call merge sort function
QSort(arr,0,99);
// display array after sorting
cout<<"\nArray "<<a<<" after quick sort...\n";
for(int i=0;i<100;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n\n";
}

return 0;
}


Related Solutions

Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAM PS: YOU ARE ANSEWRING THE SAME PROGRAMS I WANT DIFFERENT ONE PLEASE , THANK YOU . BECAUSE THE ONE WERE POSTING DOESNT WORKING !!
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Design two function templates as follows: - First function template will be used to sort arrays...
Design two function templates as follows: - First function template will be used to sort arrays of different data types in either descending or ascending order. This function template is required to have the following parameters: an array of a generic type, an integer representing the size of the array, and a character representing the order of sorting (i.e., ascending or descending). The last argument (i.e., the order of sorting) is required to be a default argument. The default order...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
What are templates in c++? How are they made? Write a program to explain function template.
What are templates in c++? How are they made? Write a program to explain function template.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT