Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 16.1, Problem 16.4CP
Explanation of Solution
Quick sort:
- This is sorting
algorithm in which it sorts the array by dividing the list into two sub lists. - After dividing the lists, choose the pivot element between the lists.
- After selecting the pivot element, the algorithm reorders the values in the array until all the elements in left sub list is lesser than the pivot.
- Next, it sorts all the elements in the right sub list which are greater than or equal to pivot.
- Then, this algorithm recursively repeat the steps on sub list 1 and sub list 2...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Quick Sort
We choose an element from the list, called the pivot. We'll use it to divide the list into two sub-lists.
We reorder all the elements around the pivot
The ones with smaller value are placed before it
All the elements greater than the pivot after it.
After this step, the pivot is in its final position. This is the important partition step.
We apply the above steps recursively to both sub-lists on the left and right of the pivot.
Â
Quick Sort (Example)
Consider the following array Arr[] = {5, 9, 4, 6, 5, 3}
Let's suppose we pick 5 as the pivot for simplicity
We'll first put all elements less than 5 in the first position of the array: {3, 4, 5, 6, 5, 9}
We'll then repeat it for the left sub-array {3,4}, taking 3 as the pivot
There are no elements less than 3
We apply quicksort on the sub-array in the right of the pivot, i.e. {4}
This sub-array consists of only one sorted element
We continue with the right part of the original array, {6, 5, 9} until we get the final ordered…
Selection sort is a sorting algorithm, like Bubble sort which you saw in the previous module. Selection sort works as follows:
Selection sort divides the input list into two parts: a sublist of sorted items (left part) and a sublist of still unsorted items (right part). Initially, the sorted
sublist is empty and the whole input consists of the unsorted sublist. To fill the sorted sublist, the algorithm computes the (index of) the minimum of the
unsorted sublist and swaps the first unsorted element and the minimum element (if the minimum is already the first element, nothing happens).
Afterward, the sorted sublist is one bigger. This continues until the unsorted sublist is empty and the entire input is sorted.
Example:
Sorted sublist
Unsorted sublist
Least element in unsorted list
(11, 25, 12, 22, 64) 11
|(11)
(25, 12, 22, 64) 12
|(11, 12)
(25, 22, 64) 22
|(11, 12, 22)
(25, 64) 25
|(11, 12, 22, 25)
(64) 64
(11, 12, 22, 25, 64)
Implement this algorithm.
Implement a function called…
Write a version of the sequential search algorithm that can be used to search a sorted list.
Chapter 16 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Ch. 16.1 - Prob. 16.1CPCh. 16.1 - Prob. 16.2CPCh. 16.1 - Prob. 16.3CPCh. 16.1 - Prob. 16.4CPCh. 16.2 - Prob. 16.5CPCh. 16.2 - Prob. 16.6CPCh. 16.2 - Prob. 16.7CPCh. 16.2 - If a sequential search is performed on an array,...Ch. 16.3 - Prob. 16.9CPCh. 16.3 - Prob. 16.10CP
Ch. 16.3 - Prob. 16.11CPCh. 16.3 - Prob. 16.12CPCh. 16.3 - Prob. 16.13CPCh. 16.3 - Prob. 16.14CPCh. 16.3 - Let a[ ] and b[ ] be two integer arrays of size n....Ch. 16.3 - Prob. 16.16CPCh. 16.3 - Prob. 16.17CPCh. 16.3 - Prob. 16.18CPCh. 16 - Prob. 1MCCh. 16 - Prob. 2MCCh. 16 - Prob. 3MCCh. 16 - Prob. 4MCCh. 16 - Prob. 5MCCh. 16 - Prob. 6MCCh. 16 - Prob. 7MCCh. 16 - Prob. 8MCCh. 16 - Prob. 9MCCh. 16 - Prob. 10MCCh. 16 - True or False: If data is sorted in ascending...Ch. 16 - True or False: If data is sorted in descending...Ch. 16 - Prob. 13TFCh. 16 - Prob. 14TFCh. 16 - Assume this code is using the IntBinarySearcher...Ch. 16 - Prob. 1AWCh. 16 - Prob. 1SACh. 16 - Prob. 2SACh. 16 - Prob. 3SACh. 16 - Prob. 4SACh. 16 - Prob. 5SACh. 16 - Prob. 6SACh. 16 - Prob. 7SACh. 16 - Prob. 8SACh. 16 - Prob. 1PCCh. 16 - Sorting Objects with the Quicksort Algorithm The...Ch. 16 - Prob. 3PCCh. 16 - Charge Account Validation Create a class with a...Ch. 16 - Charge Account Validation Modification Modify the...Ch. 16 - Search Benchmarks Write an application that has an...Ch. 16 - Prob. 8PCCh. 16 - Efficient Computation of Fibonacci Numbers Modify...
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 a sorted array, write a program named as ProblemB.cpp that creates a Binary Search Tree.Hints:•Write a recursive function to do the followings:a) Get the Middle of the array and make it root of the BST.b) Recursively call a function to do the same for left subarray (0 .. mid-1) and right subarray (mid+1..size-1).i) Get the middle of left half and make it left child of the root created in step a.ii) Get the middle of right half and make it right child of the root created in step a.c) return root.arrow_forwardIn Python: Write a recursive function named  Multiply2 that multiples by 2 and prints each element in a given list. For example:  mylist=[1,2,3,4]  Multiply2(mylist)  prints: 2 4 6 8 Note: the function takes only one parameter, which is the list on which the operation will be performed. It's ok if the list becomes empty after calling the function.arrow_forwardImplement the following two sorting algorithms in a program called p3.py. Write two separate functions for these algorithms. Both functions must take a list of integers as the input parameter.1) Bogosort: first shuffle the list argument (i.e., randomize the positions of every element) and then check to see if the result is in sorted order. If it is, the algorithm terminates successfully and returns True, but if it is not then the process must be repeated.2) Bozosort: choose two elements in the list at random, swap them, and then check if the result is in sorted order. If it is, the algorithm terminates successfully and returns True, but if it is not then the process must be repeated.Write a main() function and call both sorting functions using the same list as their arguments. The list can be of any size (try a small list first). Does any of your algorithms terminate? If yes, count the number of iterations it uses to sort the list. Does it always use the same number of repetitions? If…arrow_forward
- Apply the merge sort on the following list and sort the list in decreasing order: 91 98 29 93 98 53 68 33 33 47 You must show how the list is divided by the recursive calls to MERGE-SORT, then merged at each stage to obtain the final sorted list.arrow_forwardWrite an algorithm that searches a sorted list of n items by dividing it into three sublists of almost n/3 items. This algorithm finds the sublist that might contain the given item and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Analyze your algorithm and give the results using order notation.arrow_forwardThe implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forward
- TRUE or FALSE? Suppose you have been given a sorted list of 100 names and needed to find a particular name in that list. Using binary search, it is possible that you might have to look at every location in list before finding the desired namearrow_forwardRemoving an item from the middle of a large ArrayList (n items) is a single operation. What is the time complexity of this operation?arrow_forwardA sort algorithm that finds the smallest element of the array and interchanges it with the element in the first position of the array. Then it finds the second smallest element from the remaining elements in the array and places it in the second position of the array and so onarrow_forward
- * allSame returns true if all of the elements in list have the same value. * allSame returns false if any two elements in list have different values. * The array may be empty and it may contain duplicate values. * * Your solution should contain at most one loop. You may not use recursion. * Your solution must not call any other functions. * Here are some examples (using "==" informally): * * * * * * * * true == allSame (new double[] { }) true == allSame(new double[] {11}) true == allSame (new double[] { 11, 11, 11, 11 }) false == allSame(new double[] { 11, 11, 11, 22 }) false == allSame (new double[] { 11, 11, 22, 11 }) true == allSame (new double[] { 22, 22, 22, 22 }) * */ public static boolean allSame (double[] list) { return StdRandom.bernoulli(); //TODO: fix thisarrow_forwardQ2: a. Write an algorithm that searches a sorted list of n items by dividing it into three sublists of almost n/3 items. This algorithm finds the sublist that might contain the given item and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Dry run the above algorithm to find the value 240. A[] = {10,15,20,60,65,110,150,220,240,245,260,290,300,460,470,501}arrow_forwardplease code in python Forbidden concepts: recursion, custom classes Create a program that finds all the prime numbers between 2 to 1000. As each is found, add it to the list, then print the list in an organized fashion.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning