COP 2210-_____



COP 2210 Laboratory 10: Selection Sort

Your Name: ____________________________________ Panther-id#:_______________________

Objective

To understand the selection sort algorithm and its Java implementation.

Algorithm

Given a list of n items in random order, perform n-1 passes (scans) over the data. At each pass

• Locate the largest element in the unsorted part of the list (initially, all)

• Swap this element with the last element of the unsorted part of the list

In the table below, n = 10, so the indexes range from 0 to 9.

• Variable pass counts off the passes over the data … 9 passes for 10 items

• Variable last gives the highest index in the unsorted portion of the array

• Variable maxi gives the index of the largest element in the unsorted part

• At each pass, the elements at index maxi and index last are swapped

[pic]

Exercises

1. Complete the table by manually performing the algorithm steps. At each pass,

✓ locate the largest item in the unsorted part and record maxi

✓ swap with the last unsorted item in element last

✓ fill in the remaining (unchanged) elements

2. Run the SelectionSortDemo program until you are sure you understand the logic of the Selection Sort algorithm

3. Can you change the implementation to sort into descending order?

-----------------------

0

1

2

3

4

5

6

7

8

9

pass

last

maxi

57

48

17

88

12

61

27

35

28

36

1

9

3

57

48

17

36

12

61

27

35

28

88

2

8

5

57

48

17

36

12

28

27

35

61

88

3

7

4

6

5

5

6

4

7

3

8

2

9

1

Array Indexes

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download