1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms...

455
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

Transcript of 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms...

Page 1: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

1

C++ Classes and Data StructuresJeffrey S. Childs

Chapter 14

Introduction to Sorting Algorithms

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Page 2: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

2

Sorting

• Sorting is the process of placing elements in order

• If elements are objects, they are ordered by a particular data member

• There are many algorithms for sorting, each having its own advantages

• No sorting algorithm is better than every other sorting algorithm all the time

• Choosing the right sorting algorithm for a problem requires careful consideration

Page 3: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

3

Heapsort

PriorityQueue< float > pq( arr );for ( i = arr.length( ) – 1; i >= 0; i-- )

pq.dequeue( arr[ i ] );

Page 4: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

4

Heapsort (cont.)

PriorityQueue< float > pq( arr );for ( i = arr.length( ) – 1; i >= 0; i-- )

pq.dequeue( arr[ i ] );

Provide an initial array into the second constructor of the priority queue – this constructor makes a copy of the array and converts the copy into a heap

Page 5: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

5

Heapsort (cont.)

PriorityQueue< float > pq( arr );for ( i = arr.length( ) – 1; i >= 0; i-- )

pq.dequeue( arr[ i ] );

In the loop, we start with the last array index

Page 6: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

6

Heapsort (cont.)

PriorityQueue< float > pq( arr );for ( i = arr.length( ) – 1; i >= 0; i-- )

pq.dequeue( arr[ i ] );

Then, on the first dequeue, the largest element from the heap will be placed into the last position of the array (where it should be)

Page 7: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

7

Heapsort (cont.)

PriorityQueue< float > pq( arr );for ( i = arr.length( ) – 1; i >= 0; i-- )

pq.dequeue( arr[ i ] );

The index i is decremented, so on the next dequeue, the remaining highest value from the heap will be placed in the next to last array position (where it should be)

Page 8: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

8

Heapsort (cont.)

PriorityQueue< float > pq( arr );for ( i = arr.length( ) – 1; i >= 0; i-- )

pq.dequeue( arr[ i ] );

The loop stops when the index becomes less than 0

Page 9: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

9

Heapsort TimeComplexity

• Heapsort runs in ( n lg n ) time on average

• It has a best-case time of ( n ) if all elements have the same value– This is an unusual case, but we may want to

consider it if many of the elements have the same value

Page 10: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

10

Function Templates

• A function template can be made for heapsort, so that the client can put heapsort into the client program

• A function template is something like a class template – the compiler makes functions from the function template

• The client can then use heapsort for different types of arrays, without rewriting the heapsort function

Page 11: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

11

Heapsort Function Template (in a Client Program)

1 template <class DataType>2 void heapsort( Array<DataType> & arr )3 {4 PriorityQueue<DataType> pq( arr );5 for ( int i = arr.length( ) - 1; i >= 0; i-- )6 pq.dequeue( arr[ i ] );7 }

Page 12: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

12

Example Using the Heapsort Function Template

1 #include <iostream>2 #include "PriorityQueue.h"34 using namespace std;56 struct MyStruct {7 int id;8 float amount;9 bool operator >( const MyStruct & right ) 10 { return amount > right.amount; }11 };

Page 13: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

13

Example Using the Heapsort Function Template (cont.)

1 #include <iostream>2 #include "PriorityQueue.h"34 using namespace std;56 struct MyStruct {7 int id;8 float amount;9 bool operator >( const MyStruct & right ) 10 { return amount > right.amount; }11 };

Client will sort an array of MyStruct objects

Page 14: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

14

Example Using the Heapsort Function Template (cont.)

1 #include <iostream>2 #include "PriorityQueue.h"34 using namespace std;56 struct MyStruct {7 int id;8 float amount;9 bool operator >( const MyStruct & right ) 10 { return amount > right.amount; }11 };

Apparently, the client would like to sort the MyStruct objects by amount, not by id

Page 15: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

15

12 template <class DataType>13 void heapsort( Array<DataType> & arr );1415 int main( )16 {17 int num;1819 cout << "How many records do you want to enter? ";20 cin >> num;21 Array<MyStruct> objs( num );

Note that the function prototype for heapsort must also be templated

Example Using the Heapsort Function Template (cont.)

Page 16: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

16

22 for ( int i = 0; i < objs.length( ); i++ ) {23 cout << "Enter id for record " << i + 1 << ": ";24 cin >> objs[ i ].id;25 cout << "Enter amount for record " << i + 1 << ": ";26 cin >> objs[ i ].amount;27 }

Client asks user to enter information for the array of MyStruct objects

Example Using the Heapsort Function Template (cont.)

Page 17: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

17

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Then, the client calls heapsort to sort the array of MyStruct objects called objs

Example Using the Heapsort Function Template (cont.)

Page 18: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

18

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Example Using the Heapsort Function Template (cont.)

When the compiler sees this line of code, it makes a function out of the function template

Page 19: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

19

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Example Using the Heapsort Function Template (cont.)

The compiler determines what type objs is…

Page 20: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

20

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Example Using the Heapsort Function Template (cont.)

then substitutes that type for DataType to make a heapsort function

Page 21: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

21

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Example Using the Heapsort Function Template (cont.)

The client could also call heapsort on an array of integers in the same program…

Page 22: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

22

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Example Using the Heapsort Function Template (cont.)

…the compiler would make another heapsort function using int for DataType

Page 23: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

23

28 heapsort( objs );...39 template <class DataType>40 void heapsort( Array<DataType> & arr )41 {42 PriorityQueue<DataType> pq( arr );43 for ( int i = arr.length( ) - 1; i >= 0; i-- )44 pq.dequeue( arr[ i ] );45 }

Example Using the Heapsort Function Template (cont.)

Note that the heapsort function template is placed into the client’s program just like any other function would be

Page 24: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

24

28 heapsort( objs );29 cout << "Records in sorted order:" << endl;30 for ( int i = 0; i < objs.length( ); i++ ) {31 cout << "record " << i << ":" << endl;32 cout << " id: " << objs[ i ].id << endl;33 cout << " amount: " << objs[ i ].amount << endl;34 }3536 return 0;37 }

Example Using the Heapsort Function Template (cont.)

The final part of the main function provides the records in sorted order

Page 25: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

25

Insertion Sort

3 18 19 22 17 16 6 1 15 2

sorted section unsorted section

During the process of insertion sort, the array is divided into a sorted section and an unsorted section – the sorted section grows and the unsorted section shrinks.

Page 26: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

26

Insertion Sort (cont.)

3 18 19 22 17 16 6 1 15 2

sorted section unsorted section

next value to be inserted into sorted section

Page 27: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

27

Insertion Sort (cont.)

3 18 19 22 17 16 6 1 15 2

sorted section unsorted section

Page 28: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

28

Insertion Sort (cont.)

3 18 19 22

17

16 6 1 15 2

sorted section unsorted section

Page 29: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

29

Insertion Sort (cont.)

3

17

16 6 1 15 2

sorted section unsorted section

22 19 18

Page 30: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

30

Insertion Sort (cont.)

3 17 16 6 1 15 2

sorted section unsorted section

22 19 18

Page 31: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

31

Insertion Sort (cont.)

sorted section unsorted section

3 17 16 6 1 15 2 22 19 18

Page 32: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

32

Insertion Sort (cont.)

sorted section unsorted section

On each iteration of insertion sort, the sorted section grows by 1, and the unsorted section shrinks by 1, until the array is sorted.

3 17 16 6 1 15 2 22 19 18

Page 33: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

33

Insertion Sort (cont.)

sorted section unsorted section

16 would be the next value to be “inserted”, giving insertion sort its name

3 17 16 6 1 15 2 22 19 18

Page 34: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

34

Insertion Sort (cont.)

sorted section unsorted section

In the algorithm, j is used to mark the index of the next element to be inserted.

3 17 16 6 1 15 2 22 19 18

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Page 35: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

35

Insertion Sort (cont.)

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Page 36: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

36

Insertion Sort (cont.)

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

1 for each j, from 1 to the length of A – 1

2 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Page 37: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

37

Insertion Sort (cont.)

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

1 for each j, from 1 to the length of A – 1

2 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Page 38: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

38

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 1

3 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 39: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

39

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 40: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

40

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 41: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

41

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 42: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

42

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 43: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

43

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 44: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

44

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 45: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

45

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 1

3 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 46: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

46

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 47: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

47

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19

18

i

Page 48: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

48

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19

18

i

Page 49: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

49

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 50: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

50

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 51: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

51

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 52: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

52

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 1

3 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 53: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

53

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 54: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

54

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 55: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

55

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 56: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

56

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 57: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

57

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 58: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

58

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 59: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

59

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 1

3 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 60: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

60

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 61: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

61

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17

16 6 1 15 2 22 19 18

i

Page 62: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

62

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17

16 6 1 15 2 22 19 18

i

Page 63: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

63

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]

4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 64: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

64

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 65: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

65

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )

5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 66: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

66

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 1

3 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 67: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

67

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 68: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

68

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 69: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

69

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 1

2 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 70: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

70

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 1

2 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 71: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

71

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 1

3 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

Page 72: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

72

Insertion Sort (cont.)

sorted section unsorted section

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

j

3 17 16 6 1 15 2 22 19 18

i

etc.

Page 73: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

73

Starting it Off

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Why is it that we start j off at 1 instead of 0?

Page 74: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

74

Starting it Off (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

A section of 1 element (at index 0) is already sorted! The index j starts off as the first element in the unsorted section.

Page 75: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

75

Inversions

• An inversion between any two elements when the element that comes first in the array is greater than the element that comes next

• The array below has three inversions…

4 2 16 5 15

0 1 2 3 4

Page 76: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

76

Inversions (cont.)

• An inversion between any two elements when the element that comes first in the array is greater than the element that comes next

• The array below has three inversion:

4 2 16 5 15

0 1 2 3 4

one inversion

Page 77: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

77

Inversions (cont.)

• An inversion between any two elements when the element that comes first in the array is greater than the element that comes next

• The array below has three inversion:

4 2 16 5 15

0 1 2 3 4

a second inversion

Page 78: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

78

Inversions (cont.)

• An inversion between any two elements when the element that comes first in the array is greater than the element that comes next

• The array below has three inversion:

4 2 16 5 15

0 1 2 3 4

a third inversion

Page 79: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

79

Inversions and Swaps

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

A swap in insertion sort removes an inversion…

Page 80: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

80

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

because when performed, A[ i ] was greater than A[ i + 1 ]

Page 81: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

81

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16 19

But it can’t remove more than one inversion – those elements inverted with 16 in the shaded part of the array…

Page 82: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

82

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16 19

will still be inverted with 16 after the swap…

Page 83: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

83

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16

19

will still be inverted with 16 after the swap…

Page 84: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

84

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16

19

will still be inverted with 16 after the swap…

Page 85: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

85

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16 19

will still be inverted with 16 after the swap…

Page 86: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

86

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16 19

The same is true with 19

Page 87: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

87

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16 19

So a swap removes one and only one inversion each time it is executed; it is also the only way inversions are removed

Page 88: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

88

Inversions and Swaps(cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

0 1 2 3 4 5 6 7 8 9

16 19

Therefore, the number of swaps performed in all of insertion sort is equal to the number of inversions in the original array.

Page 89: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

89

Worst Number ofInversions

• The worst number of inversions occurs when an array is in descending order

• Each element is inverted with every other element (can’t get any worse)

23 19 16 12 10

0 1 2 3 4

Page 90: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

90

Worst Number ofInversions (cont.)

• How many inversions are there?

• There are n elements, and each is inverted with (n – 1) elements, so n( n – 1 )?

23 19 16 12 10

0 1 2 3 4

Page 91: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

91

Worst Number ofInversions (cont.)

• Not n( n – 1 ), because we are counting each inversion twice– 19 inverted with 16 and 16 inverted with 19

• The number of inversions is n( n – 1 ) / 2

23 19 16 12 10

0 1 2 3 4

Page 92: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

92

Worst Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Therefore, in the worst case, the number of swaps performed is

n( n – 1 ) / 2 …

Page 93: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

93

Worst Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

Therefore, in the worst case, the TOTAL number of swaps performed is

n( n – 1 ) / 2 = ½ n2 – ½ n

which gives us the worst-case time complexity of ( n2 ) for insertion sort.

Page 94: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

94

Average Number ofInversions

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

In the average case, we might assume that each possible inversion has a 50% chance of occurring. Therefore, on average, we would expect that the number of inversions is 50% of n( n – 1 ) / 2

Page 95: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

95

Average Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

50% of n( n – 1 ) / 2 =

½ * n ( n – 1 ) / 2 =

n( n – 1 ) / 4 (still ( n2 ) on average)

Page 96: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

96

Best Number ofInversions

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

There are no inversions in an array that is already sorted, so the condition in the while loop is never true (no swaps)

Page 97: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

97

Best Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

The outer loop still executes n – 1 times (the algorithm does not know it is already sorted)

Page 98: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

98

Best Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

Thus, in the best case, insertion sort runs in ( n ) time

Page 99: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

99

Best Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

Does an array have to be already sorted in order for insertion sort to run in ( n ) time?

Page 100: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

100

Best Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

Consider the case where we have n inversions, so we have a total of n swaps

Page 101: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

101

Best Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

For each iteration of the for loop, a swap is executed an average of 1 time (approximately)

Page 102: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

102

Best Number ofInversions (cont.)

1 for each j, from 1 to the length of A – 12 i = j – 13 while i is greater than -1 and A[ i ] is greater than A[ i + 1 ]4 swap( A[ i ], A[ i + 1 ] )5 i--

21 25 30 32 40

0 1 2 3 4

Hence, we would still have ( n ) time if we have n inversions or less

Page 103: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

103

Another Advantageof Insertion Sort

• Insertion sort is often used to sort a small number of elements

• Consider, in the average case, that we have n( n – 1 ) / 4 inversions

• However, n = n( n – 1 ) / 4 has a solution of n = 5

• Therefore, at around 5 elements, insertion sort behaves like a ( n ) sorting algorithm

Page 104: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

104

Speeding upInsertion Sort

• We can use the same idea that we used in the heap to perform “one-assignment” swaps instead of full swaps

• We first save the element at index j to a temporary variable to hold it

• An iteration of the for loop would then behave like this…

Page 105: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

105

One-AssignmentSwap

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

With sorting in progress, let’s suppose j is set to 6 here

Page 106: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

106

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

Page 107: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

107

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 1

2 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

Page 108: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

108

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 1

2 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

temp: 16

Page 109: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

109

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]

3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

temp: 16

Page 110: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

110

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]

3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

temp: 16

i

Page 111: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

111

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 1

4 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

temp: 16

i

Page 112: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

112

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 16 1 15 2 22 19 18

j

temp: 16

i

Page 113: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

113

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

Page 114: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

114

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

Page 115: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

115

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i—7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

22

Page 116: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

116

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

22

Page 117: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

117

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

22

Page 118: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

118

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 1

4 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

22

Page 119: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

119

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 22 19 18

j

temp: 16

i

22

Page 120: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

120

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18

j

temp: 16

i

22

Page 121: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

121

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18

j

temp: 16

i

22

Page 122: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

122

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 19 18

j

temp: 16

i

22

Page 123: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

123

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 19 18

j

temp: 16

i

22

Page 124: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

124

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 19 18

j

temp: 16

i

22

Page 125: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

125

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 1

4 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 19 18

j

temp: 16

i

22

Page 126: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

126

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 19 18

j

temp: 16

i

22

Page 127: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

127

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18

j

temp: 16

i

22

Page 128: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

128

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18

j

temp: 16

i

22

Page 129: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

129

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 18

j

temp: 16

i

22

Page 130: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

130

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 18

j

temp: 16

i

22

Page 131: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

131

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 18

j

temp: 16

i

22

Page 132: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

132

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 1

4 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 18

j

temp: 16

i

22

Page 133: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

133

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 18

j

temp: 16

i

22

Page 134: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

134

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18

j

temp: 16

i

22

Page 135: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

135

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18

j

temp: 16

i

22

Page 136: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

136

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp

5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 137: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

137

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 138: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

138

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]

6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 139: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

139

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 1

4 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 140: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

140

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--

7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 141: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

141

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--

7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 17 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 142: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

142

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--

7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 143: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

143

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--

7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 16 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 144: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

144

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--

7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 16 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 145: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

145

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 16 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 146: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

146

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 16 15 1 15 2 19 18 17

j

temp: 16

i

22

Page 147: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

147

One-AssignmentSwap (cont.)

1 for each j, from 1 to the length of A – 12 temp = A[ j ]3 i = j – 14 while i is greater than -1 and A[ i ] is greater than temp5 A[ i + 1 ] = A[ i ]6 i--7 A[ i + 1] = temp

sorted section unsorted section

0 1 2 3 4 5 6 7 8 9

3 16 15 1 15 2 19 18 17

j

temp: 16

i

22

etc.

Page 148: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

148

Quicksort• Time complexities of Quicksort

– best: ( n lg n )– average: ( n lg n )– worst: ( n2 )

• The average case usually dominates over the worst case in sorting algorithms, and in Quicksort, the coefficient of the n lg n term is small– makes Quicksort one of the most popular general-

purpose sorting algorithms

Page 149: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

149

Functions of Quicksort

• A recursive function, called quicksort

• A nonrecursive function, usually called partition

• quicksort calls partition

Page 150: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

150

Partition

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

In the partition function, the last element is chosen as a pivot – a special element used for comparison purposes

pivot

Page 151: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

151

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

Each element is compared to the pivot. When partition is done, it produces the result shown next…

pivot

Page 152: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

152

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 28 38 47 11 3 6 4 35 46

All elements less than or equal to the pivot are on its left

Page 153: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

153

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 28 38 47 11 3 6 4 35 46

All elements greater than the pivot are on its right side

Page 154: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

154

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 28 38 47 11 3 6 4 35 46

The pivot is where it will be in the final sorted array

Page 155: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

155

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 28 38 47 11 3 6 4 35 46

Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array.

Page 156: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

156

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 28 38 47 11 3 6 4 35 46

Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array.

Partition (cont.)

Page 157: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

157

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 28 38 47 11 3 6 4 35 46

Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array.

Partition (cont.)

Page 158: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

158

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 3 38 11 47 46 35 6 4 28

pivot

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 28 38 47 11 3 6 4 35 46

Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array.

Partition (cont.)

Page 159: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

159

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 28 38 47 11 3 6 4 35 46

Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array.

Partition (cont.)

Page 160: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

160

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 28 38 47 11 3 6 4 35 46

Partition (cont.)

The next time partition is called, for example, it will only work with this section to the left of the previous pivot.

Page 161: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

161

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 28 38 47 11 3 6 4 35 46

Partition (cont.)

The next time partition is called, for example, it will only work with this section to the left of the previous pivot.

Page 162: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

162

0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 28 38 47 11 3 6 4 35 46

Partition (cont.)

pivot

Page 163: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

163

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

pivot

Page 164: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

164

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

Each section of the array separated by a previous pivot will eventually have partition called for it

Page 165: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

165

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

Except that partition is not called for a section of just one element – it is already a pivot and it is where it belongs

Page 166: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

166

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

Partition is not called for a section of just one element – it is already a pivot and it is where it belongs

Page 167: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

167

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

Partition is called for this section

Page 168: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

168

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

pivot

Page 169: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

169

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

pivotAll elements are smaller and stay to the left of the pivot

Page 170: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

170

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

Partition is called for this section

Page 171: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

171

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 11 14 6 28 35 46

Partition (cont.)

pivot

Page 172: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

172

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 14 11 6 28 35 46

Partition (cont.)

pivot

Page 173: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

173

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 14 11 6 28 35 46

Partition (cont.)

partition

Page 174: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

174

0 1 2 3 4 5 6 7 8 9 10 11 3 10 4 28 38 47 14 11 6 28 35 46

Partition (cont.)

pivot

Page 175: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

175

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

pivot

Page 176: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

176

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

partition not called for this

Page 177: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

177

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

partition not called for this

Page 178: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

178

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

partition not called for this

Page 179: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

179

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

partition not called for this

Page 180: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

180

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

partition called for this

Page 181: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

181

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 47 14 11 10 28 35 46

Partition (cont.)

pivot

Page 182: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

182

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 35 14 11 10 28 46 47

Partition (cont.)

pivot

Page 183: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

183

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 35 14 11 10 28 46 47

Partition (cont.)

partition called for this

Page 184: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

184

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 38 35 14 11 10 28 46 47

Partition (cont.)

pivot

Page 185: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

185

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

pivot

Page 186: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

186

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

Partition not called for this

Page 187: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

187

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

Partition not called for this

Page 188: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

188

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

Partition not called for this

Page 189: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

189

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

Partition not called for this

Page 190: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

190

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

At this point, the array is sorted

Page 191: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

191

0 1 2 3 4 5 6 7 8 9 10 11 3 6 4 28 35 38 14 11 10 28 46 47

Partition (cont.)

But to achieve this, what steps does the algorithm for partition go through?

Page 192: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

192

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 2 3 38 11 47 46 35 6 4 28

pivot

Partition has a loop which iterates through each element, comparing it to the pivot

Page 193: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

193

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

When partition is in progress, there is a partitioned section on the left, and an unpartitioned section on the right

partitioned section unpartitioned section

Page 194: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

194

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

The dividing line in the partitioned section means that all elements to the left are less than or equal to the pivot; all elements to the right are greater than the pivot

partitioned section unpartitioned section

Page 195: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

195

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

On each iteration, the partitioned section grows by one and the unpartitioned section shrinks by one, until the array is fully partitioned

partitioned section unpartitioned section

Page 196: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

196

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

i is an index used to mark the last value in the small side of the partition, while j is used to mark the first value in the unpartitioned section.

partitioned section unpartitioned section

Page 197: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

197

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

i is an index used to mark the last value in the small side of the partition, while j is used to mark the first value in the unpartitioned section.

partitioned section unpartitioned section

i j

Page 198: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

198

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

On each iteration of partition, the value at j is compared to the pivot.

partitioned section unpartitioned section

i j

Page 199: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

199

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

On each iteration of partition, the value at j is compared to the pivot.

partitioned section unpartitioned section

i j

Page 200: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

200

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

If the value at j is greater, j is just incremented (the partitioned section grows by one)

partitioned section unpartitioned section

i j

Page 201: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

201

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

If the value at j is greater, j is just incremented (the partitioned section grows by one)

partitioned section unpartitioned section

i j

Page 202: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

202

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

If, on an iteration, the value at j is less than the pivot…

partitioned section unpartitioned section

i j

Page 203: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

203

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

then i is incremented…

partitioned section unpartitioned section

i j

Page 204: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

204

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

then i is incremented…

partitioned section unpartitioned section

i j

Page 205: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

205

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

and the value at i is swapped with the value at j…

partitioned section unpartitioned section

i j

Page 206: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

206

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35 6

4 28

pivot

and the value at i is swapped with the value at j…

partitioned section unpartitioned section

i j

Page 207: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

207

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35 6

4 28

pivot

and the value at i is swapped with the value at j…

partitioned section unpartitioned section

i j

Page 208: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

208

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35 6

4 28

pivot

and the value at i is swapped with the value at j…

partitioned section unpartitioned section

i j

Page 209: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

209

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35 6

4 28

pivot

and the value at i is swapped with the value at j…

partitioned section unpartitioned section

i j

Page 210: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

210

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

and the value at i is swapped with the value at j…

partitioned section unpartitioned section

i j

Page 211: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

211

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

then j is incremented

partitioned section unpartitioned section

i j

Page 212: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

212

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

pivot

then j is incremented

partitioned section unpartitioned section

i j

Page 213: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

213

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

Partition starts off by passing in the array arr…

Page 214: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

214

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

the beginning index of the array section it is working with…

Page 215: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

215

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

and the ending index of the array section it is working with

Page 216: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

216

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

i is used to mark the end of the small-side part of the partition

Page 217: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

217

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

initially, there isn’t one, so i is set to p – 1

(-1 if p is 0)

Page 218: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

218

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

this loop iterates through the elements…

Page 219: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

219

Partition (cont.)1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

comparing them to the pivot

Page 220: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

220

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

j is currently 7, with partition having already iterated through elements 0 through 6

p r

Page 221: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

221

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 1

4 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 222: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

222

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]

5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 223: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

223

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]

5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 224: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

224

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 225: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

225

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28

3

38 11 47

46

35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 226: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

226

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28

3

38 11 47

46

35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 227: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

227

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28

3

38 11 47

46

35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 228: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

228

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28

3

38 11 47

46

35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 229: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

229

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 230: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

230

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 231: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

231

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 232: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

232

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 233: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

233

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 1

4 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 234: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

234

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 235: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

235

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 236: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

236

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 1

4 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 237: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

237

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]

5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 238: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

238

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]

5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 239: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

239

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 240: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

240

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38

11 47

46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 241: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

241

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38

11

47

46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 242: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

242

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38

11

47

46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 243: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

243

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38

11

47

46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 244: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

244

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38

11 47

46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 245: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

245

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 246: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

246

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 247: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

247

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 248: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

248

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 249: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

249

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 1

4 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 250: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

250

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]

5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 251: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

251

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]

5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 252: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

252

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 35 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 253: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

253

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35

6

4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 254: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

254

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35

6

4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 255: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

255

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35

6

4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 256: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

256

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35

6

4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 257: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

257

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35

6

4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 258: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

258

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46

35

6

4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

Page 259: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

259

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 260: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

260

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++

6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 261: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

261

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 262: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

262

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 6 4 28

i j

1 partition( arr, p, r )2 i = p – 1

3 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

j isn’t incremented past r - 1

Page 263: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

263

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 46 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 264: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

264

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47

46

6 4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 265: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

265

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47

46

6 4

28 i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 266: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

266

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47

46

6 4

28 i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 267: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

267

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47

46

6 4

28 i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 268: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

268

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47

46

6 4

28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35

Page 269: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

269

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35 46

Page 270: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

270

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )

7 swap ( arr[ i + 1 ] and arr[ r ] )8 return i + 1

p r

35 46

Page 271: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

271

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )

8 return i + 1

p r

35 46

Page 272: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

272

Partition (cont.)

0 1 2 3 4 5 6 7 8 9 10 11

14 10 28 3 38 11 47 6 4 28

i j

1 partition( arr, p, r )2 i = p – 13 for all j from p to r – 14 if arr[ j ] <= arr[ r ]5 i++6 swap( arr[ i ], arr[ j ] )7 swap ( arr[ i + 1 ] and arr[ r ] )

8 return i + 1

p r

35 46

The final step of partition returns the INDEX of the pivot back to the quicksort function that called it

Page 273: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

273

The QuicksortFunction

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

Since quicksort is called recursively, it also passes in the array arr, the beginning index of the section it is working with, and the ending section it is working with

Page 274: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

274

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

If p < r, those same indexes are used in the call to partition (in such a case, a call to quicksort always produces a matching call to partition)

Page 275: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

275

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

The index of the pivot is returned from partition and assigned to q

Page 276: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

276

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

The index of the pivot is returned from partition and assigned to q

p r

q

Page 277: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

277

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

quicksort is called recursively here, working with the section on the left of the pivotp r

q

Page 278: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

278

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

quicksort is called recursively here, working with the section on the left of the pivotp r

q

q-1

Page 279: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

279

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

and quicksort is called recursively here, working with the section on the right of the pivotp r

q

q-1

Page 280: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

280

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

and quicksort is called recursively here, working with the section on the right of the pivotp r

q

q-1 q+1

Page 281: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

281

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

if the array sections are at least 2 elements in size, these quicksort functions will call partition for the same array section it is working with

Page 282: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

282

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r ) and partition will break

the sections into even smaller sections

Page 283: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

283

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

The recursive case occurs if p < r

(this means the array section has at least two elements, the one at p and the one at r)

Page 284: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

284

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

If the recursive case occurs, a call to quicksort will match a call to partition

Page 285: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

285

The QuicksortFunction (cont.)

1 quicksort( arr, p, r )2 if p < r3 q = partition( arr, p, r )4 quicksort( arr, p, q – 1 )5 quicksort( arr, q + 1, r )

If p == r, the base case occurs – there is only one element in the array section (recall that partition is not called for a section that just has one element)

Page 286: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

286

Counting Sort

• The time complexity of counting sort depends on both n and also the range of the elements– for example, if the elements range in value

from 25 to 30, the range is 30 – 25 + 1 = 6

• If the range is smaller than n, counting sort is very fast, running in ( n ) time

Page 287: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

287

Counting Sort (cont.)

• The main counting sort algorithm works with elements that range in value from 0 to some positive integer k

• However, with a little modification, counting sort can work with nearly any data, still maintaining a ( n ) time complexity

• Let’s look at the main counting sort algorithm…

Page 288: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

288

Counting SortStep 1

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

This is the array A to be sorted. It’s values range from 0 to 7, giving a range of 8.

8 < n (since n = 10), so counting sort should be fast on this array.

A

Page 289: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

289

Counting SortStep 1 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 0

Page 290: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

290

Counting SortStep 1 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 0

0 1 2 3 4 5 6 7

C

Page 291: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

291

Counting SortStep 1 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 0

0 1 2 3 4 5 6 7

C

Page 292: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

292

Counting SortStep 1 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 0

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

C

Page 293: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

293

Counting SortStep 2

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

C

Page 294: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

294

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

C

j

Page 295: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

295

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

C

j

Page 296: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

296

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

C

j

A[ j ] is 4, so this is really C[4]++

Page 297: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

297

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 0 0 0

0 1 2 3 4 5 6 7

C

j

Page 298: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

298

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 0 0 0

0 1 2 3 4 5 6 7

C

j

Page 299: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

299

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 0 0 0

0 1 2 3 4 5 6 7

C

j

Page 300: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

300

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 0 0 0

0 1 2 3 4 5 6 7

C

j

Page 301: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

301

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 1 0 0

0 1 2 3 4 5 6 7

C

j

Page 302: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

302

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 1 0 0

0 1 2 3 4 5 6 7

C

j

Page 303: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

303

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 1 0 0

0 1 2 3 4 5 6 7

C

j

Page 304: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

304

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 1 0 0

0 1 2 3 4 5 6 7

C

j

Page 305: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

305

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

5 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++

0 0 0 0 1 2 0 0

0 1 2 3 4 5 6 7

C

j

Page 306: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

306

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

0 0 0 0 1 2 0 0

0 1 2 3 4 5 6 7

C

j

Notice that the C array is being used to “count” the number of values in the A array…

Page 307: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

307

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

0 0 0 0 1 2 0 0

0 1 2 3 4 5 6 7

C

j

Since C[ 5 ] is 2, it means we’ve found 2 5’s in the A array so far

Page 308: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

308

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

0 0 0 0 1 2 0 0

0 1 2 3 4 5 6 7

C

j

Thus, at the end of this loop, the C array will look like this…

Page 309: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

309

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

Thus, at the end of this loop, the C array will look like this

Page 310: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

310

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

The total of the values in C is equal to n (since it a total count of the number of elements).

Page 311: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

311

Counting SortStep 2 (cont.)

4 5 5 2 6 5 5 7 0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

Notice that we don’t need the values in the A array any more – all information is contained in C.

Page 312: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

312

Counting SortStep 2 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

Notice that we don’t need the values in the A array any more – all information is contained in C.

Page 313: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

313

Counting SortStep 2 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

Now, it is just a matter of writing the appropriate indexes of C into the A array (one 0 goes in the first spot, two 2’s go in the next two places, etc.

Page 314: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

314

Counting SortStep 3

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

7 j = 0

Page 315: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

315

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

7 j = 0

j

Page 316: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

316

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

Page 317: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

317

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 318: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

318

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 319: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

319

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

m counts the number of times i has to be written into A[ j ]

Page 320: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

320

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one time

Page 321: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

321

Counting SortStep 3 (cont.)

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one time

Page 322: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

322

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one time

Page 323: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

323

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one time

Page 324: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

324

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one time

Page 325: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

325

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 326: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

326

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 327: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

327

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 328: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

328

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

0 times

Page 329: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

329

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 330: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

330

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 331: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

331

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 332: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

332

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

two times

Page 333: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

333

Counting SortStep 3 (cont.)

0

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

two times

Page 334: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

334

Counting SortStep 3 (cont.)

0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

two times

Page 335: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

335

Counting SortStep 3 (cont.)

0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

two times

Page 336: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

336

Counting SortStep 3 (cont.)

0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

two times

Page 337: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

337

Counting SortStep 3 (cont.)

0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

Page 338: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

338

Counting SortStep 3 (cont.)

0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one more time

Page 339: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

339

Counting SortStep 3 (cont.)

0 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one more time

Page 340: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

340

Counting SortStep 3 (cont.)

0 2 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

j

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one more time

Page 341: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

341

Counting SortStep 3 (cont.)

0 2 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one more time

j

Page 342: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

342

Counting SortStep 3 (cont.)

0 2 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

one more time

j

Page 343: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

343

Counting SortStep 3 (cont.)

0 2 2

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

etc.

j

Page 344: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

344

Counting SortStep 3 (cont.)

0 2 2 4 5 5 5 5 6 7

0 1 2 3 4 5 6 7 8 9

A

1 0 2 0 1 4 1 1

0 1 2 3 4 5 6 7

C

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

i

etc.

j

Page 345: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

345

Analysis of CountingSort

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

Page 346: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

346

Analysis of CountingSort (cont.)

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

( 1 )

Page 347: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

347

Analysis of CountingSort (cont.)

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

( k )

( 1 )

Page 348: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

348

Analysis of CountingSort (cont.)

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

( k )

( 1 )

( n )

Page 349: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

349

Analysis of CountingSort (cont.)

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

( k )

( 1 )

( n )

( 1 )

Page 350: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

350

Analysis of CountingSort (cont.)

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

( k )

( 1 )

( n )

( 1 )So far, we have ( 1 ) + ( k ) + ( n ) + ( 1 ), which is ( n ) + ( k )

Page 351: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

351

Analysis of CountingSort (cont.)

1 COUNTING-SORT( A )2 make an Array C of length k + 13 for each i, from 0 to k 4 C[ i ] = 05 for each j, from 0 to the length of A - 16 C[ A[ j ] ]++7 j = 08 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

( k )

( 1 )

( n )

( 1 )

What about this nested loop?

Page 352: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

352

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++

Page 353: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

353

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++ This line is executed

n times – if any less, array A wouldn’t get filled in – if any more, we would be writing values past the end of the array!

Page 354: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

354

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++ Thus, lines 8-11

would appear to have a ( n ) time complexity…but there is more to it

Page 355: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

355

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++ What about all of the

times that C[ i ] is 0?

In these cases, the body of the inner loop doesn’t execute, but this line still executes a number of times

Page 356: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

356

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++ C[ i ] is checked to

see if it is empty k + 1 times

Page 357: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

357

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++ Hence, these lines

execute in

( k ) + ( n ) time

and this is the time complexity of counting sort

Page 358: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

358

Analysis of CountingSort (cont.)

8 for each i from 0 to k9 for each m from 1 to C[ i ]10 A[ j ] = i11 j++ If k < n, then

counting sort is considered to run in ( n ) time

If k is much greater than n, we may not want to use it

Page 359: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

359

Modifications toCounting Sort

• What if the least value in A is some minimum value min, where min is not equal to 0?

• We could adjust the A array by first subtracting min from each value (this would make min 0)

• Then, run counting sort• Then, add min to each value to restore all the

values• The adjustments before and after counting sort

take only ( n ) time

Page 360: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

360

Modifications toCounting Sort (cont.)

• What if we do not know the maximum or minimum values?

• These can also be found in ( n ) time:min = A[0]max = A[0] for each j, from 1 to the length of A - 1

if A[ j ] > maxthen max = A[ j ]

else if A[ j ] < minthen min = A[ j ]

Page 361: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

361

Modifications toCounting Sort (cont.)

• We may even want to run a test to see which sorting algorithm we should use:

if max – min < n

CountingSort( A )

else

Quicksort( A, 0, n – 1)

Page 362: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

362

Modifications toCounting Sort (cont.)

• What if we have floating-point numbers? Is counting sort out of the question?

• Not really. Consider monetary values. We could multiply each value by 100 to make an array of integers

• After running counting sort, we multiply each value by 0.01 to adjust it back

Page 363: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

363

When Counting SortIsn’t Practical

• The main thing that can kill the practicality of counting sort is if the range (measured in terms of the unit of precision) is much greater than n– unfortunately, very often the case

Page 364: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

364

Sorting a Linked List

• Linked lists can be sorted too

• Consider going through a linked list, element by element, assigning each value to an array ( ( n ) time )

• Then, sort the array

• Then, go through the linked list, assigning each value of the array to the linked list

Page 365: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

365

Sorting a Linked List(cont.)

• A sorting algorithm must take at least ( n ) time if nothing about the elements is known ahead of time– it takes this long just to look at the elements

• Therefore, assigning elements from linked list to array and back to linked list again will not affect the time complexity of a sorting algorithm

Page 366: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

366

Copying Elements

• Recall that elements in linked lists will tend to be large– a reason for using linked lists

• Therefore, it would be very time-consuming to do this element copying

• We can sort a linked list without copying elements

Page 367: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

367

Array of Pointers

• We use a dynamic array of pointers into the linked list

• Declared like this:

Node<DataType> ** arr = new Node<DataType> * [numElements];

Why?

Page 368: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

368

Array of Pointers(cont.)

• We use a dynamic array of pointers into the linked list

• Declared like this:

Node<DataType> ** arr = new Node<DataType> * [numElements];

arr will point to the first element and the first element is a pointer

Page 369: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

369

Array of Pointers(cont.)

• We use a dynamic array of pointers into the linked list

• Declared like this:

Node<DataType> ** arr = new Node<DataType> * [numElements];

Therefore, arr is a pointer to a pointer

Page 370: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

370

numElements

• We need numElements to make the declaration

• Set to 0 inside the constructor

• Increment when adding an element

• Decrement when removing

• Set to 0 when makeEmpty is called

Page 371: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

371

Step 1

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

6 3 8 7 start

Note: Only the data members to be sorted are shown in the linked list – the objects may be quite large

Page 372: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

372

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start 6 3 8 7

Page 373: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

373

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

6 3 8 7

Page 374: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

374

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

6 3 8 7

Page 375: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

375

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 376: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

376

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 377: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

377

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 378: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

378

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 379: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

379

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 380: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

380

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 381: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

381

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 382: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

382

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 383: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

383

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 384: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

384

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 385: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

385

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 386: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

386

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 387: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

387

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 388: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

388

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 389: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

389

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 390: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

390

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

6 3 8 7

Page 391: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

391

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

i

ptr

etc.

6 3 8 7

Page 392: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

392

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

ptr : NULL

etc.

6 3 8 7

Page 393: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

393

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

ptr : NULL

This loop is a ( n ) loop and did not copy any elements

6 3 8 7

Page 394: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

394

Step 1 (cont.)

i = 0ptr = startwhile ptr is not equal to NULL

arr[ i ] = ptrptr = ptr->nexti++

0 1 2 3

start

ptr : NULL

Step 2 uses a modified sorting algorithm – instead of moving elements, we move array pointers

We’ll use insertion sort…

6 3 8 7

Page 395: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

395

Step 2

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

Page 396: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

396

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

Page 397: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

397

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

jIn the original insertion sort, temp held an element; now it holds an address

Page 398: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

398

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

jIn the original insertion sort, temp held an element; now it holds an address

tempPtr

Page 399: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

399

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]

i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

Page 400: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

400

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]

i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 401: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

401

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1

while i > -1 and arr[ i ]->info > tempPtr->infoarr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i This overloaded operator should pass its parameter by const reference…

Page 402: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

402

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1

while i > -1 and arr[ i ]->info > tempPtr->infoarr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i to completely avoid copying these large elements

Page 403: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

403

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 404: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

404

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 405: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

405

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]

i—arr[ i + 1] = tempPtr

j

tempPtr

i

Page 406: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

406

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]

i—arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 407: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

407

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1

while i > -1 and arr[ i ]->info > tempPtr->infoarr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 408: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

408

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 409: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

409

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 410: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

410

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1Note that we’ve swapped addresses (much faster than swapping elements)

Page 411: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

411

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 412: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

412

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 413: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

413

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 414: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

414

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 415: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

415

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]

i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i : -1

Page 416: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

416

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]

i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 417: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

417

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1

while i > -1 and arr[ i ]->info > tempPtr->infoarr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 418: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

418

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 419: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

419

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

no effect

Page 420: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

420

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 421: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

421

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 422: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

422

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 423: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

423

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 424: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

424

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]

i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 425: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

425

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]

i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 426: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

426

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1

while i > -1 and arr[ i ]->info > tempPtr->infoarr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 427: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

427

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 428: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

428

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 429: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

429

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]

i—arr[ i + 1] = tempPtr

j

tempPtr

i

Page 430: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

430

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]

i—arr[ i + 1] = tempPtr

j

tempPtr

i

Page 431: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

431

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1

while i > -1 and arr[ i ]->info > tempPtr->infoarr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 432: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

432

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 433: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

433

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Page 434: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

434

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

The array is now sorted according to the pointers

Page 435: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

435

Step 2 (cont.)

0 1 2 3

start 6 3 8 7

for each j, from 1 to the length of arr – 1

tempPtr = arr[ j ]i = j – 1while i > -1 and arr[ i ]->info > tempPtr->info

arr[ i + 1 ] = arr[ i ]i—

arr[ i + 1] = tempPtr

j

tempPtr

i

Now, in step 3, we just relink the linked list

Page 436: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

436

Step 3

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

Page 437: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

437

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 438: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

438

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 439: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

439

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 440: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

440

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 441: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

441

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 442: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

442

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 443: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

443

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 444: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

444

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 445: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

445

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 446: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

446

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 447: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

447

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 448: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

448

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 449: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

449

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 450: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

450

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 451: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

451

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

Page 452: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

452

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

The list has been relinked in order, without copying elements

Page 453: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

453

Step 3 (cont.)

0 1 2 3

start 6 3 8 7

for all i from 0 to n – 2A[ i ]->next = A[ i + 1 ]

start = A[ 0 ]A[ n – 1 ]->next = NULL

i

This step also takes ( n ) time

Page 454: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

454

Modifying a SortingAlgorithm for a Linked List

• Those variables that were assigned elements in the sorting algorithm should now be assigned addresses (they become pointers)

• Use ->info when element values need to be compared

Page 455: 1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.

455

Modifying Counting Sortfor a Linked List

• Each element of the C array does not need to be an integer

• An alternative is to use struct objects as elements of the C array

• One data member can be a count for the C array index

• Another data member can be an array of pointers to linked list nodes, relevant to that index of the C array (the data member for which the list is being sorted)