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)
- You are required to demonstrate competency in any or all of 2D arrays, doubly linked lists, merging of sorted data structures and constructive and destructive programming techniques. Code your solution according to the requirements below. And add comments: Class Tester, method process: Modify the following(in particular method process in class Tester).arrow_forwardPrint the following List in the reversed order using lambda / arrow operator. var array = new List <int>{ 8,2,2,24,5,7,5,3,2,10};arrow_forward1- Start by writing your choice from the list given above. Part a. Explain the important technical details about your choice. 2-(continued) Start by again writing your choice from the list given above. Part b. Most of the ADTs in this class were implemented using arrays and linked lists. If appropriate for the ADT you chose in part a., explain which one (an array or a linked list) you would choose to implement it and why. If it doesn’t make sense to implement your ADT using an array or a linked list, explain which other data structure you would use to implement your ADT.arrow_forward
- can someone help me with this in C++ (not Java) create a program that reads in two matrices from two different text files. Should use a singly or double linked list to store the two matrices and perform the following operations on them: - Add, Subtract, Mulitply, Transpose, and Determinant Requirements: Use singly or doubly linked list date structures only: - No other library methods or existing collection framework. - No use of Array, ArrayList, List, or Vectors for storing. - No two-dimensional arrays. - Input files can only be read Exactly Once for all operations. For the determinant operation, you may augment your linked list node to retain row/column id and employ recursion to directly implement the standard method for computing determinant of a matrix. You are encouraged to design your own node representation (e.g., each node element has two pointers: one to its next right and another to its next bottom element that facilitate both horizontal and vertical traversals like one gets…arrow_forwardUse pointers to add two arrayarrow_forward.“Dangling and wild pointers are known to be problems with pointers”. Justify the given statement with the helpof suitable examplearrow_forward
- Go program using generics Output True Falsearrow_forwardRewrite the Binary search function in the textbook to use a generic type for the array elements. Test the function with arrays of int, double, and string values. *When using test arrays make sure elements are stored in ascending sorted order*arrow_forwardJava- The Insertion sort is efficient for large arrays. True or False?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education