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++ 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?
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT