Question

In: Computer Science

Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...

Sort the given keys using Counting sort algorithm. Also write the algorithm.

5, 2, 3, 1, 0, 2, 1, 5, 0  

Solutions

Expert Solution

Counting Sort Algorithm:-

Counting_Sort(arr, s)

max <- find largest element in the array.

initialize count array with all zeros

for j <- 0 to s

find the total count of each unique element and store the count at jth index in count arr

for i <- 1 to max

find the cumulative sum and store it in count array itself

for j <- s to 1

restore the elements to arr

decrease count of each element restored by 1

Code:-

#include<stdio.h>
#include<conio.h>
void Counting_Sort(int arr[], int s)
{
int output[100],c[100],max=arr[0];
for(int i=1;i<s;i++)
{
if (arr[i] > max)
{
       max = arr[i];
   }  
}
for(int i=0;i<=max;i++)
{
c[i]=0;
}
for(int i=0;i<s;i++)
{
c[arr[i]]++;
}
for(int i=1;i<=max;i++)
{
c[i]+=c[i-1];
}
for(int i=s-1;i>=0;i--)
{
output[c[arr[i]]-1]=arr[i];
c[arr[i]]--;
}
for(int i=0;i<s;i++)
{
arr[i] = output[i];
}
}

void Array(int arr[], int s)
{
for(int i=0;i<s;++i)
{
printf("%d\n",arr[i]);
}
}

int main()
{
int arr[] = {5, 2, 3, 1, 0, 2, 1, 5, 0};
int n =(sizeof(arr) / sizeof(arr[0]));
Counting_Sort(arr,n);
Array(arr,n);
}

Sample:-

Exexcution:-

Run:-


Related Solutions

1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4,...
Use counting sort, sort the following numbers: 4, 2, 5, 4, 2, 3, 0, 2, 4, 3
Please write a python code which implements the counting sort algorithm by creating a CountingSort function...
Please write a python code which implements the counting sort algorithm by creating a CountingSort function with an array input in the form of a list. Then write code to sort 3 randomly generated arrays and the data array listed below, print the output clearly for all four test arrays, develop and comment on the growth function of your code. Comment on the Big O notation of the code. ( Please do not forget to comment on your code to...
(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}
Please describe the reason(s) why we choose the counting sort algorithm to sort each digit in...
Please describe the reason(s) why we choose the counting sort algorithm to sort each digit in the Radix Sort?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT