Question

Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your own code.

718.jpg

7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their way to the top and the larger values “sink” to the bottom. Write a test program that reads in ten double numbers, invokes the method, and displays the sorted numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BubbleSortExample.java

import java.util.Scanner;
public class BubbleSortExample {
        static void bubbleSort(int[] arr) {
            int n = arr.length;
            int temp = 0;
             for(int i=0; i < n; i++){
                     for(int j=1; j < (n-i); j++){
                              if(arr[j-1] > arr[j]){
                                     //swap elements
                                     temp = arr[j-1];
                                     arr[j-1] = arr[j];
                                     arr[j] = temp;
                             }
                            
                     }
             }
    
        }
public static void main(String[] args) {
    int[] arr=new int[10];
    Scanner sc=new Scanner(System.in);
        System.out.println("Please enter ten numbers : ");
        for(int j=0;j<10;j++)
        arr[j]=sc.nextInt();
    System.out.println("Array Before Bubble Sort");
                    for(int i=0; i < arr.length; i++){
                            System.out.print(arr[i] + " ");
                    }
                    System.out.println();
                    
                    bubbleSort(arr);//sorting array elements using bubble sort
                   
                    System.out.println("Array After Bubble Sort");
                    for(int i=0; i < arr.length; i++){
                            System.out.print(arr[i] + " ");
                    }
     
            }
    }

Output:-

Please enter ten numbers : 0 3 5 4 7 0 -1 2 10 90 
Array Before Bubble Sort
0 3 5 4 7 0 -1 2 10 90 
Array After Bubble Sort
-1 0 0 2 3 4 5 7 10 90 
Add a comment
Know the answer?
Add Answer to:
Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square)...

    Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square) A Latin square is an n-by-n array filled with n different Latin letters, each occurring exactly once in each row and once in each column. Write a program that prompts the user to enter the number n and the array of characters, as shown in the sample output, and checks if the input array is a Latin square. The characters are the first n...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT