SlideShare a Scribd company logo
IS 2610: Data Structures Priority Queue, Heapsort, Searching March 15, 2004
Priority Queues Applications require that we process records with keys in order Collect records Process one with largest key Maybe collect more records Applications Simulation systems (event times) Scheduling (priorities) Priority queue : A data structure of items with keys that support two basic operations: Insert a new item; Delete the item with the largest key
Priority Queue Build and maintain the following operations Construct the queue Insert a new item Delete the maximum item Change the priority of an arbitrary specified item Delete an arbitrary specified item Join two priority queues into one large one
Priority Queue: Elementary operations void PQinit(int); int PQempty(); void PQinsert(Item); Item PQdelmax(); #include <stdlib.h> #include &quot;Item.h&quot;  static Item *pq;  static int N; void PQinit(int maxN) { pq = malloc(maxN*sizeof(Item)); N = 0; } int PQempty()  { return N == 0; } void PQinsert(Item v) { pq[N++] = v; } Item PQdelmax() { int j, max = 0; for (j = 1; j < N; j++) if (less(pq[max], pq[j])) max = j; exch(pq[max], pq[N-1]);  return pq[--N]; }
Heap Data Structure Def 9.2 A tree is  heap-ordered  if the key in each node is larger than or equal to the keys in all of that node’s children (if any). Equivalently, the key in each node of a heap-ordered tree is smaller than or equal to the key in that node’s parent (if any) Property 9.1 No node in a heap-ordered tree has a key larger than the key at the root. Heap can efficiently support the basic priority-queue operations
Heap Data Structure Def 9.2  A  heap  is a set of nodes with keys arranged in a complete heap-ordered binary tree, [represented as an array]. A complete tree allows using a compact array representation The parent of node  i  is in position   i /2  The two children of the node  i  are in positions  2 i  and  2 i  + 1 . Disadvantage of using arrays?
Algorithms on Heaps Heapifying Modify heap to violate the heap condition Add new element Change the priority Restructure heap to restore the heap condition Priority of child becomes greater A E T A I M G R N O X S P
Bottom-up heapify First exchange T and R Then exchange T and S fixUp(Item a[], int k) { while (k > 1 && less(a[k/2], a[k])) { exch(a[k], a[k/2]); k = k/2; } } A E R A I M G S N O X T P
Top-down heapify Exchange with the larger child Priority of parent becomes smaller A E R A I M G S N P O T X fixDown(Item a[], int k, int N) { int j; while (2*k <= N) { j = 2*k; if (j < N && less(a[j], a[j+1])) j++; if (!less(a[k], a[j])) break; exch(a[k], a[j]); k = j; } } I M N O X P
Heap-based priority Queue Property 9.2 Insert requires no more than  lg n one comparison at each level Delete maximum requires no more than  2   lg n  two comparisons at each level #include <stdlib.h> #include &quot;Item.h&quot;  static Item *pq;  static int N; void PQinit(int maxN) { pq = malloc((maxN+1)*sizeof(Item)); N = 0; } int PQempty()  { return N == 0; } void PQinsert(Item v) { pq[++N] = v; fixUp(pq, N); } Item PQdelmax() {  exch(pq[1], pq[N]);  fixDown(pq, 1, N-1);  return pq[N--];  }
Sorting with a priority Queue Use  PQinsert  to put all the elements on the priority queue Use  PQdelmax  to remove them in decreasing order Heap construction takes <  n lg n void PQsort(Item a[], int l, int r) { int k; PQinit(); for (k = l; k <= r; k++) PQinsert(a[k]); for (k = r; k >= l; k--) a[k] = PQdelmax(); }
Bottom-up Heap Right to left Bottom-up G E X A M P R T I A S O L E N T A X M I P void heapsort(Item a[], int l, int r) { int k, N = r-l+1; Item* pq = a + l -1; for (k = N/2; k >= 1; k--)  fixDown(pq, k, N); while (N > 1)  { exch(pq[1], pq[N]);  fixDown(pq, 1, --N); } }
Bottom-up Heap Note: most nodes are at the bottom Bottom up heap construction takes linear time G E T A M I R X P A S O L E N G E S A M I R T O A X P L E N
Radix Sort Decompose keys into pieces Binary numbers are sequence of bytes Strings are sequence of characters Decimal number are sequence of digits Radix sort:  Sorting methods built on processing numbers one piece at a time Treat keys as numbers represented in base R and work with individual digits R = 10 in many applications where keys are 5- to 10-digit decimal numbers Example: postal code, telephone numbers, SSN
Radix Sort If keys are integers Can use R = 2, or a multiple of 2 If keys are strings of characters Can use R = 128 or 256 (aligns with a byte) Radix sort is based on the abstract operation  Extract the  i th  digit from a key Two approaches to radix sort Most-significant-digit (MSD) radix sorts (left-to-right) Least-significant-digit (LSD) radix sorts (right-to-left)
Bits, Bytes and Word The key to understanding Radix sorts is to recognize that Computers generally are built to process bits in groups called machine words (groups of bytes) Sort keys are commonly organized as byte sequences Small byte sequence can also serve as array indices or machine addresses Hence an abstraction can be used
Bits, Bytes and Word Def: A  byte  is a fixed-length sequence of bits; a  string  is a variable-length sequence of bytes; a  word  is a fixed-length sequence of bytes digit(A, 2)?? #define bitsword 32 #define bitsbyte 8 #define bytesword 4 #define R (1 << bitsbyte) #define digit(A, B) (((A) >> (bitsword-((B)+1)*bitsbyte)) & (R-1)) // Another possibility #define digit(A, B) A[B]
Binary Quicksort Partition a file based on leading bits Sort the sub-files recursively quicksortB(int a[], int l, int r, int w) { int i = l, j = r; if (r <= l || w > bitsword) return; while (j != i) {  while (digit(a[i], w) == 0 && (i < j)) i++; while (digit(a[j], w) == 1 && (j > i)) j--; exch(a[i], a[j]); } if (digit(a[r], w) == 0) j++; quicksortB(a, l, j-1, w+1); quicksortB(a, j, r, w+1); } void sort(Item a[], int l, int r) {  quicksortB(a, l, r, 0); }
Binary Quicksort 0 01 1 11 0 11 1 00 1 01 0 10 001 010 011 100 101 111 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1
MSD Radix Sort Binary Quicksort is a MSD with R = 2; For general R, we will partition the array into R different bins/buckets
LSD Radix Sort Examine bytes Right to left now  so b  c ab ace for  no b  w ab ago tip  ca b  t ag and
Symbol Table A  symbol table  is a data structure of items with keys that supports two basic operations:  insert  a new item, and  return  an item with a given key Examples: Account information in banks Airline reservations
Symbol Table ADT Key operations Insert a new item Search for an item with a given key Delete a specified item Select the k th  smallest item Sort the symbol table Join two symbol tables void STinit(int); int STcount(); void STinsert(Item); Item STsearch(Key); void STdelete(Item); Item STselect(int); void STsort(void (*visit)(Item));
Key-indexed ST Simplest search algorithm is based on storing items in an array, indexed by the keys static Item *st; static int M = maxKey; void STinit(int maxN)  { int i; st = malloc((M+1)*sizeof(Item)); for (i = 0; i <= M; i++) st[i] = NULLitem;  } int STcount()  { int i, N = 0; for (i = 0; i < M; i++)  if (st[i] != NULLitem) N++; return N; } void STinsert(Item item) { st[key(item)] = item; } Item STsearch(Key v) { return st[v]; } void STdelete(Item item) { st[key(item)] = NULLitem; }   Item STselect(int k) { int i; for (i = 0; i < M; i++) if (st[i] != NULLitem)  if (k-- == 0) return st[i];  } void STsort(void (*visit)(Item)) { int i; for (i = 0; i < M; i++) if (st[i] != NULLitem) visit(st[i]); }
Sequential Search based ST When a new item is inserted, we put it into the array by moving the larger elements over one position (as in insertion sort) To search for an element Look through the array sequentially If we encounter a key larger than the search key – we report an error
Binary Search Divide and conquer methodology Divide the items into two parts Determine which part the search key belongs to and concentrate on that part Keep the items sorted Use the indices to delimit the part searched. Item search(int l, int r, Key v) { int m = (l+r)/2; if (l > r) return NULLitem; if eq(v, key(st[m])) return st[m]; if (l == r) return NULLitem; if less(v, key(st[m]))  return search(l, m-1, v); else return search(m+1, r, v); } Item STsearch(Key v) { return search(0, N-1, v); }

More Related Content

What's hot (20)

PPT
Queue implementation
Rajendran
 
PPTX
My lectures circular queue
Senthil Kumar
 
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
PPTX
Queue
Bhavesh Parmar
 
PPTX
Detalied information of queue
Smit Parikh
 
PPTX
Insertion & Selection Sort - using Priority Queues
Priyanka Rana
 
PPTX
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
PDF
Array linear data_structure_2 (1)
eShikshak
 
DOCX
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
PPTX
sorting algorithm graphical method
Shantanu Mishra
 
PPT
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
PPTX
Queue
Krishanu Ghosh
 
PPTX
queue & its applications
somendra kumar
 
PPT
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
PDF
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPT
Arrays
archikabhatia
 
PDF
Queue
Zaid Shabbir
 
Queue implementation
Rajendran
 
My lectures circular queue
Senthil Kumar
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Detalied information of queue
Smit Parikh
 
Insertion & Selection Sort - using Priority Queues
Priyanka Rana
 
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
Array linear data_structure_2 (1)
eShikshak
 
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
sorting algorithm graphical method
Shantanu Mishra
 
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
queue & its applications
somendra kumar
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 

Viewers also liked (20)

PPT
Building blocks for a tangible marketing plan
Flourish Promotional Marketing
 
PDF
Comunicato attività polisportiva - Pallavolo N°10 del 14 dicembre
Giuliano Ganassi
 
PDF
WL TelPay Tutorial ENGLISH
Claudia Baha
 
PPTX
Riley james johnstone presentation
warjohnstone
 
PDF
Õppematerjalide loomine LeMill keskkonnas
Hans Põldoja
 
PPS
las herramientas
Sara Hernandez
 
PPTX
Info2 sec 2_-_data__information
saltashict
 
PDF
Some real life data analysis on stationary and non-stationary Time Series
Ajay Bidyarthy
 
PPTX
justatemplate_test
Scott Peterson
 
PPT
Presentatie 3 februari Buisleidingenstraat
marusjkalestrade
 
PPT
Clout januari 2014
BenjaminJocic
 
PDF
New account
Akkarachat Chaisena
 
PDF
Latest Items from Europe 2016
Flourish Promotional Marketing
 
PPT
presentación de la imagen fija
kitopa
 
DOCX
Buen uso de la red y el internet
CamiloRodriguez123
 
PDF
Oltre Tata: lean startup all'italiana
Francesco Trucchia
 
PPTX
Using Practice Fusion for PQRS EHR Reporting in 2014
Practice Fusion
 
PDF
Solar Chimney
Ahmed Amin
 
PPTX
Solar chimney
Suchit Gupta
 
PDF
παραβολή
Kozalakis
 
Building blocks for a tangible marketing plan
Flourish Promotional Marketing
 
Comunicato attività polisportiva - Pallavolo N°10 del 14 dicembre
Giuliano Ganassi
 
WL TelPay Tutorial ENGLISH
Claudia Baha
 
Riley james johnstone presentation
warjohnstone
 
Õppematerjalide loomine LeMill keskkonnas
Hans Põldoja
 
las herramientas
Sara Hernandez
 
Info2 sec 2_-_data__information
saltashict
 
Some real life data analysis on stationary and non-stationary Time Series
Ajay Bidyarthy
 
justatemplate_test
Scott Peterson
 
Presentatie 3 februari Buisleidingenstraat
marusjkalestrade
 
Clout januari 2014
BenjaminJocic
 
New account
Akkarachat Chaisena
 
Latest Items from Europe 2016
Flourish Promotional Marketing
 
presentación de la imagen fija
kitopa
 
Buen uso de la red y el internet
CamiloRodriguez123
 
Oltre Tata: lean startup all'italiana
Francesco Trucchia
 
Using Practice Fusion for PQRS EHR Reporting in 2014
Practice Fusion
 
Solar Chimney
Ahmed Amin
 
Solar chimney
Suchit Gupta
 
παραβολή
Kozalakis
 
Ad

Similar to Algorithm (20)

PPTX
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
PPT
Fundamentals of data structures
Niraj Agarwal
 
PPT
02 Arrays And Memory Mapping
Qundeel
 
PPT
chapter7.ppt
Preetiverma538521
 
PPT
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
PPT
chapter7 Sorting.ppt
Nielmagahod
 
PPT
Sorting & Linked Lists
J.T.A.JONES
 
PPT
Sorting and Searching in Data Structures.ppt
Vivekananda Gn
 
PPTX
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
PPTX
Advanced data structure
Shakil Ahmed
 
PDF
Generics and data structures in Java for an introductory programming course
ZiyanMaraikar1
 
PPTX
Data structure
Arvind Kumar
 
