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)
- (Implement set operations in MyList)(JAVA) Please use class name: Exercise_01 The implementations of the methods addAll, removeAll, retainAll, toArray(), and toArray(T[]) are omitted in the MyList interface. Implement these methods. Test your new MyList class using the code at https://liveexample.pearsoncmg.com/test/Exercise24_01.txt. Sample Output: Enter five strings for array name1 separated by space: TomGeorgePeterJeanJaneEnter five strings for array name2 separated by space: TomGeorgeMichaelMichelleDanielEnter two strings for array name3 separated by space: TomPeterlist1:[Tom, George, Peter, Jean, Jane]list2:[Tom, George, Michael, Michelle, Daniel]After addAll:[Tom, George, Peter, Jean, Jane, Tom, George, Michael, Michelle, Daniel] list1:[Tom, George, Peter, Jean, Jane]list2:[Tom, George, Michael, Michelle, Daniel]After removeAll:[Peter, Jean, Jane] list1:[Tom, George, Peter, Jean, Jane]list2:[Tom, George, Michael, Michelle, Daniel]After retainAll:[Tom, George] list1:[Tom, George,…arrow_forwardDon't use vector array .using ifstream and ofstream. C++ programarrow_forward(Average the major diagonal in a matrix) Write a method that averages all the numbers in the major diagonal in an n * n matrix of double values using the following header: public static double averageMajorDiagonal(double[][] m) Write a test program that reads a 4-by-4 matrix and displays the average of all its elements on the major diagonal. Here is a sample run: 1 2 3 4.0 5 6.5 7 8 9 10 11 12 13 14 15 16 Average of the elements in the major diagonal is 8.625arrow_forward
- apartmentList is a pointer to the first node in a linked list of nodes. Use this for Parts a, b, and c. Part a. Create an inner class that will hold the following information: the apartment number of the apartment (an int), the number of bedrooms in the apartment (also an int), the current rent of the apartment (a double). Don't forget to include a field for the link! Just create the class itself and the instance variables; you don't need to put any methods into the class.arrow_forwardC# (Generic Method IsEqualTo) Write a simple generic version of method IsEqualTo that compares its two arguments with the Equals method, and returns true if they’re equal and false otherwise. Use this generic method in a program that calls IsEqualTo with a variety of simple types, such as object or int. What result do you get when you attempt to run this program?arrow_forwardDon't use vector array .using c++ languagearrow_forward
- Topic: Singly Linked ListImplement the following functions in C++ program. Read the question carefully. Below is the "CODE THAT NEEDS TO BE IMPROVED" or "NOT FINAL CODE" (See attached photo for reference) bool addAt(int num, int pos) This method will add the integer num to the posth position of the list and returns true if the value of pos is valid. Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the linked list will now look like this: 10 -> 20 -> 30 -> 40 -> 50. When the value of pos is invalid i.e. greater than the size + 1 or less than one, return false. bool addAt(int num, int pos) { if (pos == 1) { // case 1: addHead addHead(num); return true; } if (pos == index + 1) { // case 3: addTail addTail(num); return true; } node* currnode = head; //addAt(20, 3) int count = 0; while (currnode…arrow_forwardPlease help with the follow question in C++arrow_forwardDefine the term " pointer generic " .arrow_forward
- (Circular linked lists) This chapter defined and identified various operations on a circular linked list.a. Write the definitions of the class circularLinkedList and its member functions. (You may assume that the elements of the circular linked list are in ascending order.)b. Write a program to test various operations of the class defined in (a).arrow_forwardTopical Information Use C++. The purpose of this project is to test your ability to use templates, dynamic memory (mixed with classes), operator overloading, and libraries effectively in program design. Program Information Create a template class for a dynamic 1D array. You can model your class off of our String class or the dynamic list class. Changes from String class: You won't need an end-of-string element in the array. The element type is now templated. Translation will no longer be necessary. operators - and -= 'might' now make sense (erase element(s)?). ... Show how useful your template array class is by creating arrays of short integers doubles String class objects (static) Point class objects pointers to Point class objects (each allocated on the heap) — all in one test application. Make sure your test application is a good/thorough test of your class. (Your test application might utilize the applyand accumulatefunctions from lecture to facilitate testing. Also note how a…arrow_forward(Generic binary search) Implement the following method using binary search. public static > int binarySearch(E[] list, E key)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