Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 8.2, Problem 1E
Program Plan Intro
To illustrate the operation of the counting sort on the array
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Show the steps for sorting 2, 6, 4, 1, 2, 4, 1, 4 using counting sort (first version) with additional arrays B and C.
A 2 6 4 1 2 4 1 4
1 2 3 4 5 6 7 8
For the following array A=[1,-3,2;-3,3,-1;2,-1,0]
find the values of the second column after applying the
initial operations
H12(-1), H23(1), H21, K23(-1), H13, H1(-1)
Pseudo Code shown in Figure Q1(a) is an algorithm for binary searching for an array
with n number of elements. By applying this algorithm, show step by step approach on
how to find number 11 in an array as depicted in Figure Q1(b).
low + 0
high e n-1
while (low s high) do
ix + (low + high) /2
if (t = Alix)) then
return ix
else if (t < A[ix]) then
high e ix - 1
else
low + ix + 1
return -1
Figure Ql(a)
2
4
6
9
11
12 | 25
[0] [1)
[2]
[3] (4) [5)
[6]
Figure Q1(b)
Chapter 8 Solutions
Introduction to Algorithms
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- MATLAB PLEASE Write a general binary search code that can find a target with multiple appear- ances in an array. You can only use binary search algorithm. Use the following array to test your code to see if your code works: A=[2,4,0,1,1,2, 3, −3,4, −4,4,5,2,8,1,10,10,1]; and choose the test targets as 1. Your code must return the indices of the target number in the original un-sorted array. In your MATLAB code, no MATLAB built-in search or sort functions are allowed to be usedarrow_forwardGiven an array arg[] below, use the incremental approach to sort it in assending order 11 42 35 38 22 45 18 39 3arrow_forwardIn an array-based implementation of a List, why does the add operation take O(n) time in the average case? Select one: a. The time it takes to shift entries over to make room in the array depends on the number of entries in the List b. The time to copy the current entries into a newly allocated, larger array depends on the number of entries in the List c. The time to access the position in the array to add the new entry depends on the number of entries in the List d. The time to access the position in the array to add the new entry depends on the capacity of the arrayarrow_forward
- - Write a program that randomly generates a 20 x 20 two-dimensional array, board, of type int. An element board[i][j] is a peak (either a maximum or a minimum) if all its neighbors (there should be either 3, 5, or 8 neighbors for any cell) are less than board[i][j], or greater than board[i][j]. The program should output all elements in board, with their indices, which are peak. It should also output if a peak is a maximum or a minimumarrow_forwardSO You have been given two integer arrays/lists (ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.Note :Input arrays/lists can contain duplicate elements.The intersection elements printed would be in the order they appear in the first sorted array/list (ARR1).Input format :The first line of input contains an integer 'N' representing the size of the first array/list.The second line contains 'N' single space separated integers representing the elements of the first the array/list.The third line contains an integer 'M' representing the size of the second array/list.The fourth line contains 'M' single space separated integers representing the elements of the second array/list.Output format :Print the intersection elements. Each element is printed in a separate…arrow_forwardGiven an 8-element array: A = {x1, X2, X3, X4, X5, X6, 7, x8}, we would like to find its 3rd smallest element. It is known that x₁ and 8 are the two extreme elements: max and min, but we do not know which one is the max and which one is the min. It is also known that x4 and x5 are the two medians: left-median and right-median. What is the minimum number of comparisons you need to find the 3rd smallest element of the original array? Ans:arrow_forward
- Wap Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Clarification: Confused why the returned value is an integer but your answer is an array? Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well. Internally you can think of this: // nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); } Example 1: Input: nums = [1,1,2] Output: 2, nums = [1,2] Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2…arrow_forwardWrite a program to remove duplicate values in an array. e.g. ar[ ]={1,2,3,2,4,5,4,6,7,4,6,5,7} After the deletion of duplicate values the array becomes arr[ ={1,2,3,4,5,6,7}arrow_forwardYou are given an array J[1 : n] which includes the scores of n players in a game. You are additionally given an array P[1 : m] with the score of m new players. Design and analyze an algorithm that given both arrays J and P, can find the rank of each player P inside the array J, i.e., for each P[i], determines what would be the rank of P[i] in the array consisting of all elements of J plus P[i]. Your algorithm should run in O((n + m) ·log n) time.arrow_forward
- Let M(n) be the minimum number of comparisons needed to sort an array A with exactly n ele- ments. For example, M(1) = 0, M(2) = 1, and M(4) = 4. If n is an even number, clearly explain why M(n) = 2M(n/2) + n/2.arrow_forwardExplain step-by-step, showing all possible detail, how a binary search algorithm works on the array {1, 2, 3, 4, 5, 6, 7, 8} searching for the values of 5 and 12.arrow_forwardYou wish to find duplicates in an unsorted n-element array A. 1,..., 2n integers.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education