Question

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

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

Homework Answers

Answer #1

The implimentation of quick sort is :

the partition method shifts every element geater than the pivot element to right of pivot and every smaller element to left of pivot.

the quicksort method finds the partition element and then recursively calls itself on left and right subarray

the code is:

#include <iostream>
using namespace std;


void swap(int *x, int *y) {
  int temp = *x;
  *x = *y;
  *y = temp;
}

// Function to print eklements of an array
void print(int array[], int n) {
  int i;
  for (i = 0; i < n; i++)
    cout << array[i] << " ";
  cout << endl;
}

// Function that partitions array based on pivot
int partition(int array[], int l, int h) {
  // Selecting pivot
  int p = array[h];
  int i = (l - 1);

  // Puts smaller element than pivot to left
  // and greater elements to right of pivot
  for (int j = l; j < h; j++) {
    if (array[j] <= p) {
      i++;
      swap(&array[i], &array[j]);
    }
  }
  swap(&array[i + 1], &array[h]);
  return (i + 1);
}

void quickSort(int array[], int l, int h) {
  if (l < h) {
    // Selects ppivot and puts smaller element to left of pivot and greater elements to right of pivot
    int pi = partition(array, l, h);

    // Sorts  elements on the left of pivot
    quickSort(array, l, pi - 1);

    // Sorts elements right of pivot
    quickSort(array, pi + 1, h);
  }
}


int main() {
  int arr[] = {8, 7, 6, 1, 0, 9, 2};
  int n = sizeof(arr) / sizeof(arr[0]);
  quickSort(arr, 0, n - 1);
  cout << "Sorted array in ascending order: \n";
  print(arr, n);
}

The code for heapsort is:

using namespace std;

void heapify(int arr[], int n, int i)
{
    int largest = i; // Initially largest is root
    int left = 2*i + 1; // left child = 2*i + 1
    int right = 2*i + 2; // right child= 2*i + 2

    // If left child is greater
    if (left < n && arr[left] > arr[largest])
        largest = left;

    // If right child is larger
    if (right < n && arr[right] > arr[largest])
        largest = right;

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

        // Recursively fix the position of largest
        heapify(arr, n, largest);
    }
}

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

    // extract an element from heap
    for (int i=n-1; i>0; i--)
    {
        // put the element to the end of heap
        swap(arr[0], arr[i]);

        heapify(arr, i, 0);
    }
}

void print(int arr[], int n)
{
    for (int i=0; i<n; ++i)
        cout << arr[i] << " ";
    cout << "\n";
}

int main()
{
    int arr[] = {8, 7, 6, 1, 0, 9, 2};
    int n = sizeof(arr)/sizeof(arr[0]);

    heapSort(arr, n);

    cout << "array in sorted order is \n";
    print(arr, n);
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The heap sort algorithm begins by converting the list into a heap, then sorting. True or...
The heap sort algorithm begins by converting the list into a heap, then sorting. True or False
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI...
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI input message, then prompt user to enter numbers in GUI input message also, sort these numbers in descending order using Selected sorting algorithm, and show the result in message dialog as shown in the following figure. The program contains two classes: 1- “Sort” Class which contains 3 methods named as follow: a. InsertionSort(int[] numbers), returns sorted numbers from the passed array using Insertion sort...
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort...
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot = first index) algorithms. Compute the CPU processing time for all the algorithms for varying input sizes as follows: N = 102, 103, 104, 105, and 106 Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 103, 1 - 106, 1 – 109, 1 - 1012 Write down your results as a table (with...
Problem 2 (2+3 marks). Assume your sorting algorithms have to deal with lists that can potentially...
Problem 2 (2+3 marks). Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal...
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])...
Using Python: 1. A simple sorting algorithm is called a "Bubble Sort" because elements bubble around...
Using Python: 1. A simple sorting algorithm is called a "Bubble Sort" because elements bubble around through the list. It is also called a "Sinking Sort" because the larger values "sink" to the end (bottom) of the list. A bubble sort iterates through a list and swaps adjacent pairs if they are in the wrong order. The sort continues until all elements are correctly ordered. Example: bubbleSort([0, 1, 8, 4, 3, 2, 9]) - as it begins to process will...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Which type of sorting algorithm is best used to sort sequentially? Explain
Which type of sorting algorithm is best used to sort sequentially? Explain
Name 3 sorting algorithms, give a short description and discuss which one you prefer and why.
Name 3 sorting algorithms, give a short description and discuss which one you prefer and why.
Evaluate the performance in general on two real applications of sorting algorithms in parallel computing. Support...
Evaluate the performance in general on two real applications of sorting algorithms in parallel computing. Support your description with diagrams and examples where it is necessary. simple and clearly answer
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT