Concept explainers
Generic bubble sort
Program Plan:
- Import the required packages.
- Create a class “Sorting”:
- Method to sort the numbers gets defined.
- Call the method bubble sort that implements the comparable interface.
- Method to sort the string given.
- Call the method bubble sort to implement the comparator interface.
- Loop that iterates to sort the values of the list.
- Perform swap operation by comparing the list.
- Return the sorted list.
- Define the main method
- Initialize the list that needs to be sorted.
- Call the bubble sort.
- Display the sorted list.
- Initialize list that contain strings.
- Call the method bubble sort.
- Display the sorted list.
The below program is perform a generic bubble sort.
Explanation of Solution
Program:
//define the required packages
import java.util.Comparator;
//define the class sorting
public class Sorting
{
//method to sort the numbers
public static <E extends Comparable<E>> void bubbleSort(E[] list)
{
//perform bubble sort
bubbleSort(list, (e1, e2) -> ((Comparable<E>)e1).compareTo(e2));
}
//method definition
public static <E> void bubbleSort(E[] list,
Comparator<? super E> comparator)
{
//declare the necessary variables
boolean needNextPass = true;
/*loop that iterates to sort the elements of the array*/
for (int k = 1; k < list.length && needNextPass; k++)
{
/*condition when the given array is sorted*/
needNextPass = false;
for (int i = 0; i < list.length - k; i++)
{
/*condition to validate the elements*/
if (comparator.compare(list[i], list[i + 1]) > 0)
{
// swap the elements
E temp = list[i];
//assign values
list[i] = list[i + 1];
//assign the value
list[i + 1] = temp;
/*assign value for the next path*/
needNextPass = true;
}
}
}
}
//define the values
public static void main(String[] args)
{
//define anew list
Integer[] new_list = {-8, 10, 5, 7, 14, 12, -12, 31, 141, 102};
//call the method bubble sort
bubbleSort(new_list);
//loop that iterates to sort the list
for (int i = 0; i < new_list.length; i++)
{
//display the list
System.out.print(new_list[i] + " ");
}
System.out.println();
//define anew list
String[] new_list1 = {"Sorting", "Data", "Fun", "Happy", "
//call bubble sort
bubbleSort(new_list1, (s1, s2) -> s1.compareToIgnoreCase(s2));
//loop that iterates to sort the values
for (int i = 0; i < new_list1.length; i++)
{
//display the list
System.out.print(new_list1[i] + " ");
}
}
}
-12 -8 5 7 10 12 14 31 102 141
Data Fun Happy Nice Programming Sorting
Want to see more full solutions like this?
Chapter 23 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- Find the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forward
- What are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forwardShould software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forward
- I need help to resolve the following activityarrow_forwardModern life has been impacted immensely by computers. Computers have penetrated every aspect of oursociety, either for better or for worse. From supermarket scanners calculating our shopping transactionswhile keeping store inventory; robots that handle highly specialized tasks or even simple human tasks,computers do much more than just computing. But where did all this technology come from and whereis it heading? Does the future look promising or should we worry about computers taking over theworld? Or are they just a necessary evil? Provide three references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage Learning
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning