• No results found

Department of Computer Science and Engineering IIT Bombay

N/A
N/A
Protected

Academic year: 2022

Share "Department of Computer Science and Engineering IIT Bombay"

Copied!
35
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: Selection Sort

(2)

IIT Bombay

• Basic programming constructs

• Iteration constructs

• Functions

• Arrays and matrices, among other things …

• The sorting problem

• Motivation

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Selection sort

• A simple, intuitive sorting technique

Overview of This Lecture

(4)

IIT Bombay

Quiz1, Quiz2 and Quiz3 Marks in CS101

ROLL NUMBER

Q1 Q2 Q3 Total

14010101 9 8 7 24

14020202 9 6 3 18 14030201 6 5 6 17

14020103 9 8 8 25

14020403 10 8 9 27

14020205 7 8 9 24

Rank all students in decreasing order of “Total” marks

Core problem:

Sort “Total” marks in decreasing order

Simplification:

If two marks are equal, any

ordering between them ok

(5)

IIT Bombay

Quiz1, Quiz2 and Quiz3 Marks in CS101

ROLL NUMBER

Q1 Q2 Q3 Total

14010101 9 8 7 24

14020202 9 6 3 18 14030201 6 5 6 17

14020103 9 8 8 25

14001122 10 8 9 27

14020202 7 8 9 24

3 4 5 2 1 3

Rank all students in decreasing order of “Total” marks

Core problem:

Sort “Total” marks in decreasing order

Simplification:

If two marks are equal, any

ordering between them ok

(6)

IIT Bombay

Quiz1, Quiz2 and Quiz3 Marks in CS101

ROLL NUMBER

Q1 Q2 Q3 Total 14001122 10 8 9 27

14020103 9 8 8 25

14010101 9 8 7 24

14020202 7 8 9 24

14020202 9 6 3 18 14030201 6 5 6 17

1 2 3 3 4 5

Rank all students in decreasing order of “Total” marks

Core problem:

Sort “Total” marks in decreasing order

Simplification:

If two marks are equal, any

ordering between them ok

(7)

IIT Bombay

How Do We Do It?

Total 24 18 17 25 27 24

?

Total

27

25

24

24

18

17

(8)

IIT Bombay

How Do We Do It?

Solve part of the problem

Arrive at a similar but

“simpler” problem to solve:

Sort remaining marks

Total 24 18 17 25 27 24 1

Total 1 27

Get only the first

element in its right place

18

17

25

24

24

(9)

IIT Bombay

How Do We Do It?

Total 24 18 17 25 27 24

Total 27 18 17 25 24 24

Total 27 1 25

1

Solve part of the problem

Arrive at a similar but “simpler”

problem to solve:

Sort remaining marks

Get only the first

(in current subproblem) element in its right place

17

18

24

24

(10)

IIT Bombay

How Do We Do It?

Total 24 18 17 25 27 24

Total 27 18 17 25 24 24

Total 27 25 17 18 24 24

Total 27 25 24

1

1

18

24

17

(11)

IIT Bombay

How Do We Do It?

Total 24 18 17 25 27 24

Total 27 18 17 25 24 24

Total 27 25 17 18 24 24

Total 27 25 24 24 18 17 Total

27 25 24 18 24 17 1

1

(12)

IIT Bombay

How Do We Do It?

Total 24 18 17 25 27 24

Total 27 18 17 25 24 24

Total 27 25 17 18 24 24

Total 27 25 24 24 18 17 Total

27 25 24 18 24 17

1

Total

27

25

24

24

18

17

1

(13)

IIT Bombay

How Do We Do It?

Total 27 18 17 25 24 24

Total 27 25 17 18 24 24

Total 27 25 24 24 18 17 Total

27 25 24 18 24 17

Total 27 25 24 24 18 17 Total

24 18 17 25 27 24

SELECTION SORT

(14)

IIT Bombay

A C++ Program For Selection Sort

• Given an array A of n integers

• Sort them in decreasing order

A[0] ¸ A[1] ¸ A[2] ¸ … A[n-1]

• If two elements are equal, either of them may be ordered before the other

[Once our program is written, final ordering among

equal elements is of course completely determined]

(15)

IIT Bombay

Selection Sort in C++

int main() { int n;

cout << “Give number of integers to sort: “; cin >> n;

// Input validation

if (n > 100) { cout << “Too many elements!” << endl; return -1;}

if (n <= 0) {cout << “Invalid input!” << endl; return -1;}

…. Rest of code … return 0;

}

(16)

IIT Bombay

Selection Sort in C++

int main() {

… Declarations and input validation …

int count, A[100]; // Array of integers to sort // Read integers to sort

cout << “Give “ << n << “integers to sort.” << endl;

for (count = 0; count < n; count++) { cin >> A[count]; }

… Rest of code … return 0;

}

(17)

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 ++) {

// Select maximum element in unsorted part of array A // Let currMaxIndex be its index in array A

// Swap A[currTop] and A[currMaxIndex]

}

… Rest of code …

return 0;

(18)

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;

(19)

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

Array as parameter

(not call-by-value)

(20)

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;

(21)

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

Array as parameter

(not call-by-value)

(22)

IIT Bombay

Role of Comparison Operator

// 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

Note the use of “>=”

(23)

IIT Bombay

Selection Sort Using >=

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

findIndexOfMax(A, 2, 6) start, i, currMaxIndex

currTop

n = 6

(24)

IIT Bombay

Selection Sort Using >=

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

i, currMaxIndex currTop

n = 6

18 >= 17

findIndexOfMax(A, 2, 6)

(25)

IIT Bombay

Selection Sort Using >=

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

i, currMaxIndex currTop

n = 6

24 >= 18

findIndexOfMax(A, 2, 6)

(26)

IIT Bombay

Selection Sort Using >=

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24

i, currMaxIndex 24

currTop

n = 6

24 >= 24

findIndexOfMax(A, 2, 6)

(27)

IIT Bombay

Selection Sort Using >=

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

currTop

n = 6

Total 27 25 24 18 24 1 17

1

(28)

IIT Bombay

Role of Comparison Operator

// 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

What if we used “>” ?

(29)

IIT Bombay

Selection Sort Using >

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

start, i, currMaxIndex currTop

n = 6

findIndexOfMax(A, 2, 6)

(30)

IIT Bombay

Selection Sort Using >

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

i, currMaxIndex currTop

n = 6

18 > 17

findIndexOfMax(A, 2, 6)

(31)

IIT Bombay

Selection Sort Using >

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

i, currMaxIndex currTop

n = 6

24 > 18

findIndexOfMax(A, 2, 6)

(32)

IIT Bombay

Selection Sort Using >

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24

i 24

currTop

n = 6

currMaxIndex 24 > 24

findIndexOfMax(A, 2, 6)

(33)

IIT Bombay

Selection Sort Using >

Total A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

Total 27 25 17 18 24 24

currTop

n = 6

Total 27 25 24 18 24 17 1

1

(34)

IIT Bombay

// 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 small as // all elements in A[start] through A[end-1], no change in A

Role of Comparison Operator

currMinIndex

currMinIndex currMinIndex currMinIndex

currMinIndex

What if we used “<=” ?

Choice of comparison operator crucially determines sorting order (increasing/decreasing), and also how equal elements are

ordered!

(35)

IIT Bombay

Summary

• Selection sort

• Intuition

• C++ implementation

• Choice of comparison operator and its effects

References

Related documents

Rhushabh Goradia and Piyush PorwalComputer Science and Engineering IIT Bombay rhushabh@cse.iitb.ac.in, porwalpiyush@cse.iitb.ac.in... Outline

Computer Science and Engineering Department Indian Institute of Technology

Memory locations accessed: local variables/arrays of functions Statically allocated in stack segment when function is called.. Quick Recap of

• Decide which half of array to recurse on based on output of comparison

• Recall how we accessed member data values of structures V3 p, *ptrP;. cin

• Uses dynamically allocated array to store elements Array can grow or shrink in size. • Dynamic memory management

Department of Computer Science and Engineering Indian Institute of Technology Bombay.

Sivakumar Computer Science and Engineering IIT Bombay siva@iitb.ac.in... But, C can listen to all