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}
Write code for A counting sort is a technique to sort an array of n positive...
Write code for A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m + 1 counters. Then, making only one pass through the array, you count the number of times each integer occurs in the array. For example, the figure below shows an array of integers that lie between 0 and 4 and the five counters after a counting sort has made its pass through the...
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
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT