1 c++ classes and data structures jeffrey s. childs chapter 14 introduction to sorting algorithms...

Post on 02-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

C++ Classes and Data StructuresJeffrey S. Childs

Chapter 14

Introduction to Sorting Algorithms

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

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

3

Heapsort

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

pq.dequeue( arr[ i ] );

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

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

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)

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)

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

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

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

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 }

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 };

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

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

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.)

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.)

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.)

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

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…

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

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…

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

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

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

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.

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

27

Insertion Sort (cont.)

3 18 19 22 17 16 6 1 15 2

sorted section unsorted section

28

Insertion Sort (cont.)

3 18 19 22

17

16 6 1 15 2

sorted section unsorted section

29

Insertion Sort (cont.)

3

17

16 6 1 15 2

sorted section unsorted section

22 19 18

30

Insertion Sort (cont.)

3 17 16 6 1 15 2

sorted section unsorted section

22 19 18

31

Insertion Sort (cont.)

sorted section unsorted section

3 17 16 6 1 15 2 22 19 18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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?

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.

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

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

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

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

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…

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 ]

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…

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…

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…

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…

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…

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

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

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.

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

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

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

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 …

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.

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

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)

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)

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)

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

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?

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

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)

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

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

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…

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

149

Functions of Quicksort

• A recursive function, called quicksort

• A nonrecursive function, usually called partition

• quicksort calls partition

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

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

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

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

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

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.

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.)

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.)

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.)

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.)

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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…

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…

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

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

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)

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…

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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)

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

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)

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

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…

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

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

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

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

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

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

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

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

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]++

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

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

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

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

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

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

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

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

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

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…

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

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…

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

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).

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.

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.

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.

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

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

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

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

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

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 ]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 )

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 )

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 )

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 )

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 )

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?

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

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!

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

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

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

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

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

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

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 ]

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)

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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…

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

top related