1. Consider the
for each of its elements, the number of smaller elements and then uses this
information to put the element in its appropriate position in the sorted array:
ALGORITHM
ComparisonCountingSort(A[0..n − 1])
//Sorts an array by comparison counting
//Input: Array A[0..n
//Output: Array S[0..n
− 1] of orderable values
− 1] of A’s elements sorted
// in nondecreasing order
for i ← 0 to nCount− 1 do[i]←0
for i ← 0 to n − 2 do
for j ← i +1 to n − 1 do
if A[i] < A[j ]
Count[j ]← Count[j ] + 1
else Count[i]← Count[i] + 1
for i ←0 to n−1 do
S[Count[i]]←A[i]
return S
a. Apply this algorithm to sorting the list 60, 35, 81, 98, 14, 47.
b. Is this algorithm stable?
c. Is it in-place?
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- Shell sort is an in-place comparison-based sorting algorithm which is based on insertion sort algorithm. It works as follow : It breaks the original list into a number of smaller sublists, each of which is sorted using an insertion sort. The unique way that these sublists are chosen is the key to the shell sort. Instead of breaking the list into sublists of contiguous items, the shell sort uses an increment i, sometimes called the gap, to create a sublist by choosing all items that are i items apart. n a) We say that the our initial gap is floor: when we divide our sequence into 2k n sublists where 2k k>0 be the number of passes that can be increment until the denominator not greater than n, n and then sorting the elements which are position away with insertion sort. We have three 2k passes for an array size of 8 to 15. Demonstrate the Shell algorithm on the input array A = [35,33,42,10,14,19,27,44] showing how even- %3D п tually the algorithm outputs the sorted array…arrow_forwardYou are given an array A[1, ., n] in which the elements A[2] to A[n] are already sorted, i.e., A[2, .., n] is a sorted array. Write down an efficient algorithm with proper pseudocode for moving A[1] in the correct place, so that the full array A[1, ..., n] is sorted.arrow_forwardWrite a C program that gets a positive integer number, n, from the user, and generates n positive random integer values and stores them in a one-dimensional array. Then it starts the timer and sorts the numbers in ascending order using bubble sort technique and display the sorted array on the screen along with the elapsed time. • Bubble Sort is a simple sorting algorithm that works by continually swapping the adjacent elements in an unsorted array if they are not in a right order. Please explain each step as I would like to understand and not copy. Thank you!arrow_forward
- Prime numbers can also be generated by an algorithm known as the Sieve of Eratosthenes . The algorithm for this procedure is presented here. Write a program that implements this algorithm so as to find all prime numbers up to n = 150. Sieve of Eratosthenes Algorithm To Display All Prime Numbers Between 1 and n Step 1: Define an array of integers P . Set all elements P i to 0, 2 <= i <= n. Step 2: Set i to 2. Step 3: If i > n , the algorithm terminates. Step 4: If P i is 0 (value i is a prime number), then i is prime and display its value. Step 5: For all positive integer values of j , such that i * j ≤ n , set P i ∗ j to 1 (non - prime). Step 6: Add 1 to i and go to step 3arrow_forwardQuicksort is a powerful divide-and-conquer sorting algorithm that can be described in just four lines ofpseudocode. The key to Quicksort is the PARTITION(A, p, r) procedure, which inputs elementsptorof array A,and chooses the final element x = A[r] as the pivot element. The output is an array where all elementsto the left ofxare less thanx, and all elements to the right of x are greater than x. In this question, we will use the Lomuto Partition Method from class and assume that the pivot isalwaysthe last (right-most) element of the input array. Question: Let A be an array withn= 2k−1 elements, where k is some positive integer. Determine a formula (in terms of n) for the minimum possible number of total comparisons required by Quicksort, as well as a formula for the maximum possible number of total comparisons required by Quicksort. Use your formulas to show that the running time of Quicksort is O(nlogn) in the best case and O(n2) in the worst case.arrow_forwardConsider sorting n numbers stored in array A[1:n] by first finding the smallestelement of A[1:n] and exchanging it with the element in A[1]. Then find thesmallest element of A[2:n], and exchange it with A[2]. Then find the smallestelement of A[3:n], and exchange it with A[3]. Continue in this manner for thefirst n-1 elements of A. Write pseudocode for this algorithm, which is knownas selection sort. What loop invariant does this algorithm maintain? Why does itneed to run for only the first n-1 elements, rather than for all n elements? Give theworst-case running time of selection sort in big theta notation. Is the best-case runningtime any better?arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY