lecture_4
Sorting and Searching
Importance of Searching: Basic operation to check if an element is stored within a data structure.
Sorting: Arranging items in non-decreasing order, which can enhance search efficiency (e.g. binary search).
Classic Sorting Algorithms
Introduction of classic sorting algorithms
Overview of three algorithms: Insertion Sort, Selection Sort, Bubble Sort
Focus on Insertion Sort during the lecture, with other two for home review.
Insertion Sort
Concept: If the first
inumbers are sorted, thei+1th number can be inserted into the sorted section.Example: Given an unsorted array, start with a sorted section and insert elements until the entire array is sorted.
Each iteration enlarges the sorted zone.
Time Complexity: Worst case is (O(n^2)) due to nested iterations.
Insertion Process: Inserting involves comparing the new element with elements in the sorted section from the back to find the appropriate position.
Other Sorting Algorithms
Selection Sort: Another classic example discussed in a sentence; students encouraged to explore its visualizations.
Bubble Sort: Simple but inefficient; also discussed briefly.
Time Complexity of All Three: (O(n^2)) in worst cases.
Introduction to Data Structures
First Data Structure: List
Definition: A collection of items stored in a linear shape.
Abstract Data Type (ADT): A list as an implementation within Python; considerations of what data is stored and what operations are supported.
List vs. Array
List: Stores items with relative position, allowing easy adjustment of size.
Array: Requires consecutive memory and fixed size, making insertion and resizing more complex.
Pros and Cons variation noted for arrays (constant time access) vs lists (dynamic length).
Array List Introduction
Rediscovered Array List: A type of list utilizing an array structure internally.
Contains:
Array: Actual storage of items.
Size Attribute: Tracks number of items stored.
Empty Structure Creation: Noted instantiation method using constructor:
Default length can be set or chosen by the user.
Array List Operations
Constructor Method: Defines how to set up an initial empty structure and size is set to zero.
Size: Returns the number of items in the list with a time complexity of (O(1)).
Check if Empty: Uses size to quickly return if the list is empty, also (O(1)).
Searching in Array List
Contains and Index Operations: Check if an item is in the list and return its index. Both are linear searches for unsorted lists, thus (O(n)).
Popping Items
Pop Last Item: Removes last item; quick operation with constant time use of size.
Pop By Index: Requires shifting elements if an item is removed from a non-final position, leading to a time complexity of (O(n)).
Remove Item
Find the index of the item and then use the pop function; worst case complexity is the same as pop operations (linear time).
Concluding Thoughts
Reinforcement that these operations can be constructed and analyzed for complexity, crucial for understanding performance in data structures.