Sorting Algorithms Flashcards
- Describe how the bubble sort algorithm works
- What is its time complexity?
- Look at com.barca.common.algorithms
- O(n^2)
- Describe how the select sort algorithm works
- What is its time complexity?
- Point to the first element
- Scan for the lowest element
- Swap the lowest element with the pointer element
- Increment the pointer by 1 and repeat 2 - 3
- O(n^2)
Although bubble sort and select sort are both O(n^2), is one more efficient than the other?
Yes. Bubble sort is O(n^2) while select sort is O(n^2/2). In other words, select sort is twice as efficient as bubble sort. This is important to recognize because it asserts the importance of optimizing algorithms within the same Big O class
- Describe how the insertion sort algorithm works
- What is its time complexity?
- Point to and “remove” the second element
- If the elements to the left are >, shift them to the right
- “Insert” the “removed” element in the space created by step 2
- Increment pointer and repeat 2 - 4
- Avergage case: O(n^2)
Worst case: O(n^2)
What is an in-place sorting algorithm?
An in-place sorting algorithm is an algorithm that sorts data without creating any new data structures
What is the difference between a stable and unstable sorting algorithm?
A stable algorithm maintains the order of elements that have the same value. An unstable algorithm does not maintain the order of elements that have the same value