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…
Exercise 1:In this problem, we would like to implement a variation of the Bubble Sort algorithm. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. The algorithm is illustrated as in the following figure:
For the first step, we perform bubble sort from the index 1 to n (n is thenumber of elements in the array).
The next step, we perform a reserved bubble sort from the index n to 1.
The process is repeated until all the array is sorted.
Propose a pseudo-code to complete the Bubble Sort algorithm. Implement and test this algorithm in C/C++. Analyze and compute the complexity of this algorithm in the best, average and worst scenarios.Exercise 2:Re-implement Exercise 1 using a linear data structure: List, Stack, Queue. Justify your choice of data structure.
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
- Fast pleasearrow_forwardGiven the following array of numbers: 8 2 3 9 10 1 4 6 7 5 Show what the array looks like after each iteration of the following sorting algorithms: Bubble Selection Insertion Mergesort Only show the array contents with each algorithm. You do not need to show function call instances if recursion is used or write any code. Just show the array at key iterations of the algorithm. You can use your own words to describe them as well for more detail (but do not write any code). Show what the array looks like after each recursive iteration of the Quicksort algorithm.arrow_forwardWrite a version of the sequential search algorithm that can be used to search a sorted list.arrow_forward
- A binary search only works if the values in the list are sorted. A bubble sort is a simple way to sort entries. The basic idea is to compare two adjacent entries in a list-call them entry[j] and entry[j+1]. If entry[j] is larger, then swap the entries. If this is repeated until the last two entries are compared, the largest element in the list will now be last. The smallest entry will ultimately get swapped, or "bubbled" up to the top. The algorithm could be described in C as: last = num; while (last > 0) { pairs = last – 1: for (j = 0; j entry (j+1] { temp = entry[il: entryli] = entrylj+1]; entrylj+1] = temp; last = i: } } Here, num is the number of entries in the list. Write an assembly language program to implement a bubble sort algorithm, and test it using a list of 8 elements. Each element should be a halfword in length. Please show your code works with the Keil tools or VisUAL, by grabbing a screen shot with your name somewhere on the screen.arrow_forwardCodeWorkout Gym Course Search exercises... Q Search kola_shreya@columbusstate.edu X173: array220 Given an array of int 5, compute recursively if the array contains somewhere a value followed immediately by that same value times 10. We'll use the convention of considering only the part of the array that begins at the given index.In this way, a recursive call can pass index + 1 to move down the array. The initial call will pass in an index position of 0. Your Answer: 1 public boolean array220(int[] nums, int index) 4} Check my answer! Reset Next exercise Feedbacz CodeWorkout © Virginia Tech About License Privacy Contact H 2 M 45arrow_forwardApply 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_forward
- The 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_forwardCode for an algorithm to: We begin with two pointers, keeping a low and a high -> finding the midpoint and comparing it to the number we want to discover. If the goal number is greater, we move to the right because the array is sorted. If it's less than that, we shift to the left because it can't be on the right side, where all the numbers are greater than the midpoint.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
- Quicksort SPLIT (the 2-pointer algorithm covered in class) is applied to the integer array [4,3,5,7,9,2,1], using the first entry as the pivot. Show the series of item swaps that are performed in the split process, and the array after each of these swaps, up to and including the step of placing the pivot at its correct location. You only need to show the split of the original array, you are not required to continue working on the subarrays after the split.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_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_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