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
Question
Chapter 8, Problem 3P
(a)
Program Plan Intro
To describe the procedure of sorting an array in
(b)
Program Plan Intro
To describe the sorting of the strings in
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Take an array of length n where all the numbers are nonnegative and unique. Find the element inthe array possessing the highest value. Split the element into two parts where the first part containsthe next highest value in the array and the second part holds the required additive entity to get thehighest value. Return the array index value. Consider the negative numbers as well.Input: [4, 8, 6, 3, 2]Output: [4, 6, 2, 6, 3, 2]
: In an array of integers, a "peak" is an element which is greater than or equal tothe adjacent integers and a "valley" is an element which is less than or equal to the adjacent integers. For example, in the array {5, 8, 6, 2, 3, 4, 6}, {8, 6} are peaks and {5, 2} are valleys. Given an arrayof integers, sort the array into an alternating sequence of peaks and valleys.EXAMPLEInput: {5, 3, 1, 2, 3}Output: {5, 1, 3, 2, 3}
You're given an array arr. Apply the following algorithm to it:
find intervals of consecutive prime numbers and consecutive non-prime numbers;
replace each such interval with the sum of numbers in it;
if the resulting array is different from the initial one, return to step 1, otherwise return the result.
Input
A non-empty integer array such that:
-10000 ≤ arr[i] ≤ 10000
1 ≤ arr.length ≤ 1000
Output
An integer array.
Examples
For arr = [1, 2, 3, 5, 6, 4, 2, 3] the result should be [21, 5]:
[1, 2, 3, 5, 6, 4, 2, 3] --> [(1), (2 + 3 + 5), (6 + 4), (2 + 3)] --> [1, 10, 10, 5]
[1, 10, 10, 5] --> [(1 + 10 + 10), (5)] --> [21, 5]
For arr = [-3, 4, 5, 2, 0, -10] the result should be [1, 7, -10]:
[-3, 4, 5, 2, 0, -10] --> [(-3 + 4), (5 + 2), (0 + -10)] --> [1, 7, -10]
Solve it in C# please
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
- Given an array, find the total number of inversions of it. If (i < j) and (A[i] > A[j]), then pair (i, j) is called an inversion of an array A. We need to count all such pairs in the array. For example, Input: A[] = [1, 9, 6, 4, 5] Output: The inversion count is 5 There are 5 inversions in the array: (9, 6), (9, 4), (9, 5), (6, 4), (6, 5)arrow_forwardImplement a Binary Search algorithm, and using the results from Exercise 5, show how longthe Binary Search takes (on average) for arrays of size 10, 100, and 1000. (You do not haveto print out the values in the array).arrow_forwardYou can see in the above display, we first sort each row of the 2D array; we then take the transpose of a two D array, i.e., all the row elements becoming the column elements; we then sort each row of the 2D again. If you read the final array, each row is sorted; each column is also sorted. The smallest element obviously is the 1st element of the two D array and the last element is the largest element of a two D array. Let us now look at the following UML diagram: (Note that additional methods are allowed; proposed methods and instance variable cannot be changed) Main method firstly constructs a 2D array of certain sizes and then construct a TwoD object and drive the task according to the above runtime interactions and displays. TwoD class has only one instance variable which is a two D array of numbers ( int or double). The constructor must do some “deep” copying. A copy constructor. The other three methods are obvious in definition: to sort each row, to rotate the 2D array (i.e.,…arrow_forward
- Given an integer **n**, create a two-dimensional array of size **n×n** according to the following rules and print it: - On the main diagonal put `0`.- On the diagonals adjacent to the main put `1`.- On the next adjacent diagonals put `2`, and so forth. # Example input ```5``` # Example output ```0 1 2 3 41 0 1 2 32 1 0 1 23 2 1 0 14 3 2 1 0```arrow_forwardRead in two integers n and m (n, m < 50). Read n integers in an array A. Read m integers in anarray B. Then do the following (write separate programs for each, only the reading part iscommon).a) Find if there are any two elements x, y in A and an element z in B, such that x + y = z.b) Copy in another array C all elements that are in both A and B (intersection).c) Copy in another array C all elements that are in either A and B (union).d) Copy in another array C all elements that are in A but not in B (difference).c programm please tell all the answerarrow_forwardCorrect answer will be upvoted else downvoted. Computer science. You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median. A median in an array of length n is an element which occupies position number ⌊n+12⌋ after we sort the elements in non-decreasing order. For example: median([1,2,3,4])=2, median([3,2,1])=2, median([2,1,2,1])=1. Subarray a[l..r] is a contiguous part of the array a, i. e. the array al,al+1,…,ar for some 1≤l≤r≤n, its length is r−l+1. Input The first line contains two integers n and k (1≤k≤n≤2⋅105). The second line contains n integers a1,a2,…,an (1≤ai≤n). Output Output one integer m — the maximum median you can get.arrow_forward
- Given an array, find the next greater element for each element in the array, ifavailable. If not available, print the element itself. The next greater element y for anelement x in the array is the first element that is greater than x and occurs on its rightside. The next greater element of the right most element in an array is the elementitself.Example: Given A = [ 6 8 4 3 9] the next greater element listB = [8 9 9 9 9]arrow_forwardWrite a program that fills an integer array of size 1000 with random positive integers between 1 and 10.Assume that each element represents a different city and the integers stored in the array represent thenumber of golds in those cities. The program traverses over the array and collects the golds at each city,starting from a random position. When the program visits a certain city, all of the golds are collected,and the visited element of the array becomes 0 (no gold is left at that city.) The journey continues withthe rule given below. If the position at the nth step is denoted by pn, and the number of golds at the citypn is denoted by gn,??+1 = {| ((??−1 + ?? + ??) ???1000|, ????? 2 = 0| ((??−1 + ?? − ??) ???1000|, ????? 2 = 1.The program continues visiting cities until the last three visited city has no gold.The program, at each visit, should print out the following information:• The current city index (pn).• The previous city index (pn-1).• The number of golds at the current city…arrow_forwardWrite an c progra, that takes an array of N integers (N will be specified by the user at run time). It then finds how many numbers in the array are divisible by 3 and how many numbers are divisible by 7. the it copies, from the input array above, the numbers that are divisible by 3 into a new array and those divisible by 7 into a different new array. Once copied, the algorithm should compute the average of each of these two new arrays and print the averages on the screen with suitable message to let the user know which average is being printed Note: an integer number is divisible by 3 (respectively 7) if the remaining of its division by 3 (respectively 7) is 0. You are allowed to use the modulus operator %.arrow_forward
- : In searching an element in an array, linear search can be used, even though simple to implement, but not efficient, with only O(n) time complexity. Assuming the array is already in sorted order, modify the search function below, using a better algorithm, so the average time complexity for the search function is O(log n). include <iostream> using namespace std; int search(int al), int s, int v) { 1/ Modify below codes. for (int i = 0; i <s; i++) { if (a[i] = v) return i; return -1; int main() { int intArray:10] = { 5, 7, 8, 9, 10, 12, 13, 15, 20, 34); // Search for element '12' in 10-elements integer array. cout << search(intArray, 10, 12); // '5' will be printed out. // Search for element '35' in 10-elements integer array. cout << search(intArray, 10, 35); // '-1' will be printed out. // Index '-l' means that the element is not found. return 0;arrow_forwardPlot the result of (given) selectionsort(A) when A is a perfectly sorted and contains randoms integers ,0 to 100 inclusive, for array sizes n = 2,4,8,16,..1024. please provide code for A and generating the plot for each doubling array, as well as the resulting plot. def selectionsort(A): for i in range(len(A)): min_idx = i for j in range(i + 1, len(A)): if A[min_idx] > A[j]: min_idx = j A[i], A[min_idx] = A[min_idx], A[i] return Aarrow_forwardarray unordered Arr has unsorted integers. SortedArr is an integer array. SortedArr performs which task better than unsortedArr? Use the fastest algorithms. Inserting a new element II Searching for a given element III Calculating the mean of the elements (A) I alone (B) II alone (C) III alone (D) I and II alone (E) I, II, and IIIarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License