• No results found

• Selection sort

N/A
N/A
Protected

Academic year: 2022

Share "• Selection sort"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay

Session: Analyzing Selection Sort

(2)

IIT Bombay

• Selection sort

• Intuition

• C++ implementation

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Analyzing performance of selection sort

• Counting “basic” steps in sorting an array of size n

Overview of This Lecture

(4)

IIT Bombay

Selection Sort Animated

Total

24

18

17

25

27

24

(5)

IIT Bombay

Selection Sort in C++

int main() {

… Declarations, input validation and reading elements of array A … // Selection sort

int currTop, currMaxIndex; // A[currTop+ … A*n-1] is unsorted array for (currTop = 0; currTop < n; currTop ++) {

currMaxIndex = findIndexOfMax(A, currTop, n);

swap(A, currTop, currMaxIndex);

}

… Rest of code …

return 0;

(6)

IIT Bombay

Selection Sort in C++

// PRECONDITION: start < end

// start, end within array bounds of A

int findIndexOfMax(int A[], int start, int end) { int i, currMaxIndex = start;

for ( i = start ; i < end; i++ ) {

if (A[i] >= A[currMaxIndex]) { currMaxIndex = i; } }

return currMaxIndex;

}

// POSTCONDITION: A[currMaxIndex] at least as large as

// all elements in A[start] through A[end-1], no change in A

(7)

IIT Bombay

Selection Sort in C++

// PRECONDITION: index1, index2 within array // bounds of A

void swap(int A[], int index1, int index2) { int temp;

temp = A[index1];

A[index1] = A[index2];

A[index2] = temp;

return;

}

// POSTCONDITION: A[index1], A[index2] swapped

// Array A changed

(8)

IIT Bombay

“Basic” Steps in Selection Sort

• Reading two elements of array A, comparing them and updating currMaxIndex, if necessary

• Swapping two specified elements of array A

Given an array of n integers, how many “basic” steps (as a

function of n) are needed to sort by selection sort?

(9)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

24 18 17 25

24 27

currMaxIndex, start, i currTop

n = 6

findIndexOfMax called with (A, 0, 6)

i

i

currMaxIndex, i

currMaxIndex, i

currMaxIndex

(10)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

24 18 17 25

24

currMaxIndex 27

currTop

n = 6

i 5

“basic” steps

5 (in general, n-1)

swap(A, 0, 4) 1

“basic” step n-1 + 1 “basic” steps done

findIndexOfMax

called with (A, 0, 6)

(11)

IIT Bombay

Recall: Selection Sort in C++

int main() {

… Declarations, input validation and reading elements of array A … // Selection sort

int currTop, currMaxIndex; // A[currTop+ … A*n-1] is unsorted array for (currTop = 0; currTop < n; currTop ++) {

currMaxIndex = findIndexOfMax(A, currTop, n);

swap(A, currTop, currMaxIndex);

}

… Rest of code …

return 0;

(12)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

27 18 17 25

24 24

currMaxIndex, start, i currTop

n = 6

i currMaxIndex, i

n-1 + 1 “basic” steps done

currMaxIndex findIndexOfMax

called with (A, 1, 6)

(13)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

27 18 17 25

24 24

currTop

n = 6

currMaxIndex i i n-1 + 1 “basic” steps done

findIndexOfMax

called with (A, 1, 6)

(14)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

27 18 17 25

24 24

currTop

n = 6

currMaxIndex

i 4

“basic” steps

4 (in general, n-2)

n-1 + 1 “basic” steps done

findIndexOfMax

called with (A, 1, 6)

(15)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

27 18 17 25

24 24

currTop

n = 6

currMaxIndex 4

“basic” steps

4 (in general, n-2)

n-1 + 1 “basic” steps done

swap(A, 1, 3) 1

“basic” step

n-2 + 1 “basic” steps done

(16)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

27 18 17 25

24 24

currTop

n = 6 n-1 + 1 “basic” steps done

n-2 + 1 “basic” steps done

n-3 + 1 “basic” steps done

(17)

IIT Bombay

Counting “Basic” Steps In Selection Sort

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total

27 18 17 25

24 24

n = 6 n-1 + 1 “basic” steps done

n-2 + 1 “basic” steps done n-3 + 1 “basic” steps done

n-(n-1) + 1 “basic” steps done

currTop

(18)

IIT Bombay

Counting “Basic” Steps in Selection Sort

• Count of “basic” steps to sort an array of n elements:

(n-1 + 1) + (n-2 + 1) + (n-(n-1) + 1)

= (1 + 2 + … n-1) + n-1 = (n - 1) x (n+2)/2

Increases quadratically

with n

(19)

IIT Bombay

Quadratic Growth With n

54 209 464 819

1274

1829

2484

3239

4094

5049

0 1000 2000 3000 4000 5000 6000

10 20 30 40 50 60 70 80 90 100

Count of “basic ” s te p s in se le ction so rt

Array size

Program will run too slow for large n

(20)

IIT Bombay

Is Selection Sort Fast Enough?

• Real-world sorting requirements

• Query generating 1 million data items, each with a score

• Selection sort too slow for such applications

• With n = 10 6 , (n-1).(n+2)/2  5 x 10 11

• If each “basic” step takes 20 ns (memory reads and

writes, comparison, etc.), we need 10 4 seconds (approx. 2.78 hours)!!!

• Can we do better?

• Yes, much better !!!

• Approximately (n. log 2 n) “basic” steps to sort an array of size n

10 6 elements can be sorted in no more than a few seconds!

• Topic of next few lectures …

(21)

IIT Bombay

Summary

• Analysis of performance of selection sort

• Count of “basic” steps grows quadratically with size of array

• Need for faster sorting techniques

References

Related documents

 to define simple functions that expect parameters and return values. 1# Write a C Program to sort a given list of Integers using Bubble sort technique. 3# Write a Python

 to define simple functions that expect parameters and return values. 1# Write a C Program to sort a given list of Integers using Bubble sort technique. 3# Write a Python

In Table 7 we test whether residents sort in response to changes in radon risk using census data by regressing changes in Output Area characteristics between the 2001 and 2011 Census

In order to sort out this anomaly between the high potentials and low production, the results of the experimen- tal fishing surveys (bottom trawl) conducted on board FORV Sagar

• A common course of conduct or behavior involving some sort of communications or exchange of views between the parties, each of whom expects that the other party will act in

This sort of budgeting is normally adopted to ensure objectivity. In formula budgeting, the resources are allocated according to some pre- determined standard. The basis of

Further, quadrupole mass filters and sector-field mass spectrometers of the sort commonly employed for ICP–MS are inherently sequen- tial instruments, which limits their

In Cryptography, the Zero Knowledge confirmation/convention is a sort of convention in which one entity (claimant) demonstrates an alternate entity (verifier) that a