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++)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.
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...
External sort: Write an external merge sort in c++ that takes in an input file with...
External sort: Write an external merge sort in c++ that takes in an input file with 120 integers. You are allowed to hold a maximum of 20 integers in memory. You must create 2 files to process the integers and they must be sorted and placed in the original file.
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
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT