Question: The bubble sort algorithm is less efficient than it could be. If a pass is made through the list without exchanging any elements, this means

The

bubble sort

algorithm 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. NO BREAK STATEMENT!

-------------------------

(java code that needs to be modified, only a few lines need to be added)

public static

extends

Comparable >

void

bubbleSort(T[ ] data)

{

int

position, scan;

T temp;

for

(position = data.length-1; position >= 0; position--)

{

for

(scan = 0; scan <= position-1

; scan++)

{

if

(data[scan].compareTo(data[scan+1]) > 0)

swap(data, scan, scan+1);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!