Lists Stacks and Queues 2 struct Node double
Lists, Stacks and Queues
2 struct Node{ double data; Node* next; }; More complete list ADT class List { public: List(); // constructor List(const List& list); // copy constructor ~List(); // destructor List& operator=(const List& list); // assignment operator // plus other overloaded operators bool empty() const; void add(double x); void delete(); double head() const; bool search(double x); void insert(double x); void delete(double x); void add. End(double x); void delete. End(); double end(); void display() const; int length() const; private: Node* head; }; // boolean function // add to the head // delete the head and get the head element // search for a given x // insert x in a sorted list // delete x in a sorted list // add to the end // delete the end and get the end element // get the element at the end // output // count the number of elements
3 list ADT (a general list) class List { public: List(); List(const List& list); ~List(); // constructor // copy constructor // destructor bool empty() const; double head() const; void add(double x); void delete(); // // boolean function get the head element add to the head delete the head element void display() const; // output private: … }; Or to define one function: Double delete(); Which deletes and returns the head element.
4 list ADT (a sorted list) class List { public: List(); List(const List& list); ~List(); bool empty() const; double head(); const; void insert(double x); void delete(double x); // // void display() const; bool search(double x); // output // search for a given x private: … }; // constructor // copy constructor // destructor boolean function get the first element insert x in a sorted list delete x in a sorted list
5 Implementation and Efficiency * Implementation Static array n Dynamic array n Linked lists n 1 with and without ‘dummy head’ tricks * Efficiency Insertion construction, composition n Deletion destruction, decomposition n Search application, usage n
6 Efficiency: Algorithm Analysis * Roughly ‘counting’ the number of operations, assuming all basis operations are of the same, unit one.
7 A Few Simple Rules * Loops at most the running time of the statements inside the for-loop (including tests) times the number of iterations. n O(N) n * Nested loops the running time of the statement multiplied by the product of the sizes of all the for-loops. n O(N 2) n
8 * Consecutive statements These just add n O(N) + O(N 2) = O(N 2) n * Conditional: If S 1 else S 2 never more than the running time of the test plus the larger of the running times of S 1 and S 2. n O(1) n
9 Our first example: search of an ordered array * Linear search and binary search * Upper bound, lower bound and tight bound
10 Linear search: // Given an array of ‘size’ in increasing order, find ‘x’ int linearsearch(int* a[], int size, int x) { int low=0, high=size-1; for (int i=0; i<size; i++) if (a[i]==x) return i; return -1; } O(N)
11 Iterative binary search: int bsearch(int* a[], int size, int x) { int low=0, high=size-1; while (low<=higt) { int mid=(low+high)/2; if (a[mid]<x) low=mid+1; else if (x<a[mid]) high=mid-1; else return mid; } return -1 }
12 Iterative binary search: int bsearch(int* a[], int size, int x) { int low=0, high=size-1; while (low<=higt) { int mid=(low+high)/2; if (a[mid]<x) low=mid+1; else if (x<a[mid]) high=mid-1; else return mid; } return -1 } n=high-low l n_i+1 <= n_i / 2 l i. e. n_i <= (N-1)/2^{i-1} l N stops at 1 or below l there at most 1+k iterations, where k is the smallest such that (N-1)/2^{k-1} <= 1 l so k is at most 2+log(N-1) l O(log N) l
13 Recursive binary search: int bsearch(int* a[], int low, int high, int x) { if (low>high) return -1; else int mid=(low+high)/2; if (x=a[mid]) return mid; else if(a[mid]<x) bsearch(a, mid+1, high, x); else bsearch(a, low, mid-1); } O(1) T(N/2)
14 Solving the recurrence: * With 2 k = N (or asymptotically), k=log N, we have * Thus, the running time is O(log N)
15 Advantages and disadvantages of different implementations of a list Static array Insertion Deletion Search Dynamic array Linked list
16 Applications: Linked Implementation of Sparse Polynomials * Consider n a polynomial of degree n Can be represented by a list my. Degree 5 0 1 2 4 5 6 7 8 9 my. Coeffs 5 7 0 -8 0 4 0 0 * For 3 a sparse polynomial this is not efficient my. Degree 99 0 1 2 3 4 5 6 7 8 9 95 96 97 98 99 my. Coeffs 5 0 0 0 0 0 … 0 0 1 COMP 152 … 16
17 * We could represent a polynomial by a list of ordered pairs { (coef, exponent) … } my. Degree 99 my. Degree 5 0 my. Terms 5 * Fixed n 1 0 7 2 1 -8 3 0 3 4 5 my. Terms 5 1 0 1 99 capacity of array still problematic Wasted space for sparse polynomial COMP 152 17
18 * * * Linked list of these ordered pairs provides an appropriate solution Now whether sparse or well populated, the polynomial is represented efficiently Polynomial class (Polynomial. h) Type parameter Coef. Type n Term and Node are inner private classes for internal use and access only n Used to create internal data structure of a linked node (for internal access only) n my. Degree 5 my. Terms COMP 152 data 0 0 5 next dummy/header/ sentinel 0 7 1 -8 3 4 5 / 18
19 Stacks
20 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations array n linked list n of stacks using
21 Stack * A stack is a list in which insertion and deletion take place at the same end This end is called top n The other end is called bottom n * Stacks are known as LIFO (Last In, First Out) lists. n The last element inserted will be the first to be retrieved
22 Push and Pop * Primary operations: Push and Pop * Push n Add an element to the top of the stack * Pop n Remove the element at the top of the stack empty stack push an element top top A push another B A pop top A
23 Implementation of Stacks * Any list implementation could be used to implement a stack Arrays (static: the size of stack is given initially) n Linked lists (dynamic: never become full) n * We will explore implementations based on arrays and linked list
24 Stack ADT class Stack { public: Stack(); Stack(const Stack& stack); ~Stack(); head. Element add. Head delete. Head bool empty() const; double top() const; void push(const double x); double pop(); … ‘physical’ // constructor // copy constructor/destructor // keep the stack unchangedinspection, access // change the stack update, ‘logical’ constructor/destructor, composition/decompositio // optional void display() const; bool full() const; private: … }; Compare with List, see that it’s ‘operations’ that define the type! ‘stack’ is the same as our ‘unsorted’ list operating at the head
25 Using Stack int main() { Stack stack; stack. push(5. 0); stack. push(6. 5); stack. push(-3. 0); stack. push(-8. 0); stack. display(); cout << "Top: " << stack. top() << endl; stack. pop(); cout << "Top: " << stack. top() << endl; while (!stack. empty()) stack. pop(); stack. display(); return 0; } result
26 Stack using linked lists struct Node{ public: double data; Node* next; }; class Stack { public: Stack(); Stack(const Stack& stack); ~Stack(); bool empty() const; double top() const; void push(const double x); double pop(); bool full(); void display() const; private: Node* top; }; // constructor // copy constructor // destructor // keep the stack unchanged // change the stack // unnecessary for linked lists
27 Push (add. Head), Pop (delete. Head) void List: : add. Head(int newdata){ Nodeptr new. Ptr = new Node; new. Ptr->data = newdata; new. Ptr->next = head; From ‘add. Head’ to ‘push’ head = new. Ptr; } void Stack: : push(double x){ Node* new. Ptr = new Node; new. Ptr->data = x; new. Ptr->next = top; top = new. Ptr; }
28 Stack using arrays class Stack { public: Stack(int size = 10); Stack(const Stack& stack); ~Stack() { delete[] values; } bool empty() { return (top == -1); } double top(); void push(const double x); double pop(); bool full() { return (top == size); } void display(); private: int size; int top; double* values; }; // max stack size = size - 1 // current top of stack // element array
29 * Attributes of Stack size: the max size of stack n top: the index of the top element of stack n values: point to an array which stores elements of stack n * Operations of Stack n n n empty: return true if stack is empty, return false otherwise full: return true if stack is full, return false otherwise top: return the element at the top of stack push: add an element to the top of stack pop: delete the element at the top of stack display: print all the data in the stack
30 Stack constructor Allocate a stack array of size. By default, size = 10. n Initially top is set to -1. It means the stack is empty. n When the stack is full, top will have its maximum value, i. e. n size – 1. Stack: : Stack(int size /*= 10*/) { values = new double[size]; top = -1; max. Top = size - 1; } Although the constructor dynamically allocates the stack array, the stack is still static. The size is fixed after the initialization.
31 * void push(const double x); Push an element onto the stack n Note top always represents the index of the top element. After pushing an element, increment top. n void Stack: : push(const double x) { if (full()) // if stack is full, print error cout << "Error: the stack is full. " << endl; else values[++top] = x; }
32 * double pop() Pop and return the element at the top of the stack n Don’t forgot to decrement top n double Stack: : pop() { if (empty()) { //if stack is empty, print error cout << "Error: the stack is empty. " << endl; return -1; } else { return values[top--]; } }
33 * double top() Return the top element of the stack n Unlike pop, this function does not remove the top element n double Stack: : top() { if (empty()) { cout << "Error: the stack is empty. " << endl; return -1; } else return values[top]; }
34 * void n print() Print all the elements void Stack: : print() { cout << "top -->"; for (int i = top; i >= 0; i--) cout << "t|t" << values[i] << "t|" << endl; cout << "t|--------|" << endl; }
35 A better variant: *A better approach is to let position 0 be the bottom of the stack n top = An integer to indicate the top of the stack (Stack. h) [7] ? [6] ? [5] [7] 80 95 [6] 95 [5] 77 [5] 121 [4] 121 [3] 64 [3] [2] 234 [1] [0] [7] ? [6] 77 [4] [7] ? [6] 95 77 [5] 77 [4] 121 64 [3] 64 [2] 234 51 [1] 51 64 [0] 29 top = Push 95 COMP 152 top = Push 80 top = Pop 35
36 Stack Application: base conversion * Consider a program to do base conversion of a number (ten to two) n 26 = (11010)2 * It assumes existence of a Stack class to accomplish this Demonstrates push, pop, and top n Push the remainder onto a stack and pop it up one by one n * Stack. h, Stack. cpp and Base. Conversion. cpp * Can be written easily using recursion (do it yourself) COMP 152 2 26 2 13 … 0 2 6 … 1 2 3 … 0 2 1 … 1 0 … 1 36
37 Stack stack(); while (D is not zero) { push the remainder of D D D/2 } while (not empty stack) pop COMP 152 37
38 convert(D) if D is zero, stop else push(the remainder) convert(D/2) while (not empty stack) pop COMP 152 38
39 Stack Application: Balancing Symbols * To check that every right brace, bracket, and parentheses must correspond to its left counterpart n e. g. [( )] is legal, but [( ] ) is illegal * How? Need to memorize n Use a counter, several counters, each for a type of parenthesis … n
40 Balancing Symbols using a stack * Algorithm (paren. cpp) (1) Make an empty stack. (2) Read characters until end of file i. If the character is an opening symbol, push it onto the stack ii. If it is a closing symbol, then if the stack is empty, report an error iii. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error (3) At end of file, if the stack is not empty, report an error
41 Stack Application: postfix, infix expressions and calculator * Evaluating Postfix expressions abc*+de*f+g*+ n Operands are in a stack n * Converting infix to postfix a+b*c+(d*e+f)*g a b c * + d e * f + g * + n Operators are in a stack n * Calculator n Adding more operators …
42 Infix, prefix, and postfix * Consider the arithmetic statement in the assignment statement: x=a*b+c * This is "infix" notation: the operators are between the operands * Compiler must generate machine instructions COMP 152 1. LOAD a 2. MULT b 3. ADD c 4. STORE x 42
43 Examples Infix RPN (Postfix) Prefix A+B AB+ +AB A*B+C AB*C+ +*ABC A * (B + C) ABC+* *A+BC A - (B - (C - D)) A B C D--- -A-B-C D A-B-C-D A B-C-D- ---A B C D Prefix : Operators come before the operands COMP 152 43
44 RPN or Postfix Notation * Reverse Polish Notation (RPN) = Postfix notation * Most compilers convert an expression in infix notation to postfix * The operators are written after the operands So “a * b + c” becomes “a b * c +” n Easier for compiler to work on loading and operation of variables n * Advantage: expressions can be written without parentheses COMP 152 44
45 Evaluating RPN Expressions (Similar to Compiler Operations) Example: 234+56 --* ® 2756 --* ® ® ® COMP 152 2756 --* 2 7 -1 - * 28* 16 45
46 An expression is a list of ‘characters’ or more generally ‘tokens’ or ‘strings while (not the end of the expression) do { get the current ‘token’ if it is operand, store it … a stack if it is operator, get back operands Evaluate store the result move to the next } Use a Stack to store ‘operands’ a stack! COMP 152 46
47 while (not the end of the expression) { get the current ‘token’ if it is operand, push; else if it is operator, pop; evaluate; push; move to the next } COMP 152 47
48 list expression stack empty while (notempty(list)) do { c = head(list); if (c == ‘operand’) push(c, stack) else if (c == ‘operator) operand 2 = pop(stack); operand 1 = pop(stack); res=evaluate(c, operand 1, operand 2); push(res, stack); list delete(list) } Further refine: - operators: +, -, *, / - operands: … - evaluate: … COMP 152 48
49 More object-oriented: List list(expression); Stack stack(); while (list. notempty) do { c = list. head(); if (c == ‘operand’) stack. push(c) else if (c == ‘operator) operand 2 = stack. pop; operand 1 = stack. pop; res=evaluate(c, operand 1, operand 2); stack. push; list. delete(); } Further refine: - operators: +, -, *, / - operands: … - evaluate: … COMP 152 49
50 2 4 * 9 5 + 2 Push 2 onto the stack 4 2 Push 4 onto the stack 8 Pop 4 and 2 from the stack , multiply, and push the result 8 back Push 9 onto the stack 4 * 9 5 + - + - 9 8 5 9 8 Push 5 onto the stack 14 8 Pop 5 and 9 from the stack, add, and push the result 14 back -6 Pop 14 and 8 from the stack, subtract, and push the result 6 back Value of expression is on top of the stack - (end of strings) COMP 152 -6 50
51 RPN Conversion 1. 2. 3. Given a fully parenthesized infix expression Replace each right parenthesis by the corresponding operator Erase all left parentheses A * B + C ((A * B) + C) ((A B * C + COMP 152 A * (B + C) (A * (B + C) ) (A (B C + * A B C + * 51
52 while (not end of expression) c = the current ‘token’ if c is ‘operator’ store it somewhere … (a stack!) else if c is ‘operand’, display it (or store them in a new expression), A stack for operators, not operands! COMP 152 52
53 while (not end of expression) { c = the current ‘token’ If c is ‘(‘ … else if c is ‘)’ … else if c is ‘operator’ … push … else if c is ‘operand’, … pop … } while (not end of the stack) { pop } COMP 152 53
54 while (not end of expression) c = the current ‘token’ If c is ‘(’ push else if c is ‘)’ while (the top is not ‘(‘ ) pop and display else if c is ‘operator’, while (the top has higher priority than c, and is not ‘(‘ ) pop and display push else if c is ‘operand’ pop and display while (notempty stack) pop an display postfix. cpp COMP 152 54
55 Stack Application: function calls and recursion * Take the example of factorial! And run it. #include <iostream> using namespace std; int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; } void main(){ int number; cout << "Enter a positive integer : " << endl; ; cin >> number; cout << fac(number) << endl; }
56 * Take the example of factorial! And run it. #include <iostream> using namespace std; int fac(int n){ int product; if(n <= 1) product = 1; else product = n * fac(n-1); return product; } void main(){ int number; cout << "Enter a positive integer : " << endl; ; cin >> number; cout << fac(number) << endl; }
57 Tracing the program … Assume the number typed is 3. fac(3): 3<=1 ? No. product 3 = 3*fac(2): 2<=1 ? No. product 2 = 2*fac(1): 1<=1 ? Yes. return 1 has the final returned value 6 product 3=3*2=6, return 6, product 2=2*1=2, return 2,
58 Call is to ‘push’ and return is to ‘pop’! top fac(1) prod 1=1 fac(2) prod 2=2*fac(1) fac(3) prod 3=3*fac(2)
59 Let the system do the ‘stack’ for you! Stack stack(); while (D is not zero) { push the remainder of D convert(D) if D is zero, stop else push(the remainder) convert(D/2) D D/2 } while (not empty stack) pop and display convert(D) if D is zero, stop else convert(D/2) display the remainder of D COMP 152 59
60 Static and dynamic objects * ‘static variables’ are from a Stack * ‘dynamic variables’ are from a heap (seen later …)
61 Array versus linked list implementations * push, pop, top are all constant-time operations in both array and linked list implementation n For array implementation, the operations are performed in very fast constant time
62 Queues *A queue is a waiting line – seen in daily life A line of people waiting for a bank teller n A line of cars at a toll both n "This is the captain, we're 5 th in line for takeoff" n COMP 152 62
63 Queue Overview * Queue ADT * Basic operations of queue n Enqueuing, dequeuing etc. * Implementation Linked list n Array n of queue
64 Queue *A queue is also a list. However, insertion is done at one end, while deletion is performed at the other end. * It is “First In, First Out (FIFO)” order. n Like customers standing in a check-out line in a store, the first customer in is the first customer served.
65 Enqueue and Dequeue * Primary queue operations: Enqueue and Dequeue * Like check-out lines in a store, a queue has a front and a rear. * Enqueue – insert an element at the rear of the queue * Dequeue – remove an element from the front of the queue Remove (Dequeue) front rear Insert (Enqueue)
66 Implementation of Queue * Just as stacks can be implemented as arrays or linked lists, so with queues. * Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks
67 Queue ADT class Queue { public: Queue(); Queue(const Queue& queue); ~Queue(); ‘physical’ constructor/destructor bool empty(); double front(); // optional: front inspection (no queue change) void enqueue(double x); ‘logical’ constructor/destructor double dequeue(); // optional void display(void); bool full(); private: … };
68 Using Queue int main(void) { Queue queue; cout << "Enqueue 5 items. " << endl; for (int x = 0; x < 5; x++) queue. enqueue(x); cout << "Now attempting to enqueue again. . . " << endl; queue. enqueue(5); queue. print(); double value; value=queue. dequeue(); cout << "Retrieved element = " << value << endl; queue. print(); queue. enqueue(7); queue. print(); return 0; }
69 Queue using linked lists Struct Node { double data; Node* next; } class Queue { public: Queue(); Queue(const Queue& queue); ~Queue(); bool empty(); void enqueue(double x); double dequeue(); // bool full(); // optional void print(void); private: Node* front; Node* rear; int counter; }; // pointer to front node // pointer to last node // number of elements
70 Implementation of some online member functions … class Queue { public: Queue() { // constructor front = rear = NULL; counter = 0; } ~Queue() { // destructor double value; while (!empty()) dequeue(value); } bool empty() { if (counter) return false; else return true; } void enqueue(double x); double dequeue(); // bool full() {return false; }; void print(void); private: Node* front; Node* rear; int counter; }; // pointer to front node // pointer to last node // number of elements, not compulsary
71 Enqueue (add. End) void Queue: : enqueue(double x) { Node* new. Node = new Node; new. Node->data = x; new. Node->next = NULL; if (empty()) { front = new. Node; } else { rear->next = new. Node; } rear 5 8 rear = new. Node; counter++; } 8 5 new. Node
72 Dequeue (delete. Head) double Queue: : dequeue() { double x; if (empty()) { cout << "Error: the queue is empty. " << endl; exit(1); // return false; } else { x = front->data; Node* next. Node = front->next; delete front; front = next. Node; front counter--; } 3 8 5 return x; } front 8 5
73 Printing all the elements void Queue: : print() { cout << "front -->"; Node* curr. Node = front; for (int i = 0; i < counter; i++) { if (i == 0) cout << "t"; else cout << "tt"; cout << curr. Node->data; if (i != counter - 1) cout << endl; else cout << "t<-- rear" << endl; curr. Node = curr. Node->next; } }
74 Queue using Arrays * There are several different algorithms to implement Enqueue and Dequeue * Naïve way n When enqueuing, the front index is always fixed and the rear index moves forward in the array. rear 3 3 front Enqueue(3) rear 6 Enqueue(6) 3 6 9 front Enqueue(9)
75 * Naïve n 6 way (cont’d) When dequeuing, the front index is fixed, and the element at the front the queue is removed. Move all the elements after it by one position. (Inefficient!!!) rear 9 9 front Dequeue() rear = -1 front Dequeue()
76 *A better way When enqueued, the rear index moves forward. n When dequeued, the front index also moves forward by one element n (front) XXXXOOOOO OXXXXOOOO OOXXXXXOO OOOOXXXXX (rear) (after 1 dequeue, and 1 enqueue) (after another dequeue, and 2 enqueues) (after 2 more dequeues, and 2 enqueues) The problem here is that the rear index cannot move beyond the last element in the array.
77 Using Circular Arrays * Using a circular array * When an element moves past the end of a circular array, it wraps around to the beginning, e. g. n OOOOO 7963 4 OOOO 7963 (after Enqueue(4)) * How to detect an empty or full queue, using a circular array algorithm? n Use a counter of the number of elements in the queue.
78 class Queue { public: Queue(int size = 10); Queue(const Queue& queue); ~Queue() { delete[] values; } bool empty(void); void enqueue(double x); double dequeue(); bool full(); void print(void); // or bool enqueue(); full() is not essential, can be embedded private: int front; int rear; int counter; int max. Size; double* values; }; // constructor // not necessary! // destructor // front index // rear index // number of elements // size of array queue // element array
79 * Attributes of Queue front/rear: front/rear index n counter: number of elements in the queue n max. Size: capacity of the queue n values: point to an array which stores elements of the queue n * Operations of Queue n n n empty: return true if queue is empty, return false otherwise full: return true if queue is full, return false otherwise enqueue: add an element to the rear of queue dequeue: delete the element at the front of queue print: print all the data
80 Queue constructor * Queue(int size = 10) Allocate a queue array of size. By default, size = 10. n front is set to 0, pointing to the first element of the array n rear is set to -1. The queue is empty initially. n Queue: : Queue(int size /* = 10 */) { values = new double[size]; max. Size = size; front = 0; rear = -1; counter = 0; }
81 Empty & Full * Since we keep track of the number of elements that are actually in the queue: counter, it is easy to check if the queue is empty or full. bool Queue: : empty() { if (counter==0) return else return } bool Queue: : full() { if (counter < max. Size) else } true; false; return true;
82 Enqueue Or ‘bool’ if you want void Queue: : enqueue(double x) { if (full()) { cout << "Error: the queue is full. " << endl; exit(1); // return false; } else { // calculate the new rear position (circular) rear = (rear + 1) % max. Size; // insert new item values[rear] = x; // update counter++; // return true; } }
83 Dequeue double Queue: : dequeue() { double x; if (empty()) { cout << "Error: the queue is empty. " << endl; exit(1); // return false; } else { // retrieve the front item x = values[front]; // move front = (front + 1) % max. Size; // update counter--; // return true; } return x; }
84 Printing the elements void Queue: : print() { cout << "front -->"; for (int i = 0; i < counter; i++) { if (i == 0) cout << "t"; else cout << "tt"; cout << values[(front + i) % max. Size]; if (i != counter - 1) cout << endl; else cout << "t<-- rear" << endl; } }
85 Using Queue int main(void) { Queue queue; cout << "Enqueue 5 items. " << endl; for (int x = 0; x < 5; x++) queue. enqueue(x); cout << "Now attempting to enqueue again. . . " << endl; queue. enqueue(5); queue. print(); double value; value=queue. dequeue(); cout << "Retrieved element = " << value << endl; queue. print(); queue. enqueue(7); queue. print(); return 0; }
86 Results based on array based on linked list Queue implemented using linked list will be never full!
87 Queue applications * When jobs are sent to a printer, in order of arrival, a queue. * Customers at ticket counters …
88 Queue application: Counting Sort * Assume N integers are to be sorted, each is in the range 1 to M. * Define an array B[1. . M], initialize all to 0 * Scan through the input list A[i], insert A[i] into B[A[i]] O(M) O(N) Scan B once, read out the nonzero integers O(M) Total time: O(M + N) n if M is O(N), then total time is O(N) n Can be bad if range is very big, e. g. M=O(N 2) N=7, M = 9, * Want to sort 8 1 9 1 2 3 5 2 6 3 5 6 Output: 1 2 3 5 6 8 9
89 * What if we have duplicates? * B is an array of pointers. * Each position in the array has 2 pointers: head and tail. Tail points to the end of a linked list, and head points to the beginning. * A[j] is inserted at the end of the list B[A[j]] * Again, Array B is sequentially traversed and each nonempty list is printed out. * Time: O(M + N)
90 M = 9, Wish to sort 8 5 1 5 9 5 6 2 7 1 2 5 6 7 5 5 Output: 1 2 5 5 5 6 7 8 9
91 Queue Application: Bin (or Bucket) Sort (generalization of counting sort) * The nodes are placed into bins, each bin contains nodes with the same score * Then we combine the bins to create a sorted chain * Note that it does not change the relative order of nodes that have the same score, the so-called stable sort. COMP 152 91
92 Number of Steps in binsort *m integers, r array size * Sort n m integers For example, sort m=1, 000 integers in the range of 0 to r=106 * We use binsort with range r: Initialization of array takes r steps; n putting items into the array takes m steps; n and reading out from the array takes another m+r steps n The total sort complexity is then (2 m + 2 r) , which can be much larger than m if r is large n COMP 152 92
93 Queue application: Radix Sort * Extra information: every integer can be represented by at most k digits d 1 d 2…dk where di are digits in base r n d 1: most significant digit n dk: least significant digit n
94 Radix Sort * Algorithm sort by the least significant digit first (counting sort) => Numbers with the same digit go to same bin n reorder all the numbers: the numbers in bin 0 precede the numbers in bin 1, which precede the numbers in bin 2, and so on n sort by the next least significant digit n continue this process until the numbers have been sorted on all k digits n
95 Radix Sort * Least-significant-digit-first Example: 275, 087, 426, 061, 509, 170, 677, 503 170 061 503 275 426 087 677 509
96 170 061 503 275 426 087 677 509 503 509 426 061 170 275 677 087 061 087 170 275 426 503 509 677
97 Radix Sort * Does it work? * Clearly, if the most significant digit of a and b are different and a < b, then finally a comes before b * If the most significant digit of a and b are the same, and the second most significant digit of b is less than that of a, then b comes before a.
98 Radix(A[], n, d) // an array of queues as bins Queue Q[9](); k=0; D=1; while (k <= d) get k-th digit t of A[i] enqueue(A[i], Q[t]) k=k+1; // out put Q p=0; j=0; While (p<=9) While (not empty Q[j]) A[j]=dequeue(Q(p)); j=j+1; p=p+1;
99 Finding digit Di From Key *We want to find the ith digit in our key A A = Dd. Dd-1. . Di…D 2 D 1 Get Digit Let D = 10 i Then Di = (A mod D) div (D/10) Example. A=30487 we want D 3 i =3, D = 1000 A mod 1000 = 487 div (D/10) 487 div (100) = 4 (first) (second) result! mod and div are integer operators COMP 152 99
100 A=input array, n=|numbers to be sorted|, d=# of digits, k=the digit being sorted, j=array index // base 10 // FIFO // d times of counting sort // scan A[i], put into correct slot // re-order back to original array
101 Radix Sort Increasing the base r decreases the number of passes * Running time * k passes over the numbers (i. e. k counting sorts, with range being 0. . r) n each pass takes 2 N n total: O(2 Nk)=O(Nk) n r and k are constants: O(N) n * Note: radix sort is not based on comparisons; the values are used as array indices n If all N input values are distinct, then k = (log N) (e. g. , in binary digits, to represent 8 different numbers, we need at least 3 digits). Thus the running time of Radix Sort also become (N log N). n
102 Radix Sort Example 2: sorting cards 2 digits for each card: d 1 d 2 n d 1 = : base 4 n 1 n d 2 = A, 2, 3, . . . J, Q, K: base 13 1 A n 2 3 . . . J Q K 2 2 5 K
- Slides: 102