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
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])...
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
C++ What does it mean for a sorting algorithm to be "stable"? Wikipedia has a list...
C++ What does it mean for a sorting algorithm to be "stable"? Wikipedia has a list of all known sorting algorithms. Which one's average is the worst and what is its Big O? (I just wanted to make you see that list.)
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // 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 manipulate the elements...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random values for an array and sorted values; sorted the same list twice and collect time each time) for the following array sizes: 1000, 2000, and 10000. You should be able to confirm that the runtime is n^2 for unsorted list (i.e., going from 1000 to 2000 should be about 4 times slower and going from 1000 to 10000 should be about 100times slower).
In my modified quick-sort algorithm, the floor(n/2) is chosen to be the pivot point. What’s the...
In my modified quick-sort algorithm, the floor(n/2) is chosen to be the pivot point. What’s the running time of my modified quick-sort algorithm on an array of numbers that’s already sorted?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT