Question

2. Consider the following unordered list: 2,4,8, 4, 4, 3, 2, 9, 2, 5, 2, 5, 2 Apply the distribution counting sort algorithm

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

//C++ Code:-

#include<iostream>
using namespace std;

// Function to sort the array and update the freq array.
void counting_sort(int a[],int n,int freq[])
{
for(int i=0;i<n;i++)
freq[a[i]]++;
int k=0;
for(int i=0;i<10;i++)
{
if(freq[i])
{
for(int j=0;j<freq[i];j++)
a[k++]=i;
}
}
}
int main()
{
// Define array with some value.
int a[]={2,4,8,4,4,3,2,9,2,5,2,5,2};
  
// Calculating size of an array.
int n=sizeof(a)/sizeof(a[0]);
  
// Array for counting Frequency of each value.
int freq[10]={0};
  
// Calling function to sort the array.
counting_sort(a,n,freq);
  
//Print the Sorted array.
cout<<"The sorted array : ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
  
// Print Distributed_Value and their corrosponding Frequency.
cout<<"Distributed_Value Frequency"<<endl;
for(int i=0;i<10;i++)
{
if(freq[i])
cout<<i<<" "<<freq[i]<<endl;
}
return 0;
}

10 { { main.cpp 1 #include<iostream> 2 using namespace std; 3 4 // Function to sort the array and update the freq array. 5 vo2 33 34 35 36 37 //Print the Sorted array. cout<<The sorted array : ; for(int i=0;i<n;i++) cout<<a[i]<< ; cout<<endl; 38

Add a comment
Know the answer?
Add Answer to:
2. Consider the following unordered list: 2,4,8, 4, 4, 3, 2, 9, 2, 5, 2, 5,...
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
  • 1. Consider the following unordered list: 20, 35, 25, 10, 40, 50, 45. Perform heap sort...

    1. Consider the following unordered list: 20, 35, 25, 10, 40, 50, 45. Perform heap sort to sort this list in nondecreasing (ascending) order. a. Perform the bottom-up method to arrange these values into a max heap. Show the heapify operations on each relevant subtree. (10 points) b. Show the tree representation and the array representation of these numbers after every dequeue operation. Remember that dequeue does not delete a number. Dequeue will instead remove that number from the heap...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Question 3 Given the input list [72, 29, 18, 4, 5, 64, 37, 2, 13, 10,...

    Question 3 Given the input list [72, 29, 18, 4, 5, 64, 37, 2, 13, 10, 23, 51, 95] Show every step of how the Selection Sort algorithm would proceed to sort this list in ascending order. Your solution goes here • You may wish to change the format of this Markdown cells into a Raw cell instead Question 4 Given the input list [72, 29, 18, 4, 5, 64, 37, 2, 13, 10, 23, 51, 95] • Show every...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list...

    Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list to be organized in any manner. The linear search works for lists that are "unsorted." But what if the values in the list are given in ascending order? This would be a sorted list. With a sorted list, is there a more efficient way to find the target? Unsorted Lists (4 pts) Assume there is a sorting algorithm with order of growth O(n), where...

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • 2. Consider your ID as an array of 9 elements, apply an insertion sort algorithm to...

    2. Consider your ID as an array of 9 elements, apply an insertion sort algorithm to sort the numbers in descending order. Show your steps. Example ID: 201710340 Array: 2 0 7 0 1 1 0 3 4

  • Sort the same array [3, 5, 1, 4, 8, 7,9, 2] in the ascending order by...

    Sort the same array [3, 5, 1, 4, 8, 7,9, 2] in the ascending order by applying the Merge sort algorithm. Write down the intermediate results step by step in a tree structure. (20 points)

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