Download presentation
Presentation is loading. Please wait.
Published byAmie Parks Modified over 9 years ago
1
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall
2
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
3 Heapsort PriorityQueue pq( arr ); for ( i = arr.length( ) – 1; i >= 0; i-- ) pq.dequeue( arr[ i ] );
4
4 Heapsort (cont.) PriorityQueue 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
5 Heapsort (cont.) PriorityQueue 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
6 Heapsort (cont.) PriorityQueue 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
7 Heapsort (cont.) PriorityQueue 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
8 Heapsort (cont.) PriorityQueue 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
9 Heapsort Time Complexity 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
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
11 Heapsort Function Template (in a Client Program) 1 template 2 void heapsort( Array & arr ) 3 { 4PriorityQueue pq( arr ); 5for ( int i = arr.length( ) - 1; i >= 0; i-- ) 6pq.dequeue( arr[ i ] ); 7 }
12
12 Example Using the Heapsort Function Template 1 #include 2 #include "PriorityQueue.h" 3 4 using namespace std; 5 6 struct MyStruct { 7int id; 8float amount; 9bool operator >( const MyStruct & right ) 10{ return amount > right.amount; } 11 };
13
13 Example Using the Heapsort Function Template (cont.) 1 #include 2 #include "PriorityQueue.h" 3 4 using namespace std; 5 6 struct MyStruct { 7int id; 8float amount; 9bool operator >( const MyStruct & right ) 10{ return amount > right.amount; } 11 }; Client will sort an array of MyStruct objects
14
14 Example Using the Heapsort Function Template (cont.) 1 #include 2 #include "PriorityQueue.h" 3 4 using namespace std; 5 6 struct MyStruct { 7int id; 8float amount; 9bool 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
15 12 template 13 void heapsort( Array & arr ); 14 15 int main( ) 16 { 17int num; 18 19cout << "How many records do you want to enter? "; 20cin >> num; 21Array objs( num ); Note that the function prototype for heapsort must also be templated Example Using the Heapsort Function Template (cont.)
16
16 22for ( 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
17 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.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
18 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.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
19 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.dequeue( arr[ i ] ); 45 } Example Using the Heapsort Function Template (cont.) The compiler determines what type objs is…
20
20 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.dequeue( arr[ i ] ); 45 } Example Using the Heapsort Function Template (cont.) then substitutes that type for DataType to make a heapsort function
21
21 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.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
22 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.dequeue( arr[ i ] ); 45 } Example Using the Heapsort Function Template (cont.) …the compiler would make another heapsort function using int for DataType
23
23 28heapsort( objs );. 39 template 40 void heapsort( Array & arr ) 41 { 42PriorityQueue pq( arr ); 43for ( int i = arr.length( ) - 1; i >= 0; i-- ) 44pq.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
24 28heapsort( objs ); 29cout << "Records in sorted order:" << endl; 30for ( 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} 35 36return 0; 37 } Example Using the Heapsort Function Template (cont.) The final part of the main function provides the records in sorted order
25
25 Insertion Sort 3 18 19 22 17 16 6 1 15 2 sorted sectionunsorted 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
26 Insertion Sort (cont.) 3 18 19 22 17 16 6 1 15 2 sorted sectionunsorted section next value to be inserted into sorted section
27
27 Insertion Sort (cont.) 3 18 19 22 17 16 6 1 15 2 sorted sectionunsorted section
28
28 Insertion Sort (cont.) 3 18 19 22 17 16 6 1 15 2 sorted sectionunsorted section
29
29 Insertion Sort (cont.) 3 17 16 6 1 15 2 sorted sectionunsorted section 22 19 18
30
30 Insertion Sort (cont.) 3 17 16 6 1 15 2 sorted sectionunsorted section 22 19 18
31
31 Insertion Sort (cont.) sorted sectionunsorted section 3 17 16 6 1 15 2 22 19 18
32
32 Insertion Sort (cont.) sorted sectionunsorted 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
33 Insertion Sort (cont.) sorted sectionunsorted 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
34 Insertion Sort (cont.) sorted sectionunsorted 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 – 1 2 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--
35
35 Insertion Sort (cont.) sorted sectionunsorted 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 – 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--
36
36 Insertion Sort (cont.) sorted sectionunsorted 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 – 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--
37
37 Insertion Sort (cont.) sorted sectionunsorted 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 – 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--
38
38 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
39 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
40
40 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
41
41 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
42
42 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
43
43 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
44
44 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
45
45 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
46 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
47
47 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
48
48 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
49
49 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
50
50 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
51
51 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
52
52 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
53 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
54
54 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
55
55 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
56
56 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
57
57 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
58
58 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
59
59 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
60 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
61
61 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
62
62 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
63
63 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
64
64 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
65
65 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
66
66 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
67 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
68
68 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
69
69 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
70
70 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
71
71 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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
72 Insertion Sort (cont.) sorted sectionunsorted section 1 for each j, from 1 to the length of A – 1 2 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 etc.
73
73 Starting it Off 1 for each j, from 1 to the length of A – 1 2 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-- Why is it that we start j off at 1 instead of 0?
74
74 Starting it Off (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 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
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
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
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
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
79 Inversions and Swaps 1 for each j, from 1 to the length of A – 1 2 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-- A swap in insertion sort removes an inversion…
80
80 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- because when performed, A[ i ] was greater than A[ i + 1 ]
81
81 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 16 19 But it can’t remove more than one inversion – those elements inverted with 16 in the shaded part of the array…
82
82 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 16 19 will still be inverted with 16 after the swap…
83
83 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 16 19 will still be inverted with 16 after the swap…
84
84 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 16 19 will still be inverted with 16 after the swap…
85
85 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 16 19 will still be inverted with 16 after the swap…
86
86 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 16 19 The same is true with 19
87
87 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 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
88 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 1 2 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 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
89 Worst Number of Inversions 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
90 Worst Number of Inversions (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
91 Worst Number of Inversions (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
92 Worst Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- Therefore, in the worst case, the number of swaps performed is n( n – 1 ) / 2 …
93
93 Worst Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- Therefore, in the worst case, the TOTAL number of swaps performed is n( n – 1 ) / 2 = ½ n 2 – ½ n which gives us the worst-case time complexity of ( n 2 ) for insertion sort.
94
94 Average Number of Inversions 1 for each j, from 1 to the length of A – 1 2 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-- 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
95 Average Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 50% of n( n – 1 ) / 2 = ½ * n ( n – 1 ) / 2 = n( n – 1 ) / 4 (still ( n 2 ) on average)
96
96 Best Number of Inversions 1 for each j, from 1 to the length of A – 1 2 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-- 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
97 Best Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 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
98 Best Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 21 25 30 32 40 0 1 2 3 4 Thus, in the best case, insertion sort runs in ( n ) time
99
99 Best Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 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
100 Best Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 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
101 Best Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 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
102 Best Number of Inversions (cont.) 1 for each j, from 1 to the length of A – 1 2 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-- 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
103 Another Advantage of 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
104 Speeding up Insertion 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
105 One-Assignment Swap 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted 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
106 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 16 1 15 2 22 19 18 j
107
107 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 16 1 15 2 22 19 18 j
108
108 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted 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
109 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted 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
110 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted 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
111 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted 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
112 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted 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
113 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i
114
114 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i
115
115 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i 22
116
116 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i 22
117
117 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i 22
118
118 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i 22
119
119 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 22 19 18 j temp: 16 i 22
120
120 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
121
121 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
122
122 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
123
123 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
124
124 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
125
125 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
126
126 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
127
127 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
128
128 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
129
129 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
130
130 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
131
131 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
132
132 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
133
133 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
134
134 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
135
135 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 j temp: 16 i 22
136
136 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 17 j temp: 16 i 22
137
137 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 17 j temp: 16 i 22
138
138 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 17 j temp: 16 i 22
139
139 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 17 j temp: 16 i 22
140
140 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 17 j temp: 16 i 22
141
141 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 17 15 1 2 19 18 17 j temp: 16 i 22
142
142 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 15 1 2 19 18 17 j temp: 16 i 22
143
143 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 16 15 1 2 19 18 17 j temp: 16 i 22
144
144 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 16 15 1 2 19 18 17 j temp: 16 i 22
145
145 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 16 15 1 2 19 18 17 j temp: 16 i 22
146
146 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 16 15 1 2 19 18 17 j temp: 16 i 22
147
147 One-Assignment Swap (cont.) 1 for each j, from 1 to the length of A – 1 2 temp = A[ j ] 3 i = j – 1 4 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 sectionunsorted section 0 1 2 3 4 5 6 7 8 9 3 16 15 1 2 19 18 17 j temp: 16 i 22 etc.
148
148 Quicksort Time complexities of Quicksort –best: ( n lg n ) –average: ( n lg n ) –worst: ( n 2 ) 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
149 Functions of Quicksort A recursive function, called quicksort A nonrecursive function, usually called partition quicksort calls partition
150
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
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
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 38 47 11 3 6 4 35 46 All elements less than or equal to the pivot are on its left
153
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 38 47 11 3 6 4 35 46 All elements greater than the pivot are on its right side
154
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 38 47 11 3 6 4 35 46 The pivot is where it will be in the final sorted array
155
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 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
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 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
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 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
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 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
159 0 1 2 3 4 5 6 7 8 9 10 11 14 10 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
160 0 1 2 3 4 5 6 7 8 9 10 11 14 10 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
161 0 1 2 3 4 5 6 7 8 9 10 11 14 10 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
162 0 1 2 3 4 5 6 7 8 9 10 11 14 10 28 38 47 11 3 6 4 35 46 Partition (cont.) pivot
163
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
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
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
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
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
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
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.) pivot All elements are smaller and stay to the left of the pivot
170
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 ij
198
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 ij
199
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 ij
200
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 ij
201
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 ij
202
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 ij
203
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 ij
204
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 ij
205
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 ij
206
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 ij
207
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 ij
208
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 ij
209
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 ij
210
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 ij
211
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 ij
212
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 ij
213
213 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 Partition starts off by passing in the array arr…
214
214 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 the beginning index of the array section it is working with…
215
215 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 and the ending index of the array section it is working with
216
216 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 i is used to mark the end of the small-side part of the partition
217
217 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 initially, there isn’t one, so i is set to p – 1 (-1 if p is 0)
218
218 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 this loop iterates through the elements…
219
219 Partition (cont.) 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 comparing them to the pivot
220
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 j is currently 7, with partition having already iterated through elements 0 through 6 p r
221
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
222
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
223
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
224
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
225
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
226
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
227
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
228
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
229
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
230
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
231
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
232
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
233
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
234
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
235
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
236
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
237
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
238
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
239
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
240
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
241
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
242
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
243
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
244
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
245
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
246
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
247
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
248
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
249
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
250
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
251
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
252
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
253
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
254
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
255
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
256
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
257
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
258
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r
259
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
260
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
261
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
262
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35 j isn’t incremented past r - 1
263
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
264
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
265
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
266
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
267
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
268
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35
269
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35 46
270
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35 46
271
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return i + 1 p r 35 46
272
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 ij 1 partition( arr, p, r ) 2i = p – 1 3for all j from p to r – 1 4if arr[ j ] <= arr[ r ] 5i++ 6swap( arr[ i ], arr[ j ] ) 7swap ( arr[ i + 1 ] and arr[ r ] ) 8return 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
273 The Quicksort Function 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( 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
274 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( 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
275 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) The index of the pivot is returned from partition and assigned to q
276
276 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) The index of the pivot is returned from partition and assigned to q pr q
277
277 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) quicksort is called recursively here, working with the section on the left of the pivot pr q
278
278 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) quicksort is called recursively here, working with the section on the left of the pivot pr q q-1
279
279 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) and quicksort is called recursively here, working with the section on the right of the pivot pr q q-1
280
280 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) and quicksort is called recursively here, working with the section on the right of the pivot pr q q-1q+1
281
281 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( 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
282 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) and partition will break the sections into even smaller sections
283
283 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( 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
284 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( arr, q + 1, r ) If the recursive case occurs, a call to quicksort will match a call to partition
285
285 The Quicksort Function (cont.) 1quicksort( arr, p, r ) 2if p < r 3q = partition( arr, p, r ) 4quicksort( arr, p, q – 1 ) 5quicksort( 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
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
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
288 Counting Sort Step 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
289 Counting Sort Step 1 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0
290
290 Counting Sort Step 1 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 0 1 2 3 4 5 6 7 C
291
291 Counting Sort Step 1 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 0 1 2 3 4 5 6 7 C
292
292 Counting Sort Step 1 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 C
293
293 Counting Sort Step 2 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 C
294
294 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 C j
295
295 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 C j
296
296 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ 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
297 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 0 0 0 0 1 2 3 4 5 6 7 C j
298
298 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 0 0 0 0 1 2 3 4 5 6 7 C j
299
299 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 0 0 0 0 1 2 3 4 5 6 7 C j
300
300 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 0 0 0 0 1 2 3 4 5 6 7 C j
301
301 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 1 0 0 0 1 2 3 4 5 6 7 C j
302
302 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 1 0 0 0 1 2 3 4 5 6 7 C j
303
303 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 1 0 0 0 1 2 3 4 5 6 7 C j
304
304 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 1 0 0 0 1 2 3 4 5 6 7 C j
305
305 Counting Sort Step 2 (cont.) 4 5 5 2 6 5 5 7 0 2 0 1 2 3 4 5 6 7 8 9 A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 0 0 0 0 1 2 0 0 0 1 2 3 4 5 6 7 C j
306
306 Counting Sort Step 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
307 Counting Sort Step 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
308 Counting Sort Step 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
309 Counting Sort Step 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
310 Counting Sort Step 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
311 Counting Sort Step 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
312 Counting Sort Step 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
313 Counting Sort Step 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
314 Counting Sort Step 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 7j = 0
315
315 Counting Sort Step 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 7j = 0 j
316
316 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++
317
317 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
318
318 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
319
319 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i m counts the number of times i has to be written into A[ j ]
320
320 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one time
321
321 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one time
322
322 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one time
323
323 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one time
324
324 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one time
325
325 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
326
326 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
327
327 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
328
328 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i 0 times
329
329 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
330
330 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
331
331 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
332
332 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i two times
333
333 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i two times
334
334 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i two times
335
335 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i two times
336
336 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i two times
337
337 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
338
338 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one more time
339
339 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one more time
340
340 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one more time
341
341 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one more time j
342
342 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i one more time j
343
343 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i etc. j
344
344 Counting Sort Step 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 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i etc. j
345
345 Analysis of Counting Sort 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++
346
346 Analysis of Counting Sort (cont.) 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ ( 1 )
347
347 Analysis of Counting Sort (cont.) 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ ( k ) ( 1 )
348
348 Analysis of Counting Sort (cont.) 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ ( k ) ( 1 ) ( n )
349
349 Analysis of Counting Sort (cont.) 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ ( k ) ( 1 ) ( n ) ( 1 )
350
350 Analysis of Counting Sort (cont.) 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ ( k ) ( 1 ) ( n ) ( 1 ) So far, we have ( 1 ) + ( k ) + ( n ) + ( 1 ), which is ( n ) + ( k )
351
351 Analysis of Counting Sort (cont.) 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ]++ 7j = 0 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ ( k ) ( 1 ) ( n ) ( 1 ) What about this nested loop?
352
352 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++
353
353 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ 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
354 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ Thus, lines 8-11 would appear to have a ( n ) time complexity…but there is more to it
355
355 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ 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
356 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ C[ i ] is checked to see if it is empty k + 1 times
357
357 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ Hence, these lines execute in ( k ) + ( n ) time and this is the time complexity of counting sort
358
358 Analysis of Counting Sort (cont.) 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ 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
359 Modifications to Counting 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
360 Modifications to Counting 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 ] > max then max = A[ j ] else if A[ j ] < min then min = A[ j ]
361
361 Modifications to Counting 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
362 Modifications to Counting 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
363 When Counting Sort Isn’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
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
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
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
367 Array of Pointers We use a dynamic array of pointers into the linked list Declared like this: Node ** arr = new Node * [numElements]; Why?
368
368 Array of Pointers (cont.) We use a dynamic array of pointers into the linked list Declared like this: Node ** arr = new Node * [numElements]; arr will point to the first element and the first element is a pointer
369
369 Array of Pointers (cont.) We use a dynamic array of pointers into the linked list Declared like this: Node ** arr = new Node * [numElements]; Therefore, arr is a pointer to a pointer
370
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
371 Step 1 i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 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
372 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start 6 3 8 7
373
373 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i 6 3 8 7
374
374 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i 6 3 8 7
375
375 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
376
376 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
377
377 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
378
378 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
379
379 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
380
380 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
381
381 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
382
382 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
383
383 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
384
384 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
385
385 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
386
386 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
387
387 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
388
388 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
389
389 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
390
390 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr 6 3 8 7
391
391 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start i ptr etc. 6 3 8 7
392
392 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start ptr : NULL etc. 6 3 8 7
393
393 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 0 1 2 3 start ptr : NULL This loop is a ( n ) loop and did not copy any elements 6 3 8 7
394
394 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i++ 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
395 Step 2 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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr
396
396 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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j
397
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j In the original insertion sort, temp held an element; now it holds an address
398
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j In the original insertion sort, temp held an element; now it holds an address tempPtr
399
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr
400
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
401
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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i This overloaded operator should pass its parameter by const reference…
402
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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i to completely avoid copying these large elements
403
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
404
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
405
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
406
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
407
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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
408
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
409
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
410
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1 Note that we’ve swapped addresses (much faster than swapping elements)
411
411 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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
412
412 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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
413
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
414
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
415
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i : -1
416
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
417
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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
418
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
419
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i no effect
420
420 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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
421
421 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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
422
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
423
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
424
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
425
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
426
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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
427
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
428
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
429
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
430
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
431
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->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
432
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
433
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 – 1 while i > -1 and arr[ i ]->info > tempPtr->info arr[ i + 1 ] = arr[ i ] i— arr[ i + 1] = tempPtr j tempPtr i
434
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 – 1 while 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
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 – 1 while 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
436 Step 3 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL
437
437 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
438
438 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
439
439 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
440
440 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
441
441 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
442
442 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
443
443 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
444
444 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
445
445 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
446
446 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
447
447 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
448
448 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
449
449 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
450
450 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
451
451 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
452
452 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ 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
453 Step 3 (cont.) 0 1 2 3 start 6 3 8 7 for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i This step also takes ( n ) time
454
454 Modifying a Sorting Algorithm 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
455 Modifying Counting Sort for 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.