Linked Lists & Sorting Algorithms Flashcards
What is the complexity for inserting at the head in a singly linked list?
O(1)
What is the complexity for removing at the head in a singly linked list?
O(1)
What is the complexity for inserting at the tail in a singly linked list?
O(1)
What is the complexity for removing at the tail in a singly linked list?
O(n)
What changes between a singly linked list and a double linked list?
The complexity for deletion at the tail is now reduced to O(1)
What is the basic idea of bubble sort?
Cycle through the array over and over again until no swaps are made. You take two elements next to each other, and you compare them. If the swap triggers, you then perform it and move on, otherwise you move on regardless. This repeats until no swaps are made in a single loop.
What is sorting ‘stability’?
A sort is stable if it preserves the original order of duplicate elements i.e. 3, 2, 2’, 1 would be sorted into 1, 2, 2’, 3. If it preserved the order of two and two prime, then it is stable, otherwise it is unstable.
What is the complexity of bubble sort?
Worst case complexity - O(n^2)
What is the basic idea of selection sort?
Similar to bubble sort, but on each scan, you remember what element has to be swapped and then move it at the end of the scan/loop directly to the spot.
What is the complexity of selection sort?
O(n^2)
What is the basic idea of insertion sort?
Keep the front of the list sorted, and then as you move through the back, we insert elements from there into the correct place at the front
What is the complexity of insertion sort?
In worst case, also O(n^2), but in best case, O(n)
What is the complexity of bubble sort on linked lists?
Also O(n^2)
What is the complexity of selection sort on linked lists?
Also O(n^2)
What is a preorder traversal regarding linked trees?
A node is visited before its descendants