java sorting
Post on 08-Apr-2018
228 Views
Preview:
TRANSCRIPT
-
8/7/2019 java sorting
1/29
Introduction to SortingObjectives:
Understand the importance of sortingdata
Understand the components of a bubblesort
-
8/7/2019 java sorting
2/29
-
8/7/2019 java sorting
3/29
Vocabulary Quiz
Click back button whenfinished with quiz
Click on test icon to checkvocabulary knowledge
Follow directions given onthe web screen
Dave use the following
student to test quiz
Username: calendar
Passwprd: tercel
ap1
-
8/7/2019 java sorting
4/29
Slide 3
jap1 Dave use the following student to test quiz
Username: calendar
Passwprd: tercelJane , 10/4/2006
-
8/7/2019 java sorting
5/29
Array
An array consists of an ordered collection ofsimilar items.
-
8/7/2019 java sorting
6/29
Element
An element is value that is stored in anarray.
-
8/7/2019 java sorting
7/29
One dimensional array
An array in which each data item isaccessed by specifying a single index
-
8/7/2019 java sorting
8/29
Sorting
Aprogramming process which makes surevalues are in either ascending ordescending order
-
8/7/2019 java sorting
9/29
Bubble
Sort
A sorting algorithm that swaps consecutiveelements that are out of order to bubblethe elements to the top or bottom on each
pass of the loop.
-
8/7/2019 java sorting
10/29
Index
The relative position of the components of anarray. Also called a subscript.
-
8/7/2019 java sorting
11/29
Subscript
The relative position of the components ofan array. Also called an index.
-
8/7/2019 java sorting
12/29
Range bound error
The error which occurs when an attempt ismade to use an array index value that isless than 0 or greater than or equal to the
size of the array.
-
8/7/2019 java sorting
13/29
Array.length
This method returns the length of an arraywhich is the number of elements in the array
-
8/7/2019 java sorting
14/29
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.
-
8/7/2019 java sorting
15/29
-
8/7/2019 java sorting
16/29
BubbleSorting
Bubble Sort A bubble sort causes a pass through the
array to compare adjacent pairs of
items.
Whenever two items are out of orderwith respect to each other, they areswapped.
Click mice to see swap code
Click bubbles to see effect of swap
-
8/7/2019 java sorting
17/29
SwapMe
thod
public static void swap(int[] a, int x, int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
-
8/7/2019 java sorting
18/29
Sorting
*Indicates items just compared
and swapped
Sorted item
-
8/7/2019 java sorting
19/29
BubbleSort
The last item will "sink" to the bottom of the array,
and preceding items will gradually bubble" to the
top.
-
8/7/2019 java sorting
20/29
Sorting
Next is a complete Java class to implement a
bubble sort for an array of integers
-
8/7/2019 java sorting
21/29
Bubble Sort Demonstration
Clickto Run
-
8/7/2019 java sorting
22/29
Wrap UpDiscuss the following questions with a neighbor
who is finished with the tutorial.
Be prepared to discuss these questions with the
class.
-
8/7/2019 java sorting
23/29
Discussion Be able to draw a diagram of what is happening
in the swap method
What is always true when one pass of the innerloop of a bubble sort is complete?
What is the purpose of the exchangeMadevariable in the bubble sort?
What one change would need to be made inorder for the array to be in descending order?
-
8/7/2019 java sorting
24/29
-
8/7/2019 java sorting
25/29
Main Sorting ProgramImport 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 enter5 numbers);
System.out.println (and it will sort them in ascendingorder \n)
readNumbers(numbers[]); //click to see code
bubbleSort(numbers[]); //for each method
outputNumbers(numbers[]);
} // end sort
-
8/7/2019 java sorting
26/29
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
-
8/7/2019 java sorting
27/29
Input Data to be Sortedpublic static void inputNumbers(int[] numbers)
{
for (int x= 0; x < numbers.length; x++)
{
numbers{x} = reader.readInt(Please enter a number
to be sorted );
}
-
8/7/2019 java sorting
28/29
SwapMe
thod
public static void swap(int[] a, int x, int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
-
8/7/2019 java sorting
29/29
utput sorted datapublic 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
top related