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++ 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 !!
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.
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT