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 Sorting Sorting is the process of placing elements in order If elements are objects, they are ordered by a particular data member There are many algorithms for sorting, each having its own advantages No sorting algorithm is better than every other sorting algorithm all the time Choosing the right sorting algorithm for a problem requires careful consideration
3 Heapsort PriorityQueue pq( arr ); for ( i = arr.length( ) – 1; i >= 0; i-- ) pq.dequeue( arr[ i ] );
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 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 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 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 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 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 Function Templates A function template can be made for heapsort, so that the client can put heapsort into the client program A function template is something like a class template – the compiler makes functions from the function template The client can then use heapsort for different types of arrays, without rewriting the heapsort function
11 Heapsort Function Template (in a Client Program) 1 template 2 void heapsort( Array & arr ) 3 { 4PriorityQueue pq( arr ); 5for ( int i = arr.length( ) - 1; i >= 0; i-- ) 6pq.dequeue( arr[ i ] ); 7 }
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 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 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 12 template 13 void heapsort( Array & arr ); 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 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 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 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 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 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 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 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 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 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 Insertion Sort 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 Insertion Sort (cont.) sorted sectionunsorted section next value to be inserted into sorted section
27 Insertion Sort (cont.) sorted sectionunsorted section
28 Insertion Sort (cont.) sorted sectionunsorted section
29 Insertion Sort (cont.) sorted sectionunsorted section
30 Insertion Sort (cont.) sorted sectionunsorted section
31 Insertion Sort (cont.) sorted sectionunsorted section
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
33 Insertion Sort (cont.) sorted sectionunsorted section 16 would be the next value to be “inserted”, giving insertion sort its name
34 Insertion Sort (cont.) sorted sectionunsorted section In the algorithm, j is used to mark the index of the next element to be inserted 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 Insertion Sort (cont.) sorted sectionunsorted section j 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 Insertion Sort (cont.) sorted sectionunsorted section j 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 Insertion Sort (cont.) sorted sectionunsorted section j 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 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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i
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 j i etc.
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 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 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…
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: one inversion
77 Inversions (cont.) An inversion between any two elements when the element that comes first in the array is greater than the element that comes next The array below has three inversion: a second inversion
78 Inversions (cont.) An inversion between any two elements when the element that comes first in the array is greater than the element that comes next The array below has three inversion: a third inversion
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 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 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 But it can’t remove more than one inversion – those elements inverted with 16 in the shaded part of the array…
82 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 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 will still be inverted with 16 after the swap…
83 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 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 will still be inverted with 16 after the swap…
84 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 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 will still be inverted with 16 after the swap…
85 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 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 will still be inverted with 16 after the swap…
86 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 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 The same is true with 19
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 So a swap removes one and only one inversion each time it is executed; it is also the only way inversions are removed
88 Inversions and Swaps (cont.) 1 for each j, from 1 to the length of A – 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, the number of swaps performed in all of insertion sort is equal to the number of inversions in the original array.
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)
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 )?
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 ) /
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 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 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 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 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 There are no inversions in an array that is already sorted, so the condition in the while loop is never true (no swaps)
97 Best Number 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 The outer loop still executes n – 1 times (the algorithm does not know it is already sorted)
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 Thus, in the best case, insertion sort runs in ( n ) time
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 Does an array have to be already sorted in order for insertion sort to run in ( n ) time?
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 Consider the case where we have n inversions, so we have a total of n swaps
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 For each iteration of the for loop, a swap is executed an average of 1 time (approximately)
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 Hence, we would still have ( n ) time if we have n inversions or less
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 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 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 With sorting in progress, let’s suppose j is set to 6 here
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 j
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 j
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 j temp: 16
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 j temp: 16
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 j temp: 16 i
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 j temp: 16 i
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 j temp: 16 i
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 j temp: 16 i
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 j temp: 16 i
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22
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 j temp: 16 i 22 etc.
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 Functions of Quicksort A recursive function, called quicksort A nonrecursive function, usually called partition quicksort calls partition
150 Partition In the partition function, the last element is chosen as a pivot – a special element used for comparison purposes pivot
151 Partition (cont.) Each element is compared to the pivot. When partition is done, it produces the result shown next… pivot
152 Partition (cont.) pivot All elements less than or equal to the pivot are on its left
153 Partition (cont.) pivot All elements greater than the pivot are on its right side
154 Partition (cont.) pivot The pivot is where it will be in the final sorted array
155 Partition (cont.) pivot Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array.
pivot Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array. Partition (cont.)
pivot Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array. Partition (cont.)
pivot Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array. Partition (cont.)
Partition is called from quicksort more than once, but when called again, it will work with a smaller section of the array. Partition (cont.)
Partition (cont.) The next time partition is called, for example, it will only work with this section to the left of the previous pivot.
Partition (cont.) The next time partition is called, for example, it will only work with this section to the left of the previous pivot.
Partition (cont.) pivot
Partition (cont.) pivot
Partition (cont.) Each section of the array separated by a previous pivot will eventually have partition called for it
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
Partition (cont.) Partition is not called for a section of just one element – it is already a pivot and it is where it belongs
Partition (cont.) Partition is called for this section
Partition (cont.) pivot
Partition (cont.) pivot All elements are smaller and stay to the left of the pivot
Partition (cont.) Partition is called for this section
Partition (cont.) pivot
Partition (cont.) pivot
Partition (cont.) partition
Partition (cont.) pivot
Partition (cont.) pivot
Partition (cont.) partition not called for this
Partition (cont.) partition not called for this
Partition (cont.) partition not called for this
Partition (cont.) partition not called for this
Partition (cont.) partition called for this
Partition (cont.) pivot
Partition (cont.) pivot
Partition (cont.) partition called for this
Partition (cont.) pivot
Partition (cont.) pivot
Partition (cont.) Partition not called for this
Partition (cont.) Partition not called for this
Partition (cont.) Partition not called for this
Partition (cont.) Partition not called for this
Partition (cont.) At this point, the array is sorted
Partition (cont.) But to achieve this, what steps does the algorithm for partition go through?
192 Partition (cont.) pivot Partition has a loop which iterates through each element, comparing it to the pivot
193 Partition (cont.) pivot When partition is in progress, there is a partitioned section on the left, and an unpartitioned section on the right partitioned section unpartitioned section
194 Partition (cont.) pivot The dividing line in the partitioned section means that all elements to the left are less than or equal to the pivot; all elements to the right are greater than the pivot partitioned section unpartitioned section
195 Partition (cont.) pivot On each iteration, the partitioned section grows by one and the unpartitioned section shrinks by one, until the array is fully partitioned partitioned section unpartitioned section
196 Partition (cont.) pivot i is an index used to mark the last value in the small side of the partition, while j is used to mark the first value in the unpartitioned section. partitioned section unpartitioned section
197 Partition (cont.) 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 Partition (cont.) pivot On each iteration of partition, the value at j is compared to the pivot. partitioned section unpartitioned section ij
199 Partition (cont.) pivot On each iteration of partition, the value at j is compared to the pivot. partitioned section unpartitioned section ij
200 Partition (cont.) pivot If the value at j is greater, j is just incremented (the partitioned section grows by one) partitioned section unpartitioned section ij
201 Partition (cont.) pivot If the value at j is greater, j is just incremented (the partitioned section grows by one) partitioned section unpartitioned section ij
202 Partition (cont.) pivot If, on an iteration, the value at j is less than the pivot… partitioned section unpartitioned section ij
203 Partition (cont.) pivot then i is incremented… partitioned section unpartitioned section ij
204 Partition (cont.) pivot then i is incremented… partitioned section unpartitioned section ij
205 Partition (cont.) pivot and the value at i is swapped with the value at j… partitioned section unpartitioned section ij
206 Partition (cont.) pivot and the value at i is swapped with the value at j… partitioned section unpartitioned section ij
207 Partition (cont.) pivot and the value at i is swapped with the value at j… partitioned section unpartitioned section ij
208 Partition (cont.) pivot and the value at i is swapped with the value at j… partitioned section unpartitioned section ij
209 Partition (cont.) pivot and the value at i is swapped with the value at j… partitioned section unpartitioned section ij
210 Partition (cont.) pivot and the value at i is swapped with the value at j… partitioned section unpartitioned section ij
211 Partition (cont.) pivot then j is incremented partitioned section unpartitioned section ij
212 Partition (cont.) pivot then j is incremented partitioned section unpartitioned section ij
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 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 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 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 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 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 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 Partition (cont.) 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 The final step of partition returns the INDEX of the pivot back to the quicksort function that called it
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 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 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 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 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 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 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 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 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 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 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 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 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 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 – = 6 If the range is smaller than n, counting sort is very fast, running in ( n ) time
287 Counting Sort (cont.) The main counting sort algorithm works with elements that range in value from 0 to some positive integer k However, with a little modification, counting sort can work with nearly any data, still maintaining a ( n ) time complexity Let’s look at the main counting sort algorithm…
288 Counting Sort Step This is the array A to be sorted. It’s values range from 0 to 7, giving a range of 8. 8 < n (since n = 10), so counting sort should be fast on this array. A
289 Counting Sort Step 1 (cont.) A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = 0
290 Counting Sort Step 1 (cont.) A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = C
291 Counting Sort Step 1 (cont.) A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = C
292 Counting Sort Step 1 (cont.) A 1COUNTING-SORT( A ) 2make an Array C of length k + 1 3for each i, from 0 to k 4C[ i ] = C
293 Counting Sort Step A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C
294 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
295 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
296 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j A[ j ] is 4, so this is really C[4]++
297 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
298 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
299 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
300 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
301 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
302 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
303 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
304 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
305 Counting Sort Step 2 (cont.) A 5for each j, from 0 to the length of A - 1 6C[ A[ j ] ] C j
306 Counting Sort Step 2 (cont.) A C j Notice that the C array is being used to “count” the number of values in the A array…
307 Counting Sort Step 2 (cont.) A C j Since C[ 5 ] is 2, it means we’ve found 2 5’s in the A array so far
308 Counting Sort Step 2 (cont.) A C j Thus, at the end of this loop, the C array will look like this…
309 Counting Sort Step 2 (cont.) A C j Thus, at the end of this loop, the C array will look like this
310 Counting Sort Step 2 (cont.) A C j The total of the values in C is equal to n (since it a total count of the number of elements).
311 Counting Sort Step 2 (cont.) A C j Notice that we don’t need the values in the A array any more – all information is contained in C.
312 Counting Sort Step 2 (cont.) A C Notice that we don’t need the values in the A array any more – all information is contained in C.
313 Counting Sort Step 2 (cont.) A C Now, it is just a matter of writing the appropriate indexes of C into the A array (one 0 goes in the first spot, two 2’s go in the next two places, etc.
314 Counting Sort Step A C 7j = 0
315 Counting Sort Step 3 (cont.) A C 7j = 0 j
316 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++
317 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
318 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
319 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
326 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
327 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
328 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
330 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
331 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
332 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A C j 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i
338 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A 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 Counting Sort Step 3 (cont.) A C 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i etc. j
344 Counting Sort Step 3 (cont.) A C 8for each i from 0 to k 9for each m from 1 to C[ i ] 10A[ j ] = i 11j++ i etc. j
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Sorting a Linked List Linked lists can be sorted too Consider going through a linked list, element by element, assigning each value to an array ( ( n ) time ) Then, sort the array Then, go through the linked list, assigning each value of the array to the linked list
365 Sorting a Linked List (cont.) A sorting algorithm must take at least ( n ) time if nothing about the elements is known ahead of time –it takes this long just to look at the elements Therefore, assigning elements from linked list to array and back to linked list again will not affect the time complexity of a sorting algorithm
366 Copying Elements Recall that elements in linked lists will tend to be large –a reason for using linked lists Therefore, it would be very time- consuming to do this element copying We can sort a linked list without copying elements
367 Array of Pointers We use a dynamic array of pointers into the linked list Declared like this: Node ** arr = new Node * [numElements]; Why?
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 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 numElements We need numElements to make the declaration Set to 0 inside the constructor Increment when adding an element Decrement when removing Set to 0 when makeEmpty is called
371 Step 1 i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start Note: Only the data members to be sorted are shown in the linked list – the objects may be quite large
372 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start
373 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i
374 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i
375 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
376 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
377 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
378 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
379 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
380 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
381 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
382 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
383 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
384 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
385 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
386 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
387 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
388 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
389 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
390 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr
391 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start i ptr etc
392 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start ptr : NULL etc
393 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start ptr : NULL This loop is a ( n ) loop and did not copy any elements
394 Step 1 (cont.) i = 0 ptr = start while ptr is not equal to NULL arr[ i ] = ptr ptr = ptr->next i start ptr : NULL Step 2 uses a modified sorting algorithm – instead of moving elements, we move array pointers We’ll use insertion sort…
395 Step start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step 2 (cont.) start 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 Step start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL
437 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
438 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
439 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
440 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
441 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
442 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
443 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
444 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
445 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
446 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
447 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
448 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
449 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
450 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
451 Step 3 (cont.) start for all i from 0 to n – 2 A[ i ]->next = A[ i + 1 ] start = A[ 0 ] A[ n – 1 ]->next = NULL i
452 Step 3 (cont.) start 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 Step 3 (cont.) start 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 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 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)