Sie sind auf Seite 1von 16

PRESENTATION

SELECTIO
N SORT

Why we do sorting?

Searching for an element in an array


will be more efficient. (example: looking
for a particular phone number).
Examples of sorting:
List containing exam scores sorted from Lowest
to Highest or from Highest to Lowest
List of student records and sorted by student
number or alphabetically by first or last name.

INTRODUCTION

In computer science, selection sort is a sorting


algorithm, specifically an in-place comparison
sort.

Selection sort is among the simplest of sorting


techniques and it work very well for small
files.

DEFINITION

Selection sort carries out a sequence


of passes over the table. At the first
pass an entry is selected on some
criteria and placed in the correct
position in the table. The possible
criteria for selecting an element are to
pick the smallest or pick the largest.

KINDS OF SELECTION
SORT

There are two ways through which we can do


sorting through selection sort:
1. ASCENDING ORDER
2. DESCENDING ORDER

ASCENDING ORDER

For sorting in ascending order, the correct position to put it is at the


beginning of the table. Now that the correct entry is in the first
place in the table the process is repeated on the remaining entries.

ALGORITHM:
i. Find smallest element (of remaining elements).
ii. Swap smallest element with current element (starting
at index 0).
iii. Finished if at the end of the array. Otherwise, repeat 1
and 2 for the next index.

ALGORITHM (cont.):

FOR i = 1 TO n - 1
min = i
FOR j = i + 1 TO n
IF x(min) > x(j) THEN
min = j
END IF
temp = x(i)
x(i) = x(min)
x(min) = temp

EXAMPLE

SORT 70 75 89 61 37 IN ASCENDING ORDER?

70 75 89 61 37

Smallest is 37
Swap with index 0

37 75 89 61 70
Smallest is 61
Swap with index 1

37 61 89 75 70
Smallest is 70
Swap with index 2

37 61 70 75 89
Smallest is 75
Swap with index 3
Swap with itself

37 61 70 75 89
Dont need to do
last element
because theres
only one left

37 61 70 75 89

DESCENDING ORDER

For sorting in descending order, the correct position to put it is


at the end of the table. Now that the correct entry is in the last
place in the table the process is repeated on the remaining
entries.
ALGORITHM:
i.
ii.

Find largest element (of remaining elements).


Swap largest element with current element (starting
at index 0).
iii. Finished if at the end of the array. Otherwise, repeat 1
and 2 for the next index.

ALGORITHM (cont.):

FOR i = 1 TO n - 1
max = i
FOR j = i + 1 TO n
IF x(max) < x(j) THEN
max = j
END IF
temp = x(i)
x(i) = x(max)
x(max) = temp

EXAMPLE

SORT 84 98 35 1 67 IN DESCENDING ORDER?

98 84 67 1 35
Largest is 35
Swap with index 3

98 84 67 35 1
Dont need to do last
element because
theres only one left

98 84 67 35 1

SUMMARY FOR TIME


COMPLEXITY

Time efficiency

Comparisons: n(n-1)/2
Exchanges: n (swapping
largest into place)
Overall: (n2), best and worst
cases

WHY USE IT?

Memory required is small


Size of array (youre using this
anyway)
Size of one variable (temp variable for
swap)
Selection sort is useful when you have
limited memory available
Relatively efficient for small arrays

SUMMARY

Selection sort is among the simplest


of sorting techniques and it work
very well for small files.
Furthermore, despite its evident
"nave approach "Selection sort has
a quite important application
because each item is actually
moved at most once, Section sort is
a method of choice for sorting files

REFERENCE

http://www.personal.kent.edu/~rmuhamma/Al
gorithms/MyAlgorithms/Sorting/selectionSort.h
tml

http://courses.cs.vt.edu/~csonline/Algorithms/
Lessons/SelectionCardSort/selectioncardsort.ht
ml

THANK YOU!