SlideShare a Scribd company logo
Class No.33  Data Structures http://ecomputernotes.com
Searching an Array: Binary Search Binary search is like looking up a phone number or a word in the dictionary Start in middle of book If name you're looking for comes before names on page, look in first half Otherwise, look in second half http://ecomputernotes.com
Binary Search If ( value == middle element )  value is found  else if ( value < middle element )  search left-half of list with the same method  else  search right-half of list with the same method http://ecomputernotes.com
Case 1:  val == a[mid] val = 10 low = 0, high = 8 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: low high Binary Search mid mid = (0 + 8) / 2 = 4 10 http://ecomputernotes.com
Case 2:  val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4  Binary Search -- Example 2 5 7 9 10 1 13 17 19 27 1 2 3 4 5 6 7 0 8 a: mid low high new low new low = mid+1 = 5 13 17 19 27 http://ecomputernotes.com
Case 3:  val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4  Binary Search -- Example 3 10 13 17 19 5 7 9 1 27 1 2 3 4 5 6 7 0 8 a: mid low high new high new high = mid-1 = 3 5 7 9 1 http://ecomputernotes.com
val = 7 Binary Search -- Example 3 (cont) 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a:
Binary Search – C++ Code int isPresent(int *arr, int val, int N) { int low = 0; int high = N - 1; int mid; while ( low <= high ){ mid = ( low + high )/2; if (arr[mid]== val) return 1; // found! else if (arr[mid] < val) low = mid + 1; else high = mid - 1; } return 0; // not found }
Binary Search: binary tree The search divides a list into two small sub-lists till a sub-list is no more divisible. First half First half An entire sorted list First half Second half Second half http://ecomputernotes.com
Binary Search Efficiency After 1 bisection N/2 items After 2 bisections N/4 = N/2 2  items   . . .  After  i  bisections N/2 i  = 1 item i  = log 2  N http://ecomputernotes.com
Implementation 3: linked list TableNodes are again stored consecutively (unsorted or sorted) insert : add to front; (1 or n for a sorted list ) find : search through potentially all the keys, one at a time; ( n   for unsorted or for a sorted list remove : find, remove using pointer alterations; ( n ) key entry and so on http://ecomputernotes.com
Implementation 4: Skip List Overcome basic limitations of previous lists Search and update require linear time Fast Searching of Sorted Chain  Provide alternative to BST (binary search trees) and related tree structures. Balancing can be expensive. Relatively recent data structure: Bill Pugh proposed it in 1990. http://ecomputernotes.com
Skip List Representation Can do better than  n  comparisons to find element in chain of length  n 20 30 40 50 60 head tail http://ecomputernotes.com
Skip List Representation Example:  n/2 + 1  if we keep pointer to middle element 20 30 40 50 60 head tail http://ecomputernotes.com
Higher Level Chains For general n, level 0 chain includes all elements level 1 every other element, level 2 chain every fourth, etc. level  i , every  2 i   th element 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
Higher Level Chains Skip list contains a hierarchy of chains In general level  i  contains a subset of elements in level  i-1 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
Skip List: formally A skip list for a set  S  of distinct (key, element) items is a series of lists  S 0 ,  S 1  , … ,  S h  such that Each list  S i  contains the special keys  and   List  S 0  contains the keys of  S  in nondecreasing order  Each list is a subsequence of the previous one, i.e., S 0     S 1     …   S h List  S h  contains only the two special keys

More Related Content

PPTX
Doubly linked list
PPTX
Double link list
PPTX
Double Linked List (Algorithm)
PPTX
linked list
PPT
computer notes - Data Structures - 32
PPT
Computer notes - Binary Search
PPTX
Doubly Linked List
PPT
Unit ii(dsc++)
Doubly linked list
Double link list
Double Linked List (Algorithm)
linked list
computer notes - Data Structures - 32
Computer notes - Binary Search
Doubly Linked List
Unit ii(dsc++)

What's hot (20)

PDF
Doubly Link List
PPTX
Doubly Linked Lists
PPTX
PPTX
Linked list
PPT
Link list
PPTX
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
PPT
Singly link list
PPTX
Linked List
PPT
Circular linked list
PPT
Data Structure and Algorithms Linked List
PPSX
Data Structure (Circular Linked List)
PPT
Linked list
PDF
PPTX
Linked list
PPT
linked list
PDF
Circular linked list
PPTX
Deletion from single way linked list and search
PPT
linked list
PPTX
Ppt on Linked list,stack,queue
PPTX
Linked list
Doubly Link List
Doubly Linked Lists
Linked list
Link list
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
Singly link list
Linked List
Circular linked list
Data Structure and Algorithms Linked List
Data Structure (Circular Linked List)
Linked list
Linked list
linked list
Circular linked list
Deletion from single way linked list and search
linked list
Ppt on Linked list,stack,queue
Linked list
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 6
computer notes - Data Structures - 21
computer notes - Data Structures - 2
computer notes - Data Structures - 25
computer notes - Data Structures - 19
computer notes - Data Structures - 39
computer notes - Data Structures - 8
computer notes - Data Structures - 37
computer notes - Data Structures - 36
computer notes - Data Structures - 20
computer notes - Data Structures - 4
computer notes - Data Structures - 12
computer notes - Data Structures - 14
computer notes - Data Structures - 18
computer notes - Data Structures - 1
computer notes - Data Structures - 5
computer notes - Data Structures - 29
computer notes - Data Structures - 7
computer notes - Data Structures - 28
computer notes - Data Structures - 27
computer notes - Data Structures - 6
Ad

Similar to computer notes - Data Structures - 33 (20)

DOC
Linked List
PPTX
data structures and algorithms Unit 3
PPTX
Chapter3.pptx
PPTX
Data structure using c module 3
PPTX
DS - Unit 2 FINAL (2).pptx
PPTX
Data Structures_ Sorting & Searching
PPT
Advanced s and s algorithm.ppt
PPTX
Unit III Version I.pptx
PPT
Algorithm, Pseudocode and Flowcharting in C++
PPT
21-algorithms.ppt
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPTX
Mca ii dfs u-3 linklist,stack,queue
PDF
Stacks,queues,linked-list
PPT
16-sorting.ppt
PPT
21-algorithms (1).ppt
PPT
Chap10
PPTX
Searching and Sorting Algorithms in Data Structures
PPT
Data Structures by Maneesh Boddu
Linked List
data structures and algorithms Unit 3
Chapter3.pptx
Data structure using c module 3
DS - Unit 2 FINAL (2).pptx
Data Structures_ Sorting & Searching
Advanced s and s algorithm.ppt
Unit III Version I.pptx
Algorithm, Pseudocode and Flowcharting in C++
21-algorithms.ppt
Bsc cs ii dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Stacks,queues,linked-list
16-sorting.ppt
21-algorithms (1).ppt
Chap10
Searching and Sorting Algorithms in Data Structures
Data Structures by Maneesh Boddu

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 11
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 31
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Electronic commerce courselecture one. Pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
sap open course for s4hana steps from ECC to s4
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Electronic commerce courselecture one. Pdf
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
Understanding_Digital_Forensics_Presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.

computer notes - Data Structures - 33

  • 1. Class No.33 Data Structures http://ecomputernotes.com
  • 2. Searching an Array: Binary Search Binary search is like looking up a phone number or a word in the dictionary Start in middle of book If name you're looking for comes before names on page, look in first half Otherwise, look in second half http://ecomputernotes.com
  • 3. Binary Search If ( value == middle element ) value is found else if ( value < middle element ) search left-half of list with the same method else search right-half of list with the same method http://ecomputernotes.com
  • 4. Case 1: val == a[mid] val = 10 low = 0, high = 8 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: low high Binary Search mid mid = (0 + 8) / 2 = 4 10 http://ecomputernotes.com
  • 5. Case 2: val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example 2 5 7 9 10 1 13 17 19 27 1 2 3 4 5 6 7 0 8 a: mid low high new low new low = mid+1 = 5 13 17 19 27 http://ecomputernotes.com
  • 6. Case 3: val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example 3 10 13 17 19 5 7 9 1 27 1 2 3 4 5 6 7 0 8 a: mid low high new high new high = mid-1 = 3 5 7 9 1 http://ecomputernotes.com
  • 7. val = 7 Binary Search -- Example 3 (cont) 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a:
  • 8. Binary Search – C++ Code int isPresent(int *arr, int val, int N) { int low = 0; int high = N - 1; int mid; while ( low <= high ){ mid = ( low + high )/2; if (arr[mid]== val) return 1; // found! else if (arr[mid] < val) low = mid + 1; else high = mid - 1; } return 0; // not found }
  • 9. Binary Search: binary tree The search divides a list into two small sub-lists till a sub-list is no more divisible. First half First half An entire sorted list First half Second half Second half http://ecomputernotes.com
  • 10. Binary Search Efficiency After 1 bisection N/2 items After 2 bisections N/4 = N/2 2 items . . . After i bisections N/2 i = 1 item i = log 2 N http://ecomputernotes.com
  • 11. Implementation 3: linked list TableNodes are again stored consecutively (unsorted or sorted) insert : add to front; (1 or n for a sorted list ) find : search through potentially all the keys, one at a time; ( n for unsorted or for a sorted list remove : find, remove using pointer alterations; ( n ) key entry and so on http://ecomputernotes.com
  • 12. Implementation 4: Skip List Overcome basic limitations of previous lists Search and update require linear time Fast Searching of Sorted Chain Provide alternative to BST (binary search trees) and related tree structures. Balancing can be expensive. Relatively recent data structure: Bill Pugh proposed it in 1990. http://ecomputernotes.com
  • 13. Skip List Representation Can do better than n comparisons to find element in chain of length n 20 30 40 50 60 head tail http://ecomputernotes.com
  • 14. Skip List Representation Example: n/2 + 1 if we keep pointer to middle element 20 30 40 50 60 head tail http://ecomputernotes.com
  • 15. Higher Level Chains For general n, level 0 chain includes all elements level 1 every other element, level 2 chain every fourth, etc. level i , every 2 i th element 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
  • 16. Higher Level Chains Skip list contains a hierarchy of chains In general level i contains a subset of elements in level i-1 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
  • 17. Skip List: formally A skip list for a set S of distinct (key, element) items is a series of lists S 0 , S 1 , … , S h such that Each list S i contains the special keys  and  List S 0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one, i.e., S 0  S 1  …  S h List S h contains only the two special keys

Editor's Notes

  • #3: End of Lecture 38
  • #4: Start lecture 39
  • #18: End of lecture 39, Start of lecture 40.