selection sort - cs.appstate.edu · selectionsort() public static void selectionsort(int[] arr) {//...

12
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort Selection Sort Proceed through an array position by Proceed through an array position by position, starting with index 0. At the position, starting with index 0. At the current position, select the element from current position, select the element from the remaining elements that is next in the remaining elements that is next in order and copy it into the current position. order and copy it into the current position.

Upload: others

Post on 26-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection SortSelection Sort

Proceed through an array position by Proceed through an array position by position, starting with index 0. At the position, starting with index 0. At the current position, select the element from current position, select the element from the remaining elements that is next in the remaining elements that is next in order and copy it into the current position.order and copy it into the current position.

Page 2: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort Example Step 1Selection Sort Example Step 1

The ordering begins by locating the The ordering begins by locating the smallest remaining animal, the fish, that is smallest remaining animal, the fish, that is currently in position 1. The operation currently in position 1. The operation occurs by having the owl that currently occurs by having the owl that currently occupies position 0 exchange places with occupies position 0 exchange places with the fish. The effect is to place the the fish. The effect is to place the smallest animal at the front of the list. smallest animal at the front of the list.

Page 3: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort Example Step 2Selection Sort Example Step 2

The tail of the list, starting at position 1 is The tail of the list, starting at position 1 is unordered. The process continues by unordered. The process continues by identifying the smallest animal among the identifying the smallest animal among the owl, dragon, and dog and putting it in owl, dragon, and dog and putting it in position 1. The selection is the owl that position 1. The selection is the owl that is already in its proper position and so that is already in its proper position and so that it exchanges with itself and we can move it exchanges with itself and we can move to the next step. to the next step.

Ordered Unordered

Page 4: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort Example Step 3Selection Sort Example Step 3

With the fish and owl in position, only the With the fish and owl in position, only the last two animals must be ordered. We last two animals must be ordered. We identify the dog as the nextidentify the dog as the next--smallest smallest animal and have it exchange positions animal and have it exchange positions with the dragon. After this step, the tail with the dragon. After this step, the tail of the list has only one item left, which of the list has only one item left, which must be the largest animal. The entire list must be the largest animal. The entire list is ordered. is ordered.

Ordered Unordered

Page 5: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort in ActionSelection Sort in Action

Page 6: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

selectionSortselectionSort()()

public static void selectionSort(int[] arr){

// index of smallest element in the sublistint smallIndex;int pass, j, n = arr.length;int temp;

// pass has the range 0 to n-2for (pass = 0; pass < n-1; pass++){

// scan the sublist starting at index passsmallIndex = pass;

Page 7: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

selectionSortselectionSort() (concluded)() (concluded)// j traverses the sublist// arr[pass+1] to arr[n-1]for (j = pass+1; j < n; j++)

// if smaller element found, assign// smallIndex to that positionif (arr[j] < arr[smallIndex])

smallIndex = j;

// swap the next smallest// element into arr[pass]temp = arr[pass];arr[pass] = arr[smallIndex];arr[smallIndex] = temp;

}}

Page 8: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Selection Sort ExampleSelection Sort Example

// declare an integer arrayint[] arr = {66, 20, 33, 55, 53, 57, 69, 11, 67, 70};// call selectionSort() to order the arrayArrays.selectionSort(arr);System.out.print("Sorted: ");for (int i=0; i < arr.length; i++)

System.out.print(arr[i] + " ");

Output:Sorted: 11 20 33 53 55 57 66 67 69 70

Page 9: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Student QuestionStudent Question

Suppose that I wanted to sort the array Suppose that I wanted to sort the array into descending order, that is, from the into descending order, that is, from the largest element to the smallest element.largest element to the smallest element.How would I change the algorithm?How would I change the algorithm?

Page 10: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

selectionSortselectionSort()()

public static void selectionSort(int[] arr){

// index of smallest element in the sublistint smallIndex;int pass, j, n = arr.length;int temp;

// pass has the range 0 to n-2for (pass = 0; pass < n-1; pass++){

// scan the sublist starting at index passsmallIndex = pass;

Page 11: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

selectionSortselectionSort() (concluded)() (concluded)// j traverses the sublist// arr[pass+1] to arr[n-1]for (j = pass+1; j < n; j++)

// if smaller element found, assign// smallIndex to that positionif (arr[j] < arr[smallIndex])

smallIndex = j;

// swap the next smallest// element into arr[pass]temp = arr[pass];arr[pass] = arr[smallIndex];arr[smallIndex] = temp;

}}

Page 12: Selection Sort - cs.appstate.edu · selectionSort() public static void selectionSort(int[] arr) {// index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length;

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Student QuestionStudent Question

Suppose that I wanted to sort an array Suppose that I wanted to sort an array that contains objects other than integers.that contains objects other than integers.Would I have to create a separate Would I have to create a separate algorithm for each type of data?algorithm for each type of data?