PPTX
Address calculation-sort
Vasim Pathan
 
PPT
Chapter 5 ds
Hanif Durad
 
PPTX
هياكلبيانات
Rafal Edward
 
PPTX
data structures with algorithms vtu 2023 notes.pptx
hemanthkumar40680
 
PPTX
data structures and algorithms Unit 3
infanciaj
 
PPT
Arrays in C++
Janpreet Singh
 
PDF
I want help in the following C++ programming task. Please do coding .pdf
bermanbeancolungak45
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
Fundamentals of data structures
Niraj Agarwal
 
02 Arrays And Memory Mapping
Qundeel
 
chapter7.ppt
Preetiverma538521
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
chapter7 Sorting.ppt
Nielmagahod
 
Sorting & Linked Lists
J.T.A.JONES
 
Sorting and Searching in Data Structures.ppt
Vivekananda Gn
 
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
Advanced data structure
Shakil Ahmed
 
Generics and data structures in Java for an introductory programming course
ZiyanMaraikar1
 
Data structure
Arvind Kumar
 
Address calculation-sort
Vasim Pathan
 
Chapter 5 ds
Hanif Durad
 
هياكلبيانات
Rafal Edward
 
data structures with algorithms vtu 2023 notes.pptx
hemanthkumar40680
 
data structures and algorithms Unit 3
infanciaj
 
Arrays in C++
Janpreet Singh
 
I want help in the following C++ programming task. Please do coding .pdf
bermanbeancolungak45
 
Ad

Recently uploaded (20)

PPTX
Joshua Through the Lens of Jesus: Part 3 - Ch.7-9
Vintage Church
 
PPTX
I don't know why scribd is making me upload pets
yangjessica629
 
PPTX
How the Universe Works - Philosophical Idealism.pptx
Sanford Schoolfield
 
PPTX
VILASINI RADHA DD CHATUSLOKI BAGVATAM (3).pptx
neelam108thapa
 
PDF
Printable French Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Haitian Creole Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PPTX
10.pptf if you want to prente it in school assbly or any spritaul placwe it i...
dhruvnavdiya1908
 
PPTX
HOLY-EUCHARIST lkjflsdjfladsjf;ladskjfl;adskjf
reinelmercado800
 
PPT
Diwali Festival 2025 Bring Home Laxmi Ganesh Marble Idol
SAI SHRADHA MOORTI ART
 
PDF
Printable Hawaiian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Konkani Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Galician Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
The Odd Women of Seabreeze <3<3<3<3<3<3<3
ssuser83613b
 
PDF
Printable Ilocano Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Printable Faroese Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PDF
Gautama Buddha or Sugata Buddha? Clarifying Vishnu's Avatars.pdf
GovindNath3
 
PPTX
Life_of_Saint_Agnes_Presentationnnn.pptx
ChristerPastranaVill
 
PDF
NOTICE OF INFORMATION JC-DKR-07112025-02.pdf
trungvo92
 
PDF
Printable Kinyarwanda Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
PPTX
Sabbath School Lesson 2, 3rd Quarter 2025.pptx
DavidSyahputra4
 
Joshua Through the Lens of Jesus: Part 3 - Ch.7-9
Vintage Church
 
I don't know why scribd is making me upload pets
yangjessica629
 
How the Universe Works - Philosophical Idealism.pptx
Sanford Schoolfield
 
VILASINI RADHA DD CHATUSLOKI BAGVATAM (3).pptx
neelam108thapa
 
Printable French Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Haitian Creole Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
10.pptf if you want to prente it in school assbly or any spritaul placwe it i...
dhruvnavdiya1908
 
HOLY-EUCHARIST lkjflsdjfladsjf;ladskjfl;adskjf
reinelmercado800
 
Diwali Festival 2025 Bring Home Laxmi Ganesh Marble Idol
SAI SHRADHA MOORTI ART
 
Printable Hawaiian Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Konkani Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Galician Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
The Odd Women of Seabreeze <3<3<3<3<3<3<3
ssuser83613b
 
Printable Ilocano Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Faroese Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Gautama Buddha or Sugata Buddha? Clarifying Vishnu's Avatars.pdf
GovindNath3
 
Life_of_Saint_Agnes_Presentationnnn.pptx
ChristerPastranaVill
 
NOTICE OF INFORMATION JC-DKR-07112025-02.pdf
trungvo92
 
Printable Kinyarwanda Gospel Tract - Do Not Fear Death.pdf
Filipino Tracts and Literature Society Inc.
 
Sabbath School Lesson 2, 3rd Quarter 2025.pptx
DavidSyahputra4
 

Algorithm

  • 1. IS 2610: Data Structures Priority Queue, Heapsort, Searching March 15, 2004
  • 2. Priority Queues Applications require that we process records with keys in order Collect records Process one with largest key Maybe collect more records Applications Simulation systems (event times) Scheduling (priorities) Priority queue : A data structure of items with keys that support two basic operations: Insert a new item; Delete the item with the largest key
  • 3. Priority Queue Build and maintain the following operations Construct the queue Insert a new item Delete the maximum item Change the priority of an arbitrary specified item Delete an arbitrary specified item Join two priority queues into one large one
  • 4. Priority Queue: Elementary operations void PQinit(int); int PQempty(); void PQinsert(Item); Item PQdelmax(); #include <stdlib.h> #include &quot;Item.h&quot; static Item *pq; static int N; void PQinit(int maxN) { pq = malloc(maxN*sizeof(Item)); N = 0; } int PQempty() { return N == 0; } void PQinsert(Item v) { pq[N++] = v; } Item PQdelmax() { int j, max = 0; for (j = 1; j < N; j++) if (less(pq[max], pq[j])) max = j; exch(pq[max], pq[N-1]); return pq[--N]; }
  • 5. Heap Data Structure Def 9.2 A tree is heap-ordered if the key in each node is larger than or equal to the keys in all of that node’s children (if any). Equivalently, the key in each node of a heap-ordered tree is smaller than or equal to the key in that node’s parent (if any) Property 9.1 No node in a heap-ordered tree has a key larger than the key at the root. Heap can efficiently support the basic priority-queue operations
  • 6. Heap Data Structure Def 9.2 A heap is a set of nodes with keys arranged in a complete heap-ordered binary tree, [represented as an array]. A complete tree allows using a compact array representation The parent of node i is in position  i /2  The two children of the node i are in positions 2 i and 2 i + 1 . Disadvantage of using arrays?
  • 7. Algorithms on Heaps Heapifying Modify heap to violate the heap condition Add new element Change the priority Restructure heap to restore the heap condition Priority of child becomes greater A E T A I M G R N O X S P
  • 8. Bottom-up heapify First exchange T and R Then exchange T and S fixUp(Item a[], int k) { while (k > 1 && less(a[k/2], a[k])) { exch(a[k], a[k/2]); k = k/2; } } A E R A I M G S N O X T P
  • 9. Top-down heapify Exchange with the larger child Priority of parent becomes smaller A E R A I M G S N P O T X fixDown(Item a[], int k, int N) { int j; while (2*k <= N) { j = 2*k; if (j < N && less(a[j], a[j+1])) j++; if (!less(a[k], a[j])) break; exch(a[k], a[j]); k = j; } } I M N O X P
  • 10. Heap-based priority Queue Property 9.2 Insert requires no more than lg n one comparison at each level Delete maximum requires no more than 2 lg n two comparisons at each level #include <stdlib.h> #include &quot;Item.h&quot; static Item *pq; static int N; void PQinit(int maxN) { pq = malloc((maxN+1)*sizeof(Item)); N = 0; } int PQempty() { return N == 0; } void PQinsert(Item v) { pq[++N] = v; fixUp(pq, N); } Item PQdelmax() { exch(pq[1], pq[N]); fixDown(pq, 1, N-1); return pq[N--]; }
  • 11. Sorting with a priority Queue Use PQinsert to put all the elements on the priority queue Use PQdelmax to remove them in decreasing order Heap construction takes < n lg n void PQsort(Item a[], int l, int r) { int k; PQinit(); for (k = l; k <= r; k++) PQinsert(a[k]); for (k = r; k >= l; k--) a[k] = PQdelmax(); }
  • 12. Bottom-up Heap Right to left Bottom-up G E X A M P R T I A S O L E N T A X M I P void heapsort(Item a[], int l, int r) { int k, N = r-l+1; Item* pq = a + l -1; for (k = N/2; k >= 1; k--) fixDown(pq, k, N); while (N > 1) { exch(pq[1], pq[N]); fixDown(pq, 1, --N); } }
  • 13. Bottom-up Heap Note: most nodes are at the bottom Bottom up heap construction takes linear time G E T A M I R X P A S O L E N G E S A M I R T O A X P L E N
  • 14. Radix Sort Decompose keys into pieces Binary numbers are sequence of bytes Strings are sequence of characters Decimal number are sequence of digits Radix sort: Sorting methods built on processing numbers one piece at a time Treat keys as numbers represented in base R and work with individual digits R = 10 in many applications where keys are 5- to 10-digit decimal numbers Example: postal code, telephone numbers, SSN
  • 15. Radix Sort If keys are integers Can use R = 2, or a multiple of 2 If keys are strings of characters Can use R = 128 or 256 (aligns with a byte) Radix sort is based on the abstract operation Extract the i th digit from a key Two approaches to radix sort Most-significant-digit (MSD) radix sorts (left-to-right) Least-significant-digit (LSD) radix sorts (right-to-left)
  • 16. Bits, Bytes and Word The key to understanding Radix sorts is to recognize that Computers generally are built to process bits in groups called machine words (groups of bytes) Sort keys are commonly organized as byte sequences Small byte sequence can also serve as array indices or machine addresses Hence an abstraction can be used
  • 17. Bits, Bytes and Word Def: A byte is a fixed-length sequence of bits; a string is a variable-length sequence of bytes; a word is a fixed-length sequence of bytes digit(A, 2)?? #define bitsword 32 #define bitsbyte 8 #define bytesword 4 #define R (1 << bitsbyte) #define digit(A, B) (((A) >> (bitsword-((B)+1)*bitsbyte)) & (R-1)) // Another possibility #define digit(A, B) A[B]
  • 18. Binary Quicksort Partition a file based on leading bits Sort the sub-files recursively quicksortB(int a[], int l, int r, int w) { int i = l, j = r; if (r <= l || w > bitsword) return; while (j != i) { while (digit(a[i], w) == 0 && (i < j)) i++; while (digit(a[j], w) == 1 && (j > i)) j--; exch(a[i], a[j]); } if (digit(a[r], w) == 0) j++; quicksortB(a, l, j-1, w+1); quicksortB(a, j, r, w+1); } void sort(Item a[], int l, int r) { quicksortB(a, l, r, 0); }
  • 19. Binary Quicksort 0 01 1 11 0 11 1 00 1 01 0 10 001 010 011 100 101 111 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1
  • 20. MSD Radix Sort Binary Quicksort is a MSD with R = 2; For general R, we will partition the array into R different bins/buckets
  • 21. LSD Radix Sort Examine bytes Right to left now so b c ab ace for no b w ab ago tip ca b t ag and
  • 22. Symbol Table A symbol table is a data structure of items with keys that supports two basic operations: insert a new item, and return an item with a given key Examples: Account information in banks Airline reservations
  • 23. Symbol Table ADT Key operations Insert a new item Search for an item with a given key Delete a specified item Select the k th smallest item Sort the symbol table Join two symbol tables void STinit(int); int STcount(); void STinsert(Item); Item STsearch(Key); void STdelete(Item); Item STselect(int); void STsort(void (*visit)(Item));
  • 24. Key-indexed ST Simplest search algorithm is based on storing items in an array, indexed by the keys static Item *st; static int M = maxKey; void STinit(int maxN) { int i; st = malloc((M+1)*sizeof(Item)); for (i = 0; i <= M; i++) st[i] = NULLitem; } int STcount() { int i, N = 0; for (i = 0; i < M; i++) if (st[i] != NULLitem) N++; return N; } void STinsert(Item item) { st[key(item)] = item; } Item STsearch(Key v) { return st[v]; } void STdelete(Item item) { st[key(item)] = NULLitem; } Item STselect(int k) { int i; for (i = 0; i < M; i++) if (st[i] != NULLitem) if (k-- == 0) return st[i]; } void STsort(void (*visit)(Item)) { int i; for (i = 0; i < M; i++) if (st[i] != NULLitem) visit(st[i]); }
  • 25. Sequential Search based ST When a new item is inserted, we put it into the array by moving the larger elements over one position (as in insertion sort) To search for an element Look through the array sequentially If we encounter a key larger than the search key – we report an error
  • 26. Binary Search Divide and conquer methodology Divide the items into two parts Determine which part the search key belongs to and concentrate on that part Keep the items sorted Use the indices to delimit the part searched. Item search(int l, int r, Key v) { int m = (l+r)/2; if (l > r) return NULLitem; if eq(v, key(st[m])) return st[m]; if (l == r) return NULLitem; if less(v, key(st[m])) return search(l, m-1, v); else return search(m+1, r, v); } Item STsearch(Key v) { return search(0, N-1, v); }