Question
The bubble sort in the given program is less efficient than it could be. If a pass is made through the list without exchanging any
The bubble sort in the given program is less efficient than it could be. If a pass is made through the list without exchanging any elements, this means that the list is sorted and there is no reason to continue. Modify this algorithm so that it will stop as soon as it recognizes that the list is sorted. Do not use a break statement.
Here is some code
class Main
{
/** * Creates an array of Contact objects, sorts them, then prints * them. */
public static void main(String[] args)
{
Contact[] friends = new Contact[7];
friends[0] = new Contact("John", "Smith", "610-555-7384");
friends[1] = new Contact("Sarah", "Barnes", "215-555-3827");
friends[2] = new Contact("Mark", "Riley", "733-555-2969");
friends[3] = new Contact("Laura", "Getz", "663-555-3984");
friends[4] = new Contact("Larry", "Smith", "464-555-3489");
friends[5] = new Contact("Frank", "Phelps", "322-555-2284");
friends[6] = new Contact("Marsha", "Grant", "243-555-2837");
bubbleSort(friends);
for (Contact friend: friends)
System.out.println(friend);
}
/** * Sorts the specified array of objects using a bubble sort * algorithm. Stops when the list is sorted. * *
@param data the array to be sorted */
public static < T extends Comparable
{
int position;
int scan;
for (position = data.length - 1; position >= 0; position -= 1)
{
for (scan = 0; scan <= position - 1; scan += 1)
{
if (data[scan].compareTo(data[scan + 1]) > 0)
{
swap(data, scan, scan + 1);
}
}
}
}
private static < T extends Comparable < T >>
void swap(T[] data, int index1, int index2)
{
T temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
}
/** * Contact represents a phone contact. * * @author Java Foundations * @version 4.0 */
public class Contact implements Comparable
{
private String firstName, lastName, phone;
/** * Sets up this contact with the specified information. * * @param first a string representation of a first name * @param
last a string representation of a last name * @param telephone a string representation of a phone number */
public Contact(String first, String last, String telephone)
{
firstName = first;lastName = last;
phone = telephone;
}
/** * Returns a description of this contact as a string. * * @return a string representation of this contact */
public String toString()
{
return lastName + ", " + firstName + "\t" + phone;
}
/** * Uses both last and first names to determine lexical ordering. * * @param other the contact to be compared to this contact * @return the integer result of the comparison */
public int compareTo(Contact other)
{
int result;
if (lastName.equals(other.lastName))
result = firstName.compareTo(other.firstName);
elseresult = lastName.compareTo(other.lastName);
return result;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started