Docsity

Selection Sort Algorithm: A Simple Approach to Sorting and Understanding Big-Oh Notation, Exams of Computer Science

Toggle sidebar
bg1
CMSC 131: Chapter 30 (Supplement)
CMSC 131: Chapter 30 (Supplement)
Sorting
Sorting
Sorting and Searching
Sorting and Searching
Sorting: Given a list of items from any ordered domain (integers, doubles, Strings,
Dates, …) permute (rearrange) the elements so they are in increasing order.
Searching: Given a list of items and given a designated query item q, determine
whether q appears in the list, and if so, then where.
Sorting and Searching: are two of the most fundamental tasks in all of computer
science.
– Although these may seem pretty simple, there are many different ways to solve these
problems.
– These approaches are quite different in terms of complexity and efficiency.
– There is a 722 page book (by D. E. Knuth) devoted to just these two topics alone!
Note: We will talk only about Sorting (one type of Sort: Selection Sort). You will see
Search in CMSC 132.
Selection Sort
Selection Sort
Selection Sort: one of the simplest sorting algorithms known. (Recall: an
algorithm is a method of solving a problem.)
Input Argument: Let a denote the array to be sorted. We assume only that the
elements of a are from any class that implements the Comparable interface,
that is, it defines a public method:
int compareTo( Object obj )
This compares the current object to object obj. It returns:
< 0 if this < obj
== 0 if this == obj
> 0 if this > obj
Examples of Objects that implement Comparable:
– All the numeric wrappers (Integer, Double, Float, etc.)
–String
– Any class of yours, for which you define compareTo( ).
pf3
pf4
pf5

Partial preview of the text

Download Selection Sort Algorithm: A Simple Approach to Sorting and Understanding Big-Oh Notation and more Exams Computer Science in PDF only on Docsity!

CMSC 131: Chapter 30 (Supplement)

CMSC 131: Chapter 30 (Supplement)

Sorting

Sorting

Sorting and Searching

Sorting and Searching

Sorting: Given a list of items from any ordered domain (integers, doubles, Strings,

Dates, …) permute (rearrange) the elements so they are in increasing order.

Searching : Given a list of items and given a designated query item q, determine

whether q appears in the list, and if so, then where.

Sorting and Searching : are two of the most fundamental tasks in all of computer

science.

  • Although these may seem pretty simple, there are many different ways to solve these

problems.

  • These approaches are quite different in terms of complexity and efficiency.
  • There is a 722 page book (by D. E. Knuth) devoted to just these two topics alone!

Note: We will talk only about Sorting (one type of Sort: Selection Sort). You will see

Search in CMSC 132.

Selection Sort

Selection Sort

Selection Sort : one of the simplest sorting algorithms known. ( Recall : an

algorithm is a method of solving a problem.)

Input Argument : Let a denote the array to be sorted. We assume only that the

elements of a are from any class that implements the Comparable interface ,

that is, it defines a public method:

int compareTo( Object obj )

This compares the current object to object obj. It returns:

< 0 if this < obj

== 0 if this == obj

0 if this > obj

Examples of Objects that implement Comparable :

  • All the numeric wrappers (Integer, Double, Float, etc.)
  • String
  • Any class of yours, for which you define compareTo ( ).

Selection Sort AlgorithmSelection Sort Algorithm

Selection Sort : Works as follows. Let a[0 … n-1] be the array to be sorted.

for ( i running from 0 up to n-1 ) {

Let j = index of the smallest of a[i], a[i+1], … a[n-1];

swap a[i] with a[j];

}

Example:

Selection Sort Selection Sort

public static void selectionSort( Comparable[ ] a ) {

for ( int i = 0; i < a.length; i++ ) {

int j = indexOfMin( i, a );

swap( i, j, a );

}

}

private static int indexOfMin( int start, Comparable[ ] a ) {

Comparable min = a[start];

int minIndex = start;

for ( int i = start + 1; i < a.length; i++ )

if ( a[i].compareTo( min ) < 0 ) {

min = a[i];

minIndex = i;

}

return minIndex;

}

private static void swap( int i, int j, Comparable[ ] a ) {

Comparable temp = a[i];

a[i] = a[j];

a[j] = temp;

}

i j

swa

p

i j

swa

p

i j

swa

p

i j swap

i j swap

i j swap

3 5 11 8 15 6

3 5 6 8 15 11

3 5 6 8 15 11

3 5 6 8 11 15

8 6 11 3 15 5

3 6 11 8 15 5

Running time of Selection Sort

Running time of Selection Sort

Running time depends on the contents of the array :

  • Length : The principal determinant of running time is the number of elements, n , in

the array.

  • Contents : For some sorting algorithms, the running time may depend on the contents

of the array. E.g., some algorithms run faster if the initial array is nearly sorted.

  • Worst-case running time : Among all arrays of length n, consider the one that has

the highest running time.

  • Average-case running time : Average the running time over all arrays of length n.

(This is very messy , so we won’t do it.)

Worst-case running time :

  • Let T(n) denote the time (measured as the number of calls to compareTo( )) required

in the worst-case to sort an array of n items using Selection Sort.

Running time of Selection Sort

Running time of Selection Sort

How many times is compareTo( ) called? Call this T(n).

  • Let n denote the length of array a.
  • We go through the for-loop in selectionSort( ) exactly n times ,

for i = 0, 1, 2, …, n-1.

  • Each time we call indexOfMin(i, a), we call compareTo( ) to compare the min to each

element of the subarray a[i+1, …, n-1].

  • In general, any subarray a[j, …, k] contains k – j + 1 elements.
  • Thus, each call to indexOfMin(i, a) makes (n-1) - (i+1) + 1 = n-i-1 calls to

compareTo( ).

  • To compute T(n), we simple add up (n-i-1) , for i = 0, 1, …, n-1:

i=0 (n-0-1) = n-

i=1 (n-1-1) = n-

i=2 (n-2-1) = n-

…

i=n-2 (n-(n-2)-1) = 1

i=n-1 (n-(n-1)-1) = 0

n 1

i 0

T (n) 0 1 (n 3) (n 2) (n 1 )

i

Running time of Selection SortRunning time of Selection Sort

What is the value of this sum?

T(n) = 0 + 1 + 2 + 3 + … + (n-3) + (n-2) + (n-1)

An old addition trick : Group terms to get a common sum of values:

T(n) = 0 + 1 + 2 + 3 + … + (n-3) + (n-2) + (n-1)

= (1 + (n-1)) + (2 + (n-2)) + (3 + (n-3)) + …

= n + n + n + …

There are roughly (n-1)/2 pairs, each of which sums to n. Thus, the total value is roughly

n(n-1)/2. (In fact, this is exactly correct.)

Final running time :

Plot of Running Time vs. Array Size

Plot of Running Time vs. Array Size

Big-”Oh” Notation

Big-”Oh” Notation

Observation : Efficiency is most critical for large n.

2 2

n(n 1 ) n n n n

T (n)

2 2 2 2

 

   