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.1CP
Explanation of Solution
Bubble sort:
This is simple sorting
- This algorithm makes the several passes through the elements of the array and the smallest elements are “bubble up” to the top of the array.
- On each pass, the larger elements are “bubbled” towards the end of the array.
- After all the passes are completed, the elements are arranged from smallest to biggest in order.
Example:
Consider the list of elements (4, 8, 2, 6) to perform the bubble sort.
Pass 1:
- Compare the elements in the first pair (4 and 8); no swap is needed because the pair is already in order.
- Now compare the elements in the second pair (8 and 2); the value 8 is higher than the value 2. So, swap the values 8 and 2.
- Compare the elements in the third pair (8 and 6); the value 8 is higher than the value 6. So, swap the values 8 and 6.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
For selection sort, how many comparisons would be needed to sort an array
containing 100 elements if the original array elements were already sorted?
What if the original array elements were sorted in reverse order?
What if the original array elements were all identical?
Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sorted. On the next iteration, take the two adjacent elements to the sorted portion of the array. If they are out of order with respect to each other, then swap them. Now, push the left element toward the right in the array so long as it is greater than the element to its right. And push the right element toward the left in the array so long as it is less than the element to its left. The algorithm begins by processing the middle two elements of the array if the array is even. If the array is odd, then skip processing the middle item and begin with processing the elements to its immediate left and right. Implement Double Insertion Sort, being careful to properly handle both when the array is odd and when it is even.
Improved Bubble Sort: One possible improvement for Bubble Sort would be to add a flag variable and a test that…
Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sorted. On the next iteration, take the two adjacent elements to the sorted portion of the array. If they are out of order with respect to each other, then swap them. Now, push the left element toward the right in the array so long as it is greater than the element to its right. And push the right element toward the left in the array so long as it is less than the element to its left. The algorithm begins by processing the middle two elements of the array if the array is even. If the array is odd, then skip processing the middle item and begin with processing the elements to its immediate left and right. Implement Double Insertion Sort, being careful to properly handle both when the array is odd and when it is even.
Implement the Double Insertion sort algorithm by using JAVA on a randomly generated list of N integer Your program…
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
- Double Insertion Sort is a variation on Insertion Sort that works from the middle of the array out. At each iteration, some middle portion of the array is sorted. On the next iteration, take the two adjacent elements to the sorted portion of the array. If they are out of order with respect to each other, then swap them. Now, push the left element toward the right in the array so long as it is greater than the element to its right. And push the right element toward the left in the array so long as it is less than the element to its left. The algorithm begins by processing the middle two elements of the array if the array is even. If the array is odd, then skip processing the middle item and begin with processing the elements to its immediate left and right. Implement Double Insertion Sort, being careful to properly handle both when the array is odd and when it is even. By using java, Implement the Double Insertion sort algorithm on a randomly generated list of N integer numbers. Your…arrow_forwardWrite a modified version of the selection sort algorithm that selects the largest element each time and moves it to the end of the array, rather than selecting the smallest element and moving it to the beginning. Will this algorithm be faster than the standard selection sort? What will its complexity class (big-Oh) be?arrow_forwardSort the array (D, G, J, F, A, C) using selection sort (show the array after each step).arrow_forward
- What is the tradeoff between using an unordered array versus an ordered array ?arrow_forwardWrite a program that reads the numbers and sorts them by using the Counting Sort algorithm and finally search a number from that array using Linear Search Algorithm. Input: 3 6 5 4 789 Search Item: 7 Output: Sorted Array: 3 4 5 6 789 Search item 7 is found.arrow_forwardWhat are the advantages and disadvantages of using an unordered array as opposed to an ordered one?arrow_forward
- Question 8 Sort the following numbers using an "in place" version of a selection sort. This means that you should have only one array throughout and all elements should be present at all times. Show each "pass" of the algorithm. 34, 25, 11, 44, 21, 8, 4, 28, 16, 31arrow_forwardusing selection sort algorithm, the intermediate sorting results of sorting the array (1,3,24,19,5,2} in Descending order are: starting: {12,4,25,20,6,3,100} Result of the first iteration: Result of the second iteration: Result of the third iteration: Result of the fourth iteration: { Result of the fifth iteration:arrow_forwardCreate a sort algorithm that counts the variety of key values before sorting the array using key-indexed counting using a symbol table. (This technique should not be used if there are many different key values.)arrow_forward
- An inversion in an array is a pair of elements that are "out of order," meaning that the element that occurs earlier in the array is bigger than the one that occurs later. What is the largest-possible total number of inversions an 8-element array can have? O 64 O 15 O None of the choices O 7 O 28 O 32arrow_forwardThe binary search algorithm that follows may be used to search an array when the elements are in order. This algorithm is analogous to the following approach to finding a name in a telephone book. a. Open the book in the middle and look at the middle name on the page. b. b. If the middle name isn't the one, you're looking for, decide whether it comes before or after the name you want. c. Take the appropriate half of the section of the book you were looking in and repeat these steps until you land on the name. 1. Let the bottom be the subscript of the initial array element. 2. Let the top be the subscript of the last array element. 3. Let found be false. 4. Repeat as long as the bottom isn't greater than the top and the target has not been found. 5. Let middle be the subscript of the element halfway between bottom and top. 6. If the element in the middle is the target 7. Set found to true and index to middle. else if the element in the middle is larger than the target 8. Let the top be…arrow_forwardMake a programme that compares the four advanced sorting algorithms covered in this chapter. Create a 1,000-element randomly generated array for the tests. What is the algorithm's ranking? What happens when the array size is increased to 10,000, then 100,000 elements?arrow_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