Question

In: Computer Science

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 !!

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;

//QUICK SORT
int partition (int arr[], int low, int high)
{
   int pivot = arr[high];
   int i = (low - 1);

   for (int j = low; j <= high - 1; j++)
   {
       if (arr[j] < pivot)
       {
           i++;
           swap(arr[i],arr[j]);
       }
   }
   swap(arr[i + 1],arr[high]);
   return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
   if (low < high)
   {
       int pi = partition(arr, low, high);
       quickSort(arr, low, pi - 1);
       quickSort(arr, pi + 1, high);
   }
}

//HEAP SORT
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i=n-1; i>0; i--)
{
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

int main()
{
   cout<<"Enter the no of elements-\n";
   int n;
   cin>>n;
   cout<<"Enter the array's elements-\n";
   int arr[n+1];
   for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Press 1 for quick sort or 2 for heap sort-\n";
int ch;
cin>>ch;
if(ch==1)
{
quickSort(arr, 0, n - 1);
cout << "Sorted array after applying quick sort is: \n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
   else if(ch==2)
{
heapSort(arr, n);
cout << "Sorted array after applying heap sort is: \n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
else
cout<<"Invalid choice";
   return 0;
}

OUTPUT -


Related Solutions

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++
Implementation of Quick sort and heap sorting algorithms in C++
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?
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
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.
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort...
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort Under the following scenarios for input data: 1. Uniform random 2. Almost sorted (90% sorted – 1 in 10 is out of place) 3. Reverse sorted On data of sizes 5,000, 10,000, … in increments of 5,000 up to …, 50,000 -Attach a screenshot of a program compilation below -Attach a screenshot of a successful program run below -Attach a graph (either line graph...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input to the programs should be ASCII characters 2. Your program should be able to handle upper and lower case letters both 3. The sort should be done in a descending manner 4.Note: Please use array-based representation for these sorting algorithms Please write comment n explain each step clearly as well your program should show what you are taking as input array and what your...
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)...
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT