Sort the numbers: 2, 1, 4, 5, 7, 1, 7, 11, 8, 9 using the counting sort.
COUNTING SORT ALGORITHM:
Counting Sort Algorithm is an efficient and stable sorting algorithm that can be used for sorting elements within a specific range. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array.
Let us consider an example and the data in the range 0 to 9
Input data: 1,5,1,3,7,4,3
1)Take a count array for storing the count of each unique element
INDEX: 0 1 2 3 4 5 6 7 8 9
COUNT: 0 2 0 2 1 1 0 1 0 0
2)Modify the count array such that each element at each index stores the sum of previous counts
INDEX: 0 1 2 3 4 5 6 7 8 9
COUNT: 0 2 2 4 5 6 6 7 7 7
The modified count array indicates the position of each object in the output sequence
3)Output each object from the input sequence followed by decreasing its count by 1.
Process the input data : 1,5,1,3,7,4,3. Position of 1 is 2.Put data 1 at output with index 2. Decrease count by 1 to place next data 1 at an index 1 smaller than this index.
Now for the given elements 2,1,4,5,7,1,7,11,8,9
STEP 1:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 2 1 0 1 1 0 2 1 1 0 1
STEP 2:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 2 3 3 4 5 5 7 8 9 9 10
STEP 3:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 2 3 3 4 5 5 7 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
2
STEP 4:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 2 2 3 4 5 5 7 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 2
STEP 5:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 1 2 3 4 5 5 7 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 2 4
STEP 6:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 1 2 3 3 5 5 7 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 2 4 5
STEP 7:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 1 2 3 3 4 5 7 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 2 4 5 7
STEP 8:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 1 2 3 3 4 5 6 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 1 2 4 5 7
STEP 9:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 1 2 3 3 4 5 6 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 1 2 4 5 7 7
STEP 10:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 0 2 3 3 4 5 5 8 9 9 10
PLACES:
1 2 3 4 5 6 7 8 9 10
1 1 2 4 5 7 7 11
STEP 11:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 0 2 3 3 4 5 5 8 9 9 9
PLACES:
1 2 3 4 5 6 7 8 9 10
1 1 2 4 5 7 7 8 11
STEP 12:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 0 2 3 3 4 5 5 7 9 9 9
PLACES:
1 2 3 4 5 6 7 8 9 10
1 1 2 4 5 7 7 8 9 11
STEP 13:
INDEX: 0 1 2 3 4 5 6 7 8 9 10 11
COUNT: 0 0 2 3 3 4 5 5 7 8 9 9
PLACES:
1 2 3 4 5 6 7 8 9 10
1 1 2 4 5 7 7 8 9 11
Therefore, the sorted elements for the given input are:
1,1,2,4,5,7,7,8,9,11
Sort the numbers: 2, 1, 4, 5, 7, 1, 7, 11, 8, 9 using the counting...
2. Sort the following numbers using counting sort 8 11 4 7 6 912 Index Sum Count Position Sorted Input
Just show Paper-2 1. Sort the following numbers using Bubble sort 17 12 19 11 5 8 10 2
Using an insertion sort, sort the array 5, 7, 4, 9, 8, 6, 3 into ascending order. After the first swap, what will the array look like?
Sort the sequence 3, 10, 7, 2, 11, 6, 9, 4 using Merge sort. Show the intermediate steps .
1. Given a set of numbers: A = {1, 3, 5, 2, 8, 7, 9, 4 }. Add each digit of your N-Number (e.g. N0 0 3 2 2 0 1 8) to each element of A separately to get your own final array B. ( e.g. B={1,3,8,4,10,7,10,12} = {B1,B2,B3,B4,B5,B6,B7,B8}. ) a) Your set B = ? b) Use merge-sort to sort the array B. Show each step. c) How many “comparisons” for each algorithm above? 2. Use binary-search for...
Counting numbers are to be formed using only the digits 6, 8, and 9. Determine the number of different possibilities for the type of number described below. Four-digit numbers with one pair of adjacent 6s and no other repeated digits (Hint: You may want to split the task of designing such a number into three parts, such as (1) position the pair of 6s, (2) position the 8, and (3) position the 9.)
Sort the sequence of integers { 2 ,5 ,4 , 6 , 3, 7, 8, 1} (show steps, do not write code) (20 points) Using Insertion Sort
[5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...
DATA STRUCTURE 1) Sort 3, 4, 5, 6,1, 9, 2, 5, 6 using insertion sort.
2 - 3 5 7 8 10 11 12 13 Serial numbers for a product are to be made using 4 letters followed by 2 digits. The letters are to be taken from the first 5 letters of the alphabet, with no repeats. The digits are taken from the 10 digits (0, 1, 2, ..., 9), with no repeats. How many serial numbers can be generated? (If necessary, consult a list of formulas.) a 5 Continue Submit Assignment 2020 MGw.el...