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 PROGRAMM

Solutions

Expert Solution

Implementation of Quick sort and heap sorting algorithms in C++--->

#include <iostream>
#define ll long long
using namespace std;
void swap(int *x, int *y) 
{ 
        int* t; 
        t=x; 
        x=y; 
        y=t; 
} 
int part(int *A,int start,int end) 
{ 
    /* function  to partition array and return true position of
    pivot element in array*/
        int pivot,pindex,i; 
         
        pivot=A[end]; 
        pindex=start; 
         
        for(i=start;i<end;i++) 
        { 
                if(A[i]<=pivot) 
                {        
                        swap(A[i],A[pindex]); 
                        pindex++; 
                } 
        } 
         
        swap(A[pindex],A[end]); 
         
        return pindex; 
} 

void quicksort(int *A,int start,int end) //complexity nlogn average case n^2 in worst case
{ 
        if(start<end) 
        { 
       /* pindex is partitioning index*/
                int pindex=part(A,start,end); 
                quicksort(A,start,pindex-1);  //recursive calling left
                quicksort(A,pindex+1,end); //recursive calling right
    }
}
void MaxHeap(int A[],int i,int n) //code to heapify or create maxheap
{ 
        int large=i; // initialize large with root index
        int lf=2*i; 
        int rf=2*i+1; 
        if(lf<n && A[lf]>A[large]) //if left child value > root value
                large=lf; 
        if(rf<n && A[rf]>A[large]) //if right child value > root value
                large=rf; 

        if(i!=large) 
        { 
                int s=A[large]; 
                A[large]=A[i]; 
                A[i]=s; 
        /*recursively heapify*/
                MaxHeap(A,large,n); 
        } 
} 

void heapSort(int arr[], int n) //complexity O(nlogn)
{ 
        int i; 
    /*creating heap*/
        for (i = n / 2 - 1; i >= 0; i--) 
                MaxHeap(arr,i,n);   
        for (i=n-1; i>=0; i--) 
        {   
        /* in each iteration 1 element is extracted*/
                int s=arr[0]; 
                arr[0]= arr[i]; 
                arr[i]=s;   
                MaxHeap(arr,0,i); 
        } 
} 

int main()
{
    /** taking input of size and sorting using quicksort*/
    int n;
    cin>>n;
   int a[n];
   for(int i=0;i<n;i++)
   cin>>a[i];
   quicksort(a,0,n-1);
   for(int i=0;i<n;i++)
   cout<<a[i]<<" ";

/*sort using heapsort*/
   int m;
   cin>>m;
   int b[m];
   for(int i=0;i<m;i++)
   cin>>b[i];
   heapSort(b,m);
   for(int i=0;i<m;i++)
   cout<<b[i]<<" ";
}

output


Related Solutions

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?
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...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
Part A Write a 6-8 sentence summary explaining how you can use the Heap Sort algorithms...
Part A Write a 6-8 sentence summary explaining how you can use the Heap Sort algorithms to solve a real world problem. In your summary, provide a descriptive or visual explanation of your heapsort solution. Part B For this portion of the assignment you will design an algorithm to implement the solution described in part A. Your algorithm must be written in pseudocode. Note: Sample HeapSort Algorithm Pseudocode: Heapsort(A as array) BuildHeap(A) for i = n to 1 swap(A[1], A[i])...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function compare that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an abstract class with a pure virtual member function void sort(int arr[ ], int size) which, when overridden, will sort the array by calling...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
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[ ] ).
What is the runtime for quick sort?
What is the runtime for quick sort?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT