SlideShare a Scribd company logo
1 of 20
Data Structures, Search and Sort
Algorithms
Kar-Hai Chu
karhai@hawaii.edu
Data structures
 Storage
 Insertion, deletion
 Searching
 Sorting
 Big O
Stacks
 LIFO
 Push, pop
 O(1) operations
Linked lists v. Arrays
 Linked lists:
– Resizable
– Insertion/deletion
 Arrays:
– Faster index
– O(1) lookup
– Preset size
Hash tables
 Keys and values
 O(1) lookup
 Hash function
– Good v fast
 Clustering
 Databases
Selection sort :-(
 O(n2)
 Algorithm:
– Find the minimum value
– Swap with 1st position value
– Repeat with 2nd position down
Insertion sort :-)
 O(n2)
 O(1) space
 Great with small number of elements
(becomes relevant later)
 Algorithm:
– Move element from unsorted to sorted list
Bubble sort :-(
 O(n2)
 Algorithm:
– Iterate through each n, and sort with n+1
element
 Maybe go n-1 steps every iteration?
 Great for big numbers, bad for small
 Totally useless?
Merge sort :-)
 O(nlogn)
 Requires O(n) extra space
 Parallelizable
 Algorithm:
– Break list into 2 sublists
– Sort sublist
– Merge
Quick sort :-)
 Average O(nlogn), worst O(n2)
 O(n) extra space (can optimized for O(logn))
 Algorithm:
– pick a pivot
– put all x < pivot in less, all x > pivot in more
– Concat and recurse through less, pivot, and more
 Advantages also based on caching, registry
(single pivot comparison)
 Variations: use fat pivot
Linear search :-(
 O(n)
 Examines every item
Binary search :-)
 Requires a sorted list
 O(log n)
 Divide and conquer
Trees
 Almost like linked lists!
 Traverse: Pre-order v. Post-order v. In-
order
 Node, edge, sibling/parent/child, leaf
Binary trees
 0, 1, or 2 children per node
 Binary Search Tree: a binary tree where
node.left_child < node.value and
node.right_child >= node.value
Balanced binary
trees
 Minimizes the level of nodes
 Compared with “bad” binary tree?
 Advantages:
– Lookup, insertion, removal: O(log n)
 Disadvantages:
– Overhead to maintain balance
Heaps (binary)
 Complete: all leafs are at n or n-1,
toward the left
 Node.value >= child.value
 In binary min/max heap
– Insert = O(logn) .. add to bottom, bubble-up
– deleteMax = O(logn) .. Move last to root
and bubble-down
Heapsort
 O(nlogn)
 Algorithm:
– Build a heap
– deleteMax (or Min) repeatedly
 O(1) overhead
Why bother?
 Tries (say trees)
– Position determines the key
– Great for lots of short words
– Prefix matching
 But..
– Long strings..
– Complex algorithms
Chess!
 Minimax:
 Alpha-beta pruning - pick a bag!
– ordering
B: B1 B: B2 B: B3
A: A1 +3 -2 +2
A: A2 -1 0 +4
A: A3 -4 -3 +1
Useful
 http://www.cs.pitt.edu/~kirk/cs1501/anim
ations/Sort3.html

More Related Content

What's hot

Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
A brief introduction to scalaz-stream
A brief introduction to scalaz-streamA brief introduction to scalaz-stream
A brief introduction to scalaz-stream
Frank Thomas
 

What's hot (20)

Arrays
ArraysArrays
Arrays
 
Standard Library Functions
Standard Library FunctionsStandard Library Functions
Standard Library Functions
 
Data structures
Data structuresData structures
Data structures
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Understanding the components of standard template library
Understanding the components of standard template libraryUnderstanding the components of standard template library
Understanding the components of standard template library
 
List
ListList
List
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
The awesome algorithm
The awesome algorithmThe awesome algorithm
The awesome algorithm
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Maps
MapsMaps
Maps
 
Mutable data types in python
Mutable data types in pythonMutable data types in python
Mutable data types in python
 
A brief introduction to scalaz-stream
A brief introduction to scalaz-streamA brief introduction to scalaz-stream
A brief introduction to scalaz-stream
 
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
2nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 12nd puc computer science chapter 3 data structures 1
2nd puc computer science chapter 3 data structures 1
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Data structure day1
Data structure day1Data structure day1
Data structure day1
 

Similar to 702 present

Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
Bindiya syed
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 

Similar to 702 present (20)

Short dec
Short decShort dec
Short dec
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Sorting
SortingSorting
Sorting
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
46267037-Data-Structures-PPT.ppt
46267037-Data-Structures-PPT.ppt46267037-Data-Structures-PPT.ppt
46267037-Data-Structures-PPT.ppt
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 
tree.ppt
tree.ppttree.ppt
tree.ppt
 
Linked list ppt
Linked list pptLinked list ppt
Linked list ppt
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 

702 present

  • 1. Data Structures, Search and Sort Algorithms Kar-Hai Chu karhai@hawaii.edu
  • 2. Data structures  Storage  Insertion, deletion  Searching  Sorting  Big O
  • 3. Stacks  LIFO  Push, pop  O(1) operations
  • 4. Linked lists v. Arrays  Linked lists: – Resizable – Insertion/deletion  Arrays: – Faster index – O(1) lookup – Preset size
  • 5. Hash tables  Keys and values  O(1) lookup  Hash function – Good v fast  Clustering  Databases
  • 6. Selection sort :-(  O(n2)  Algorithm: – Find the minimum value – Swap with 1st position value – Repeat with 2nd position down
  • 7. Insertion sort :-)  O(n2)  O(1) space  Great with small number of elements (becomes relevant later)  Algorithm: – Move element from unsorted to sorted list
  • 8. Bubble sort :-(  O(n2)  Algorithm: – Iterate through each n, and sort with n+1 element  Maybe go n-1 steps every iteration?  Great for big numbers, bad for small  Totally useless?
  • 9. Merge sort :-)  O(nlogn)  Requires O(n) extra space  Parallelizable  Algorithm: – Break list into 2 sublists – Sort sublist – Merge
  • 10. Quick sort :-)  Average O(nlogn), worst O(n2)  O(n) extra space (can optimized for O(logn))  Algorithm: – pick a pivot – put all x < pivot in less, all x > pivot in more – Concat and recurse through less, pivot, and more  Advantages also based on caching, registry (single pivot comparison)  Variations: use fat pivot
  • 11. Linear search :-(  O(n)  Examines every item
  • 12. Binary search :-)  Requires a sorted list  O(log n)  Divide and conquer
  • 13. Trees  Almost like linked lists!  Traverse: Pre-order v. Post-order v. In- order  Node, edge, sibling/parent/child, leaf
  • 14. Binary trees  0, 1, or 2 children per node  Binary Search Tree: a binary tree where node.left_child < node.value and node.right_child >= node.value
  • 15. Balanced binary trees  Minimizes the level of nodes  Compared with “bad” binary tree?  Advantages: – Lookup, insertion, removal: O(log n)  Disadvantages: – Overhead to maintain balance
  • 16. Heaps (binary)  Complete: all leafs are at n or n-1, toward the left  Node.value >= child.value  In binary min/max heap – Insert = O(logn) .. add to bottom, bubble-up – deleteMax = O(logn) .. Move last to root and bubble-down
  • 17. Heapsort  O(nlogn)  Algorithm: – Build a heap – deleteMax (or Min) repeatedly  O(1) overhead
  • 18. Why bother?  Tries (say trees) – Position determines the key – Great for lots of short words – Prefix matching  But.. – Long strings.. – Complex algorithms
  • 19. Chess!  Minimax:  Alpha-beta pruning - pick a bag! – ordering B: B1 B: B2 B: B3 A: A1 +3 -2 +2 A: A2 -1 0 +4 A: A3 -4 -3 +1