Download presentation
Presentation is loading. Please wait.
1
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar 7 2006 http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr based on slides by Kurt Eiselt
2
2 News n Remember CSLC available! n Mon-Thu 10-6, Fri 10-4, x150 (near Reboot) n extra TA lab coverage for A2 help: n Tue 4-6 Hastings, 6-8 Leavitt
3
3 Reading n This week: no new reading
4
4 0 185 1 92 2 370 3 485 4 209 5 128 6 84 7 151 8 32 9 563 0 201.25 1 100.50 2 412.75 3 555.25 4 195.00 5 160.00 6 105.00 7 188.75 8 40.00 9 703.75 Recap: Arrays and Object Design n Multiple array approach not very object-oriented n can create arrays of objects n can create objects of our own design... 01234567890123456789 "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science" location cashIn cansSold
5
5 Recap: CokeEmpire n What does this return? myMachines.getCokematic(1).getCansSold() myMachines 01234567890123456789 Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: 201.25 Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn: 100.50 Cokematic numberOfCans: 200 location: "Main Library" cansSold: 370 cashIn: 412.75
6
6 Objectives n Understanding when and how to use n 2D arrays
7
7 Arrays of Arrays 01230123 012012 012012 012012 012012
8
8 01230123 012012 012012 012012 n In any given array, all data must be of same type 0 1 0 2 0
9
9 01230123 Arrays of Arrays n In any given array, all data must be of same type n All arrays in array of arrays must be of same type 0 1 0 2 0 0 1 2 0 1 2 2 4 0 1 3 2 6
10
10 01230123 012012 012012 012012 012012 Arrays of Arrays n In any given array, all data must be of same type n All arrays in array of arrays must be of same type n So easier to use a two-dimensional array!
11
11 Two-Dimensional Arrays n In Java, 2D array implemented internally as array of arrays n but externally syntax of 2D array may seem easier to use 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
12
12 Two-Dimensional Arrays n In Java, 2D array implemented internally as array of arrays n but externally syntax of 2D array may seem easier to use n Typical control structure for computing with 2D array is nested loop n loop within another loop n Let's write program to n load array with values shown n print contents of array 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
13
13 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
14
14 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
15
15 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
16
16 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
17
17 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } for (int col = 0; col < multTable[row].length; col++){ System.out.print(multTable[row][col] + " "); } } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
18
18 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++){ System.out.print(multTable[row][col] + " "); } } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
19
19 Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++){ System.out.print(multTable[row][col] + " "); } System.out.println(); } 01230123 0 1 2 0 0 0 0 1 2 0 2 4 0 3 6 rows columns
20
20 Example: Per-Student Averages n 2D array n each row is student in course n values in each row represent student's quiz scores in course n Print average quiz score for each student n for each row of scores n add up scores n divide by number of quizzes in a row n approach: nested loop 0123401234 95 82 13 96 51 68 63 57 73 71 84 78 50 50 99 70 32 12 scores 0 1 2 3 average of row 0 is 71.5 average of row 1 is 59.75 average of row 2 is 76.5 average of row 3 is 50.0 average of row 4 is 53.25
21
21 Example: Per-Student Averages public class ArrayEx4 { public static void main(String[] args) { double[][] scores = {{95, 82, 13, 96}, {51, 68, 63, 57}, {73, 71, 84, 78}, {50, 50, 50, 50}, {99, 70, 32, 12}}; double average; // here's where we control looping row by row (student by student) for (int row = 0; row < scores.length; row++) { average = 0; // and here's where we control looping through the columns // (i.e., quiz scores) within each row for (int col = 0; col < scores[row].length; col++) { average = average + scores[row][col]; } average = average / scores[row].length; System.out.println("average of row " + row + " is " + average); }
22
22 Example: Per-Quiz Averages n Print average score for each quiz n for each column of scores n add up all scores n divide by number of students n approach: again, nested loop n Switch of outer loop with inner loop, vs. previous 0123401234 95 82 13 96 51 68 63 57 73 71 84 78 50 50 99 70 32 12 scores 0 1 2 3 average of column 0 is 73.6 average of column 1 is 68.2 average of column 2 is 48.4 average of column 3 is 58.6
23
23 Example: Per-Quiz Averages public class ArrayEx5 { public static void main(String[] args) { double[][] scores = {{95, 82, 13, 96}, {51, 68, 63, 57}, {73, 71, 84, 78}, {50, 50, 50, 50}, {99, 70, 32, 12}}; double average; // here's where we control looping column by column (quiz by quiz) for (int col = 0; col < scores[0].length; col++) { average = 0; // and here's where we control looping through the rows // (i.e., students) within each column for (int row = 0; row < scores.length; row++) { average = average + scores[row][col]; } average = average / scores.length; System.out.println("average of column " + col + " is " + average); }
24
24 Sorting n Computers are essential for keeping track and finding large quantities of data n Finding data when necessary is much easier when data is sorted in some way n computer people think a lot about how to sort things: n finding medical records n banking information n income tax returns n driver's license information... n even names in a phone book... n all depend on the information being sorted
25
25 Selection sort 16 3 19 8 12 0 1 2 3 4 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort
26
26 Selection sort 16 3 19 8 12 0 1 2 3 4 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed
27
27 Selection sort 16 3 19 8 12 0 1 2 3 4 The smallest value so far is 16 Its index is 0 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value
28
28 Selection sort 16 3 19 8 12 0 1 2 3 4 The smallest value so far is 3 Its index is 1 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value
29
29 Selection sort 16 3 19 8 12 0 1 2 3 4 The smallest value so far is 3 Its index is 1 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value
30
30 Selection sort 16 3 19 8 12 0 1 2 3 4 The smallest value so far is 3 Its index is 1 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value
31
31 Selection sort 16 3 19 8 12 0 1 2 3 4 The smallest value so far is 3 Its index is 1 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value
32
32 Selection sort 16 3 19 8 12 0 1 2 3 4 The smallest value so far is 3 Its index is 1 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value n Once we've found the minimum value n swap that value with the one we selected at beginning
33
33 Selection sort n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value n Once we've found the minimum value n swap that value with the one we selected at beginning 3 16 19 8 12 0 1 2 3 4 The smallest value so far is 3 Its index is 1 n Let's say want to sort array values in increasing order n one way to approach problem is to use algorithm called selection sort n Start by setting pointer to first element in array n this is where smallest value in array will be placed n Then look at every value in this unsorted array n find minimum value n Once we've found the minimum value n swap that value with the one we selected at beginning
34
34 Selection sort 3 16 19 8 12 0 1 2 3 4 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value
35
35 Selection sort 3 16 19 8 12 0 1 2 3 4 The smallest value so far is 16 Its index is 1 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value n In other words, do everything again to unsorted part of array n in this case, all but first element
36
36 Selection sort 3 16 19 8 12 0 1 2 3 4 The smallest value so far is 16 Its index is 1 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value n In other words, do everything again to unsorted part of array n in this case, all but first element
37
37 Selection sort 3 16 19 8 12 0 1 2 3 4 The smallest value so far is 8 Its index is 3 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value n In other words, do everything again to unsorted part of array n in this case, all but first element
38
38 Selection sort 3 16 19 8 12 0 1 2 3 4 The smallest value so far is 8 Its index is 3 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value n In other words, do everything again to unsorted part of array n in this case, all but first element
39
39 Selection sort 3 16 19 8 12 0 1 2 3 4 The smallest value so far is 8 Its index is 3 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value n In other words, do everything again to unsorted part of array n in this case, all but first element n Now swap minimum value with selected array value n in this case, second element
40
40 Selection sort 3 8 19 16 12 0 1 2 3 4 The smallest value so far is 8 Its index is 3 n At this point we know n smallest number in array is in first element (index 0) n first element is sorted n rest of array remains unsorted n Now select second element of array to be location which will hold next smallest value n In other words, do everything again to unsorted part of array n in this case, all but first element n Now swap minimum value with selected array value n in this case, second element
41
41 Selection sort 3 8 19 16 12 0 1 2 3 4 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before
42
42 Selection sort 3 8 19 16 12 0 1 2 3 4 The smallest value so far is 19 Its index is 2 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before
43
43 Selection sort 3 8 19 16 12 0 1 2 3 4 The smallest value so far is 16 Its index is 3 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before
44
44 Selection sort 3 8 19 16 12 0 1 2 3 4 The smallest value so far is 12 Its index is 4 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before
45
45 Selection sort 3 8 19 16 12 0 1 2 3 4 The smallest value so far is 12 Its index is 4 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before n Again, swap values
46
46 Selection sort 3 8 12 16 19 0 1 2 3 4 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before n Again, swap values
47
47 Selection sort 3 8 12 16 19 0 1 2 3 4 The smallest value so far is 16 Its index is 3 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before n Again, swap values n then do whole thing again
48
48 Selection sort 3 8 12 16 19 0 1 2 3 4 The smallest value so far is 16 Its index is 3 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before n Again, swap values n then do whole thing again
49
49 Selection sort 3 8 12 16 19 0 1 2 3 4 The smallest value so far is 16 Its index is 3 n Now first two elements of array are sorted n Select third element of array to be location of next smallest value n Search unsorted portion of array for that value, just like before n Again, swap values n then do whole thing again n Swap again n not actually necessary in this case n but we follow algorithm
50
50 Selection sort n Are we done? n could select last element of array n (index 4) n but all of array except for last element is already sorted n so last element is largest value in array n and that’s the right place n Yes, array is sorted, and we're done n no need to select last element 3 8 12 16 19 0 1 2 3 4
51
51 Selection sort n Showed arrows moving down array n red arrow on left represents one array index variable n yellow arrow on right represents different one n Consider variables being controlled by loop n red arrow shows outer loop n yellow arrow shows inner loop inside outer loop n Nested loop structure again 0 1 2 3 4 16 3 19 8 12
52
52 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j
53
53 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp
54
54 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0
55
55 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0
56
56 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 0
57
57 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 1 0
58
58 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 1 0
59
59 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 1 0
60
60 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 1 1
61
61 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 2 1
62
62 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 2 1
63
63 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 2 1
64
64 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 3 1
65
65 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 3 1
66
66 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 3 1
67
67 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 4 1
68
68 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 4 1
69
69 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 4 1
70
70 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 5 1
71
71 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 5 1
72
72 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 16 3 19 8 12 i j i j min temp 0 5 1 16
73
73 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 19 8 12 i j i j min temp 0 5 1 16
74
74 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 0 5 1 16
75
75 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 5 1 16
76
76 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 5 1 16
77
77 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 5 1 16
78
78 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 2 1 16
79
79 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 2 1 16
80
80 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 2 1 16
81
81 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 3 1 16
82
82 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 3 1 16
83
83 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 3 1 16
84
84 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 3 3 16
85
85 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 4 3 16
86
86 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 4 3 16
87
87 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 4 3 16
88
88 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 5 3 16
89
89 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 5 3 16
90
90 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 16 19 8 12 i j i j min temp 1 5 3 16
91
91 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 8 12 i j i j min temp 1 5 3 16
92
92 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 1 5 3 16
93
93 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 5 3 16
94
94 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 5 3 16
95
95 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 5 2 16
96
96 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 3 2 16
97
97 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 3 2 16
98
98 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 3 2 16
99
99 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 3 3 16
100
100 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 4 3 16
101
101 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 4 3 16
102
102 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 4 3 16
103
103 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 4 4 16
104
104 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 5 4 16
105
105 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 5 4 16
106
106 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 19 16 12 i j i j min temp 2 5 4 19
107
107 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 12 i j i j min temp 2 5 4 19
108
108 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 2 5 4 19
109
109 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 4 19
110
110 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 4 19
111
111 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 3 19
112
112 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 4 3 19
113
113 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 4 3 19
114
114 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 4 3 19
115
115 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 3 19
116
116 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 3 19
117
117 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 3 16
118
118 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 3 16
119
119 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 3 5 3 16
120
120 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 4 5 3 16
121
121 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 4 5 3 16
122
122 Selection sort 0 1 2 3 4 // selection sort public class SortTest1 { public static void main(String[] args) { int[] numbers = {16,3,19,8,12}; int min, temp; //select location of next sorted value for (int i = 0; i < numbers.length-1; i++) { min = i; //find the smallest value in the remainder of //the array to be sorted for (int j = i+1; j < numbers.length; j++) { if (numbers[j] < numbers[min]) { min = j; } //swap two values in the array temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } System.out.println("Printing sorted result"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } 3 8 12 16 19 i j i j min temp 4 5 3 16
123
123 Tracing with the Debugger
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.