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

SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2,...
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2, 3, 1, 0, 2, 1, 5, 0                                                                  SHOW WORK!
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.
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
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.
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}
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3...
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3 numbers a, b, c, from the user, and prints the 3 numbers in ascending order
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT