Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
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
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 16.1, Problem 16.1CP

Explanation of Solution

Bubble sort:

This is simple sorting algorithm to sort the elements in ascending order or descending order.

  • 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.

Blurred answer
Students 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)

Knowledge Booster
Background pattern image
Computer Science
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
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage