Question

Code in C - CountingSort exact as the algorithm which takes in an array of 1000 random integers. Provide comments in code for understanding purpose

COUNTING-SORT(A, B, k) 1 let C[0..k] be a new array 2 for i = 0 to k C[i] = 0 4 for j = 1 to A.length 5 C[Ajl] = C[A;l] + 1 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C program

#include<stdio.h>
#include<time.h>
#define n 1000

//counting sort function
void countSort(int A[] , int B[] , int k){
   int i;
   int C[k+1];
   for(i=0;i<=k;i++) C[i] = 0;
  
   for(i=0 ;i<n;i++)C[A[i]] = C[A[i]] + 1;
  
   for(i=1;i<=k;i++)C[i] = C[i] + C[i-1];
  
   for(i = n-1;i>=0;i--){
       B[C[A[i]]-1] = A[i];
       C[A[i]] = C[A[i]] -1;
   }

}
//max() function return maximum element in array A[]
int max(int A[]){
   int m = A[0];
   int i;
  
   for(i=0;i<n;i++){
       if(A[i]>m) m =A[i];
   }
   return m;
}

//Driver main function
int main(){

   int A[n],B[n];
   int i=0;
   srand(time(NULL));
  
   //Array initialization using random numbers
   for(i=0;i<n;i++){
       A[i] = rand()%1000;
   }
  
   printf("Unsorted Array : \n");
   //printing unsorted array
   for(i=0;i<n;i++)printf("%d ",A[i]);
  
   int k = max(A);
   //calling countSort() function to sort array A[] and put sorted element int array B[]
   countSort(A,B,k);
   printf("\nsorted Array : \n");
   //printing sorted array B[]
   for(i=0;i<n;i++)printf("%d ",B[i]);
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Code in C - CountingSort exact as the algorithm which takes in an array of 1000...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • When asked to describe an algorithm you are expected to give a clear pseudo-code description of...

    When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

  • Which sorting algorithm works by iterating through an input array a, and swapping a[i] with a[x],...

    Which sorting algorithm works by iterating through an input array a, and swapping a[i] with a[x], where i starts at 0 and ends at a.length-1, and x is an index of a minimum value found in a[i], a[i+1], ..., a[a.length-1]? Selection Sort Bubble Sort Insertion Sort Bogo Sort Ascending Sort

  • [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You...

    [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can...

  • How to write a recursive method named lessThanKFirst that receives an array of integers a and...

    How to write a recursive method named lessThanKFirst that receives an array of integers a and an integer k and rearranges the integers in a in such a way that all integers that are smaller than k come before any integers that are greater than or equal to k. As an example, suppose that a and k are: int[] a  = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52}; int k = 73; Then, the following...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • Run-able, Corrected source code The following code will sort an array in descending order. Keep it...

    Run-able, Corrected source code The following code will sort an array in descending order. Keep it as Descending. However it has two logic bugs. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /*    The sorting algorithm is to be left as descending    There are two logical bugs, the number do not sort correctly    Fix it    Add analysis output as per requirements */ #include <stdio.h> main() { char wait; short m[]={3,5,7,2,5,1,2,2,              6,5,7,2,4,1,3,3,              7,7,3,2,5,7,1,9}; unsigned char temp, i, j; unsigned char numElements =...

  • Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements...

    Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements that out of order. BUBBLESORT(A) 1. for i = 1 to A.length – 1 2. for j = i + 1 to A.length 3. if A[j] < A[i] 4. exchange A[j] with A[i] a) A loop invariant for the outer for loop in lines 1 – 4 is: At iteration i, the sub-array A[1..i] is sorted and any element in A[i+1..A.size] is greater or...

  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT