Introduction to Sorting
Objectives:
Understand the importance of sorting data
Understand the components of a bubble
sort
Sorting/Array Vocabulary
Click on the vocabulary term to see its definition
array sorting
element bubble sort
index subscript
one-dimensional array range bound error
.length
Now click on “bubble” link to test your
Knowledge of these vocabulary terms.
Dave use the following
Vocabulary Quiz student to test quiz
Username: calendar
Passwprd: tercel
Click on test icon to check
vocabulary knowledge
Follow directions given on
the web screen
Click “back” button when
finished with quiz
Array
An array consists of an ordered collection of
similar items.
Element
An element is value that is stored in an
array.
One dimensional array
An array in which each data item is
accessed by specifying a single index
Sorting
A programming process which makes sure
values are in either ascending or
descending order
Bubble Sort
A sorting algorithm that swaps consecutive
elements that are out of order to bubble
the elements to the top or bottom on each
pass of the loop.
Index
The relative position of the components of an
array. Also called a subscript.
Subscript
The relative position of the components of
an array. Also called an index.
Range bound error
The error which occurs when an attempt is
made to use an array index value that is
less than 0 or greater than or equal to the
size of the array.
Array.length
This method returns the length of an array
which is the number of elements in the array
Sorting
When the elements of an array are in random order,
we need to rearrange them before we can take
advantage of any ordering.
This process is called sorting.
Suppose we have an array of five integers that we
wish to sort from smallest to largest.
Bubble Sorting
Bubble Sort
A bubble sort causes a pass through the
array to compare adjacent pairs of items.
Whenever two items are out of order
with respect to each other, they are
swapped.
Click mice to see swap code
Click “bubbles” to see effect of swap
Swap Method
public static void swap(int[] a, int x, int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
Sorting
Sorted item
*Indicates items just compared
and swapped
Bubble Sort
The last item will "sink" to the bottom of the array,
and preceding items will gradually “bubble" to the
top.
Sorting
Next
is a complete Java class to implement a
bubble sort for an array of integers
Bubble Sort Demonstration
Click to Run
Wrap Up
Discuss the following questions with a neighbor
who is finished with the tutorial.
Be prepared to discuss these questions with the
class.
Discussion
Be able to draw a diagram of what is happening
in the swap method
What is always true when one pass of the inner
loop of a bubble sort is complete?
What is the purpose of the exchangeMade
variable in the bubble sort?
What one change would need to be made in
order for the array to be in descending order?
Try It!!!
Use the data on this page to find the slides which contain the
complete bubble sorting code
They are the last 5 slides of the presentation
Copy and paste the code into BlueJ
Compile the code
Run the program
Main Program
Input Data Method
Bubble Method
Swap Method
Output Data Method
Main Sorting Program
Import TerminalIO.*;
Public class Sort
{
public static void main(String [] args)
{
KeyboardReader reader = new KeyboardReader();
int [] numbers = new int[5]
System.out.println(“This program will allow you to enter 5
numbers”);
System.out.println (“and it will sort them in ascending order \n”)
readNumbers(numbers[]); //click to see code
bubbleSort(numbers[]); //for each method
outputNumbers(numbers[]);
} // end sort
public static void bubbleSort(int[] numbers)
{
int k = numbers.length-1;
boolean exchangeMade = true;
// Make up to n - 1 passes through array, exit early if no exchanges
//are made on previous pass
while k > 0 && exchangeMade == true)
{
exchangeMade = false;
k--;
for (int j = 0; j < k; j++)
{
if (a[j] > a[j + 1])
{
swap(a, j, j + 1); //click to see swap method
exchangeMade = true;
}
} // end for
} // end while
} //end method
Input Data to be Sorted
public static void inputNumbers(int[] numbers)
{
for (int x= 0; x < numbers.length; x++)
{
numbers{x} = reader.readInt(“Please enter a number
to be sorted “);
}
Swap Method
public static void swap(int[] a, int x, int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
Output sorted data
public static void outputNumbers(int[] numbers)
{
System.out.println ( “\nThe list in sorted order is”);
for int x = 0; x < numbers.length; x++)
{
System.out.println(numbers[x]);
}
} // end method