Midterm Flashcards
Lists - insert (appending at end)
Constant O(1)
Lists - insertAfter
Linear O(n)
Lists - search
Linear O(n)
Lists - sorting
O(n^2)
Array backward - insert
O(1)
Arrays - searching
O(n)
Array - sorting
O(n^2)
Array - once sorting, searching (ordered array)
O(log2(n))
Array - sorted all the time, insert
O(lg(n))
Stacks
LIFO
Push O(1) Pop O(1) Peep O(1) IsEmpty O(1) —> look at head (null) isFull O(1)
Do not support the removal of items from the middle
Push
Add element to top of stack
Change head
Pop
Get head
Remove the top item from the stack
Peep
Without removing say what’s on top
Queues
FIFO
Enqueue O(1) Dequeue O(1)
A list in which all insertions take place at one end (back) and all deletions take place at the opposite end (front)
Does not allow the insertion or deletion of items from the middle
Enqueue
From back
Add new item to the back
Dequeue
From front
Remove an item from the front
Arrays
Used to store multiple instance of anything as long as they are all of the same kind
Order is important
Linked lists
Can grow or shrink as needed
Not necessarily stored in contiguous memory locations
First node in a linked list
Head
Last node in a linked list
Tail (link component will always be null)
Bubble sort
Processes list of items from left-to-right
Repeatedly comparing two neighboring elements (positions i and i+1)
If two neighboring elements are out of order, they are swapped
Continue to next element to the right
Repeat until the end of the list (last element now sorted)
Largest element will bubble to the right
N-1 passes
Sequential search
Linear search
Process of locating a value in a list by checking each element one at a time until either the value is located or the end of the list has been reached
Not efficient
N/2 comparisons
Binary search
Process of locating a value in an ordered list of values by repeatedly comparing the value in the middle of the list to the desired value and discarding the appropriate half
Searching terminates when either the desired value is located or the least can no longer be divided in half
Binary search - find middle
Floor(n/2) + 1
Binary search - number of comparisons
Ceiling(Lg(n+1))
Round up
Bubble sort - number of comparisons
1/2(n^2 - n)
Optimized bubble sort
Terminating the bubble sort during a pass if no swaps are made
Selection sort
Processes a list of items from left-to-right
Finding the smallest value in the unsorted portion of the list and swapping it with the first value in the unsorted portion
Increases sorted portion of list and shrinks the unsorted portion by one value
n-1 passes
As each pass was made, fewer and fewer elements were compared
Selection sort - number of comparisons
1/2(n^2 - n)