Question

In: Computer Science

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 output is. This should be a user input array. Do not declare your array already in the code. The user should input n output should be according to that.

Solutions

Expert Solution

QUICK SORT

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

// A utility function to swap two elements
void swap(char* a, char* b)
{
        char t = *a;
        *a = *b;
        *b = t;
}

/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (char arr[], int low, int high)
{
        char pivot = arr[high]; // pivot
        int i = (low - 1); // Index of smaller element

        for (int j = low; j <= high - 1; j++)
        {
                // If current element is smaller than the pivot
                if (arr[j] > pivot)
                {
                        i++; // increment index of smaller element
                        swap(&arr[i], &arr[j]);
                }
        }
        swap(&arr[i + 1], &arr[high]);
        return (i + 1);
}

/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(char arr[], int low, int high)
{
        if (low < high)
        {
                /* pi is partitioning index, arr[p] is now
                at right place */
                int pi = partition(arr, low, high);

                // Separately sort elements before
                // partition and after partition
                quickSort(arr, low, pi - 1);
                quickSort(arr, pi + 1, high);
        }
}

/* Function to print an array */
void printArray(char arr[], int size)
{
        int i;
        for (i = 0; i < size; i++)
                cout << arr[i] << " ";
        cout << endl;
}

// Driver Code
int main()
{

        int n ;
        cout << "Enter the number of characters to be sorted: ";
        cin >> n;
        char arr[n];
        cout << "Enter the characters to be sorted: ";
        for (int i = 0; i < n; ++i) {
                cin >> arr[i];
        }
        quickSort(arr, 0, n - 1);
        cout << "Sorted array: \n";
        printArray(arr, n);
        return 0;
}


OUTPUT:

HEAP SORT

// C++ program for implementation of Heap Sort 
#include <iostream> 

using namespace std; 

// To heapify a subtree rooted with node i which is 
// an index in arr[]. n is size of heap 
void heapify(char arr[], int n, int i) 
{ 
        int largest = i; // Initialize largest as root 
        int l = 2*i + 1; // left = 2*i + 1 
        int r = 2*i + 2; // right = 2*i + 2 

        // If left child is larger than root 
        if (l < n && arr[l] < arr[largest]) 
                largest = l; 

        // If right child is larger than largest so far 
        if (r < n && arr[r] < arr[largest]) 
                largest = r; 

        // If largest is not root 
        if (largest != i) 
        { 
                swap(arr[i], arr[largest]); 

                // Recursively heapify the affected sub-tree 
                heapify(arr, n, largest); 
        } 
} 

// main function to do heap sort 
void heapSort(char arr[], int n) 
{ 
        // Build heap (rearrange array) 
        for (int i = n / 2 - 1; i >= 0; i--) 
                heapify(arr, n, i); 

        // One by one extract an element from heap 
        for (int i=n-1; i>0; i--) 
        { 
                // Move current root to end 
                swap(arr[0], arr[i]); 

                // call max heapify on the reduced heap 
                heapify(arr, i, 0); 
        } 
} 

/* A utility function to print array of size n */
void printArray(char arr[], int n) 
{ 
        for (int i=0; i<n; ++i) 
                cout << arr[i] << " "; 
        cout << "\n"; 
} 

// Driver program 
int main() 
{ 
        int n ;
        cout << "Enter the number of characters to be sorted: ";
        cin >> n;
        char arr[n];
        cout << "Enter the characters to be sorted: ";
        for (int i = 0; i < n; ++i) {
                cin >> arr[i];
        }
        heapSort(arr, n);
        cout << "Sorted array: \n";
        printArray(arr, n);
        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++ 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++
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, please show the code and output Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1,...
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...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition()...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition() and quickSort(). You are expected to call swap() within partition(), to call partition() within quickSort(), and you are not expected to declare/ implement other additional functions nor change the main() function. OPTIONAL: If you don’t need/ want to use swap() in your implementation, that is fine. Just delete/ comment it. quickSort.cpp #include <iostream> using namespace std;    // A helper function to facilitate swapping...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT