Question

In: Computer Science

(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}

Solutions

Expert Solution

Answer-

Your code given below here as your requirement-

  1. #include<iostream>
  2. using namespace std;
  3. int z=0;
  4. void CouSort(int A[],int B[],int t) //using function for the counting sort
  5. {
  6.    int C[z];
  7.    for(int i=0;i<z+1;i++)
  8.    {
  9.        C[i]=0;
  10.    }
  11.    for(int j=1;j<=t;j++)
  12.    {
  13.    //increment it at position x in C*/
  14.        C[A[j]]++;          
  15.    }
  16.    for(int i=1;i<=z;i++)
  17.    {
  18.        C[i]+=C[i-1];
  19.    }
  20.    for(int j=t;j>=1;j--)
  21.    {
  22.        B[C[A[j]]]=A[j];
  23.        C[A[j]]=C[A[j]]-1;      
  24.    }
  25. }
  26. int main()
  27. {
  28.    int t;
  29.    cout<<"please enter array size and after enter array element :";
  30.    cin>>t;
  31.   
  32.    int A[t],B[t];
  33.   
  34.    for(int i=1;i<=t;i++)
  35.    {
  36.        cin>>A[i];
  37.        if(A[i]>z)
  38.        {
  39.            z=A[i];
  40.        }
  41.    }
  42.    CouSort(A,B,t); // driver code for function
  43.    for(int i=1;i<=t;i++)   
  44.    {
  45.        cout<<B[i]<<" "; ////print the sorted sequence on the
  46.    }
  47.    cout<<endl;
  48.    return 0;
  49. }

screenshot of output and running program -

Note- Please do upvote, if any problem then comment in box sure I will help.


Related Solutions

(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the...
(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the low order to high order. The input array is A = {329, 457, 657, 839, 436, 720, 353}
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19,...
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19, 6, 10, 7, 16, 17, 13, 14, 12, 9] if the input range is 0-19.
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) 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...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...
5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
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)...
Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT