Question

In: Computer Science

(C++)Heapsort: Write C++ codes for heapsort. The input array is a random permutation of A={1, 2,...

(C++)Heapsort: Write C++ codes for heapsort. The input array is a random permutation of A={1,

2, 3, …, 99, 100}. You should write codes to generate and print the random permutation first.

Solutions

Expert Solution

// c++ program for heapsort
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

  

void countSort(vector<int>& arr)
{

int max = *max_element(arr.begin(), arr.end());

int min = *min_element(arr.begin(), arr.end());

int range = max - min + 1;

  

vector<int> count(range), output(arr.size());

for (int i = 0; i < arr.size(); i++)

count[arr[i] - min]++;

  

for (int i = 1; i < count.size(); i++)

count[i] += count[i - 1];

  

for (int i = arr.size() - 1; i >= 0; i--) {

output[count[arr[i] - min] - 1] = arr[i];

count[arr[i] - min]--;

}

  

for (int i = 0; i < arr.size(); i++)

arr[i] = output[i];
}

  

void printArray(vector<int>& arr)
{

for (int i = 0; i < arr.size(); i++)

cout << arr[i] << " ";

cout << "\n";
}

  

int main()
{

vector<int> arr = { 1, 2, 3, ..........., 99,100,};

countSort(arr);

printArray(arr);

return 0;
}


Related Solutions

(C++)Counting Sort: Write C++ codes for counting sort. The input array is A = {20, 18,...
(C++)Counting Sort: Write C++ codes for counting sort. The input array is A = {20, 18, 5, 7, 16, 10, 9, 3, 12, 14, 0}
Related to HeapSort (a) Construct a heap for the following array of numbers:         8 1 2...
Related to HeapSort (a) Construct a heap for the following array of numbers:         8 1 2 3 5 6 4 7 10 9       Show the array after the insertion of each element into the heap. (b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
Create the following algorithms in c++ as a call to method: 1. HeapSort 2. QuickSort 3....
Create the following algorithms in c++ as a call to method: 1. HeapSort 2. QuickSort 3. MergeSort
1)Find the permutation and combination of the letters a, b, c, d taking 2 at a...
1)Find the permutation and combination of the letters a, b, c, d taking 2 at a time. Verify your answers with the permutation & combination formula.2) 2.An urn contains 10 marbles. 3 blues, 4 yellow and 3 red marbles. 6 marbles is selected at random. Find the probability of picking 1 blue, 3 yellow marbles and 2 red ones. 3) A computer operator has to issue passwords made up of a letter followed by two digits. How many passwords are...
Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT