Consider an implementation of the array list ADT using a dynamic array, but instead of copying the elements into an array of double the size (that is, from N to 2N) when its capacity is reached, we copy the elements into an array with ⌈N/4⌉ additional cells, going from capacity N to N +⌈N/4⌉. Show that performing a sequence of n push operations (that is, insertions at the end) still runs in O(n) time in this case.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Data Structures and Algorithms in Java
Additional Engineering Textbook Solutions
Absolute Java (6th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Web Development and Design Foundations with HTML5 (8th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Big Java Late Objects
- implement QuickSort of ints that sorts the numbers in the non-decreasing order. Implement the rearrange function using QuickSort ( such that the pivot is set on the extreme left and the rearrangement is carried on on two pointers) using the O(n) time algorithmThe function gets as input an array, and index of the pivot.The function rearranges the array, and returns the index of the pivot after the rearrangement. int rearrange(int* A, int n, int pivot_index); Implement the QuickSort algorithm. - For n<=2 the algorithm just sorts the (small) array (smaller number first). - For n>=3 the algorithm uses the rearrange function with the pivot chosen to be the median of A[0], A[n/2], A[n-1]. void quick_sort(int* A, int n);arrow_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_forwardGiven an unsorted array A of size N that contains only positive integers, find a continuous sub-array that adds to a given number S and return the left and right index(1-based indexing) of that subarray. In case of multiple subarrays, return the subarray indexes which come first on moving from left to right. Note:- You have to return an ArrayList consisting of two elements left and right. In case no such subarray exists return an array consisting of element -1. Code please.arrow_forward
- Write a version of bottom-up mergesort that takes advantage of order in the array by proceeding as follows each time it needs to find two arrays to merge: find a sorted subarray (by incrementing a pointer until finding an entry thatis smaller than its predecessor in the array), then find the next, then merge them. Analyze the running time of this algorithm in terms of the array size and the number of maximal increasing sequences in the array.arrow_forwardLet A be an array with n = 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_forwardGiven 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_forward
- Develop and implement a version of mergesort that does not rearrange the array, but returns an int[] array perm such that perm[i] is the index of the i th smallest entry in the array.arrow_forwardGiven a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it In other words, arrange the elements into a sequence such that arr[1] >= arr[2] = arr[4] <= arr[5].. Example 1: Input: n = 5 arr[] {1,2,3,4,5} Output: 2 1 4 3 5 Explanation: Array elements after sorting it in wave form are 2 1 4 3 5.arrow_forwardWrite a version of bottom-up mergesort that takes advantage of order in the array by proceeding as follows each time it needs to find two arrays to merge: find a sorted subarray (by incrementing a pointer until finding an entry that is smaller than its predecessor in the array), then find the next, then merge them. Analyze the running time of this algorithm in terms of the array size and the number of maximal increasing sequences in the array.arrow_forward
- Given two arrays X and Y of positive integers, find the number of pairs such that xy > yx (raised to power of) where x is an element from X and y is an element from Y. Example 1: Input: M = 3, X[] = [2 1 6] N = 2, Y[] = [1 5]Output: 3Explanation: The pairs which follow xy > yx are as such: 21 > 12, 25 > 52 and 61 > 16 .Example 2: Input: M = 4, X[] = [2 3 4 5]N = 3, Y[] = [1 2 3]Output: 5Explanation: The pairs for the given input are 21 > 12 , 31 > 13 , 32 > 23 , 41 > 14 , 51 > 15 . Your Task:This is a function problem. You only need to complete the function countPairs() that takes X, Y, M, N as parameters and returns the total number of pairs. Expected Time Complexity: O((N + M)log(N)).Expected Auxiliary Space: O(1). Constraints:1 ≤ M, N ≤ 1051 ≤ X[i], Y[i] ≤ 103arrow_forwardGiven an array of locations in memory of n dimensions we want to find out what ordering it has in memory. For example, given the following input array [[[ 0 4 8 12] [16 20 24 28] [32 36 40 44]] [[48 52 56 60] [64 68 72 76] [80 84 88 92]]] your function has to return the tuple: [2,1,0] As a suggestion: - Loop through the number of dimensions - For each dimension generate a list of slices equivalent to the above indexing notation - Get the first two elements of each axis - Order the axes by the difference between those two elements Complete the code: def get_storage_order(r): import numpy as np return to check the code manually: r = np.array([[[ 0, 4, 8, 12], [32, 36, 40, 44], [64, 68, 72, 76]], [[16, 20, 24, 28], [48, 52, 56, 60], [80, 84, 88, 92]]]) get_storage_order(r)arrow_forwardIn 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_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning