Case in point: the words are ordered alphanumerically in a dictionary and looking for a word in a dictionary is easy. Just imagine if a dictionary contains all the words in an unordered manner - it would be impossible to find the definition of a word in such a dictionary !!!
This sorting algorithm is not in your text book... (I have no idea why...)
Let's see what happens if we apply the procedure one more time:
It looks better, so let's see what happens if we apply the procedure one more time:
It looks even better, so let's see what happens if we apply the procedure one more time:
Now, let's try to write the steps down - precisely...
Clearly, if the list of value is NOT YET sorted , we should keep going.
But, if the list of value is sorted , we should STOP !
Answer:
int[] data = new int[...]; "fill data with value" boolean done; // The variable will tell us if we need to continue done = false; while ( ! done ) { done = true; // Set to true to detect if some pair is swapped for "every pair of values in data" { if ( "pair of value is out of order" ) { "swap the pair of values"; done = false; // Detect a swap - need to continue... } } // NOTE: done will be "true" if all pair were in correct order ! } |
The algorithm compares all these pairs of values:
We can use an index i running from 0 to data.length - 2:
for (i = 0; i <= data.length - 2; i++) { ... compare data[i] and data[i+1] ... }
The pair of values compared are: data[i] and data[i+1]
Easy:
for (i = 0; i <= data.length - 2; i++) { if ( data[i] > data[i+1] ) { } }
This is a classic and we have seen it before (three way exchange): (see: click here )
int help; for (i = 0; i <= data.length - 2; i++) { if ( data[i] > data[i+1] ) { help = data[i]; data[i] = data[i+1]; data[i+1] = help; done = false; // Detect a swap - need to continue... } }
int[] data = new int[...]; int help; "fill data with value" /* ------------------------ Bubble Sort Algorithm ------------------------ */ boolean done; // The variable will tell us if we need to continue done = false; while ( ! done ) { done = true; // Set to true to detect if some pair is swapped for (i = 0; i <= data.length - 2; i++) { if ( data[i] > data[i+1] ) { help = data[i]; data[i] = data[i+1]; data[i+1] = help; done = false; // Detect a swap - need to continue... } } // NOTE: done will be "true" if all pair were in correct order ! } |
String[] data = new String[...]; // Array of String (objects) "fill data with value" boolean done; // The variable will tell us if we need to continue done = false; while ( ! done ) { done = true; // Set to true to detect if some pair is swapped for "every pair of values in data" { if ( "pair of value is out of order" ) { "swap the pair of values"; done = false; // Detect a swap - need to continue... } } // NOTE: done will be "true" if all pair were in correct order ! } |
The algorithm compares all these pairs of values:
This step does not change when the type of the data is changed
We can use:
for (i = 0; i <= data.length - 2; i++) { ... compare data[i] and data[i+1] ... }
The pair of values compared are: data[i] and data[i+1]
We need to express the fact that we want to compare the values on basis of they are Strings.
data[i].compareTo(data[i+1]) will compares Strings
data[i] and data[i+1]
data[i].compareTo(data[i+1]) will returns a
positive value
when the strings are out of order.
for (i = 0; i <= data.length - 2; i++) { if ( data[i].compareTo(data[i+1]) > 0 ) { } }
This is a classic algorithm and we don't change a classic. But we do need the right type of help variable to make the exchange possible.
String help; for (i = 0; i <= data.length - 2; i++) { if ( data[i] > data[i+1] ) { help = data[i]; data[i] = data[i+1]; data[i+1] = help; done = false; // Detect a swap - need to continue... } }
String[] data = new String[...]; String help; "fill data with value" /* ------------------------ Bubble Sort Algorithm ------------------------ */ boolean done; // The variable will tell us if we need to continue done = false; while ( ! done ) { done = true; // Set to true to detect if some pair is swapped for (i = 0; i <= data.length - 2; i++) { if ( data[i].compareTo(data[i+1]) > 0 ) { help = data[i]; data[i] = data[i+1]; data[i+1] = help; done = false; // Detect a swap - need to continue... } } // NOTE: done will be "true" if all pair were in correct order ! } |
Information are often sorted for easy access...
Computers are ideally suited to do this mundane task...
Item[] data = new Item[...]; // Array of Item objects "fill data with value" boolean done; // The variable will tell us if we need to continue done = false; while ( ! done ) { done = true; // Set to true to detect if some pair is swapped for "every pair of values in data" { if ( "pair of value is out of order" ) { "swap the pair of values"; done = false; // Detect a swap - need to continue... } } // NOTE: done will be "true" if all pair were in correct order ! } |
The algorithm compares all these pairs of values:
This step does not change when the type of the data is changed
We can use:
for (i = 0; i <= data.length - 2; i++) { ... compare data[i] and data[i+1] ... }
The pair of values compared are: data[i] and data[i+1]
We need to express the fact that we want to compare the Items on basis of the name fields - which is a String.
data[i].getName() will return the name in data[i]
data[i+1].getName() will return the name in data[i+1]
data[i].getName().compareTo(data[i+1].getName()) compares the names in data[i] and data[i+1]
for (i = 0; i <= data.length - 2; i++) { if ( data[i].getName().compareTo(data[i+1].getName()) > 0 ) { } }
This is a classic algorithm and we don't change a classic. But we do need the right type of help variable to make the exchange possible.
Item help; for (i = 0; i <= data.length - 2; i++) { if ( data[i] > data[i+1] ) { help = data[i]; data[i] = data[i+1]; data[i+1] = help; done = false; // Detect a swap - need to continue... } }
Item[] data = new Item[...]; Item help; "fill data with value" /* ------------------------ Bubble Sort Algorithm ------------------------ */ boolean done; // The variable will tell us if we need to continue done = false; while ( ! done ) { done = true; // Set to true to detect if some pair is swapped for (i = 0; i <= data.length - 2; i++) { if ( data[i].getName().compareTo(data[i+1].getName()) > 0 ) { help = data[i]; data[i] = data[i+1]; data[i+1] = help; done = false; // Detect a swap - need to continue... } } // NOTE: done will be "true" if all pair were in correct order ! } |