(a)
To show that the values of array and auxiliary values after each iterations of while loop using HOARE-PARTITION.
(a)
Explanation of Solution
Given Information: The array is
Explanation: According to the HOARE-PARTITION,
Consider that
Now the array and auxiliary after each pass through HOARE-PARTITION algorithm will show in below table-
After the iteration
Hence, the HOARE-PARTITION algorithm exit the while loop with the auxiliary values of
(b)
To showthat the indices i and j are such that we never access an element of array A outside the sub-array.
(b)
Explanation of Solution
In the beginning of while loop when
Consider that it has an element k and
If it takes parameter
Hence, it is can be concluded that the indices i and j are such that one never access an element of array A outside the sub-array.
(c)
To explain that when HOARE-PARTITION terminates, it returns a value j such that
(c)
Explanation of Solution
The value of jis decreased after every iteration and at the final iteration of the loop i will equals to 1but greater than r .
Line 11 of HOARE-PARTITION illustrate that
Therefore, the HOARE-PARTITION algorithm terminates and returns
(d)
To explain that the every element of array is less than or equal to every element of
(d)
Explanation of Solution
Consider that it just finished the iteration of the loop in which j went to j1 and j2 and I went to i1 to i2 . The elements of the array
Similarly, the elements of the array
Now putting all the conditions of the array defined in the algorithm it has condition that is
Since at the termination of the algorithm
Therefore, the every element of array
(e)
To rewrite the QUICK-SORT procedure by using HOARE-PARTITION algorithm.
(e)
Explanation of Solution
QUICKSORT(A,p,r). If then q =HOARE-PARTITION(A,p,r). QUICKSORT(A,p,q-1). QUICKSORT(A,q+1,r). End if. HOARE-PARTITION(A,p,r) while TRUE repeat until . repeat . until . if then exchange with . else return j. end if. end while.
The quick sort algorithm is based on recursion. It first sets the parameters and then check the initial condition.
If the initial condition is true then it call the HOARE-PARTITION algorithm with recursion of calling itself again and again until the initial condition becomes false.
Want to see more full solutions like this?
Chapter 7 Solutions
Introduction to Algorithms
- PYTHON def _insertionsort(self, order): # Implements the insertion sort algorithm to sort a list of items in ascending or descending order n = len(self.items) for i in range(1, n): key = self.items[i] j = i - 1 if order == 'asc': while j >= 0 and key < self.items[j]: self.items[j + 1] = self.items[j] j -= 1 self.items[j + 1] = key elif order == 'desc': while j >= 0 and key > self.items[j]: self.items[j + 1] = self.items[j] j -= 1 self.items[j + 1] = key def sort(self, order = 'asc', type = 'insertion'): if type == 'insertion': # call the insertions sort method to sort atos array based on the parameter values #######################################################################arrow_forwardQuestion #7. SORTED LIST NOT UNSORTED C++ The specifications for the Sorted List ADT state that the item to be deleted is in the list. Rewrite the specification for DeleteItem so that the list is unchanged if the item to be deleted is not in the list. Implement DeleteItem as specified in (a) using an array-based. Implement DeleteItem as specified in (a) using a linked implementation. Rewrite the specification for DeleteItem so that all copies of the item to be deleted are removed if they exist. Implement DeleteItem as specified in (d) using an array-based. Implement DeleteItem as specified in (d) using a linked implementation.arrow_forward1. Explain why the modified bubble sort still exhibits O(n2) behavior on the average. Explain why insertion sort works well on partially sorted lists. 2. Write the use case for returning a book to the library, as discussed in this section. 3. Explain the difference between aggregation and composition; use an example.arrow_forward
- Stable sorting algorithms keep records with equal keys in the same order (i.e. values). That is, a sorting method is stable if R appears before S in the original list and R appears before S in the sorted list whenever there are two records R and S with the same key. Is Counting-sort a stable algorithm?arrow_forwardJAVA Help: Mergesort Implement a natural merge sort for linked lists. (This is the method of choice for sorting linked lists because it uses no extra space and is guaranteed to be linearithmic.) During each iteration, natural merge sort works by scanning the list from the left to right identifying naturally sorted sub-lists and merging the sub-lists, and continue scanning further identifying and merging the sub-lists until the end of the list. Repeats the process until the entire list is sorted. Example: Unsorted list M -> E -> R -> G -> E -> S -> O -> R -> T -> E -> X -> A -> M -> P -> L -> E After first iteration: E - > M -> R -> E -> G -> S -> E -> O -> R -> T -> X -> A -> L -> M -> P -> E After second iteration: E -> E -> G -> M -> R -> S -> A -> E -> L -> M -> 0 -> P -> R -> T -> X -> E After third iteration: A -> E -> E -> E -> G…arrow_forwardusing import java.util.Collections; make the merge sort algorithm to implement the MergeSort sort method public class Person implements Comparable<Person> { private String firstName; private String lastName; private int age; Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } @Override public String toString() { return lastName + ", " + firstName + " " + age; } @Override public int compareTo(Person other) { return this.lastName.compareTo(other.lastName); } } import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Frank", "Denton", 73)); people.add(new Person("Mark", "Cohen", 44)); people.add(new Person("Tim", "Smith", 22)); people.add(new Person("Steve", "Denton", 16)); people.add(new Person("Andy", "Ashton", 44)); people.add(new Person("Albert", "Denton", 58));…arrow_forward
- 3) FSM - Draw the table and diagram for the FSM for the below algorithm (c) Return smallest element of ordered set S, where completion character for S is ”*”arrow_forward8.Implementation of priority queue using linear array.Insertion - O(n)Extract min/max Node - O(1)"""import itertools class PriorityQueueNode: def __init__(self, data, priority): self.data = data self.priority = priority def __repr__(self): return "{}: {}".format(self.data, self.priority) class PriorityQueue: def __init__(self, items=None, priorities=None): """Create a priority queue with items (list or iterable). If items is not passed, create empty priority queue.""" self.priority_queue_list = [] if items is None: return if priorities is None: priorities = itertools.repeat(None) for item, priority in zip(items, priorities): self.push(item, priority=priority) def __repr__(self): return "PriorityQueue({!r})".format(self.priority_queue_list) def size(self): """Return size of the priority queue. """ return len(self.priority_queue_list) def…arrow_forwardThe code below serves as a template where the base case and the recursive calls are done. The specific task is to implement the partitioning where elements are either put to the left or right of the pivot depending on their relative values. Input Format The first line contains an integer n, the number of elements in the list. The next n lines would be the unsorted elements of the list. Constraints 1 ≤ n ≤ 10000 Each element i is: -1000 ≤ i ≤ 1000 Minimize auxiliary space usage. Output Format n lines that display the n elements of the sorted list. Smallest element first, largest element last. Example Input 15 3 44 38 5 47 15 36 26 27 2 46 4 19 50 48 Example Output 2 3 4 5 15 19 26 27 36 38 44 46 47 48 50arrow_forward
- The bubble sort algorithm discussed in class is used to sort the following sequence of integers: 2 16 38 9 4 14 How many passes must the algorithm perform to guarantee the entire sequence is sorted? What is the list obtained after the first pass? What is the list obtained after the third pass? What is the list obtained after the final pass?arrow_forward1-- Consider the following algorithm: sum = 0 For j starting at 1 and ending with 11: sum = sum + (9*j + 4) print(sum) What is printed as a result of executing this algorithm? 2-- #Part 1: computes A - B and its cardinality AminusB = set()for element in A: # this line runs through every element in A if not(element in B): #A - B is the set of elements that are in A and are not in B AminusB.add(element) # Add to AminusB every element in A if the element is also not in B n = len(AminusB) #len() returns the number of elements in the arrayprint(n) What value is printed as a result of executing this algorithm?arrow_forwardThe Quicksort algorithm based on divide and conquer algorithm. Explain the difference between it and merge sort. If the list has the following items17 9 11 30 8 20 35 7 5 45 3 28Apply the quick sort on the above list by using the pivot is always the left item and find the following The list after a first partition call The split point in the second partition call.arrow_forward
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning