Sorting Flashcards
What is a heap?
- an array visualized as a binary tree
- ordered binary tree
- an implementation of a priority queue
- it must be a complete binary tree, which means that we fill up all of the nodes on the level we are on before adding another level
ex:
5
1 | 4
3 2 | 0 -1
What is a max heap?
All parent elements must be larger than both of their child elements
What does the build-max-heap method do in heaps?
builds a max heap from an unsorted array
What does the maxheapify method do in heaps?
- assumes part of the array is already sorted
- corrects a single violation of the heap property in a tree’s subroot
What is the time complexity of maxheapify?
lg(n)
What is the time complexity of heapsort?
nlgn
What are the steps for heapsort?
- build a max heap, so that the element with the greatest value is at the top.
- switch it with the last element and remove it from the heap.
- repeat steps one and two until the heap has only one element remaining.
How do we find the left child of any parent element?
multiply the parent element’s index by 2 and add 1.
let left = 2indexOfParent + 1
How do we find the right child of any parent element?
multiply the parent element’s index by 2 and add 2
let right = 2indexOfParent + 2
What are the steps for quick sort?
- choose a pivot
- find itemFromLeft that is larger than pivot
- find itemFromRight that is smaller than pivot
- swap itemFromLeft with itemFromRight
- repeat the process
- stop when index of itemFromLeft > index of itemFromRight
- swap itemFromLeft with the pivot
How do you choose a pivot for quick sort?
-choose middle element or
- median-of-three approach
look at the first medium and last positions of the array. we sort them in those positions and choose the middle element of the array after those three elements are swapped and placed in those three positions in sorted order
What is the time complexity of quick sort?
Worst case: n^2
average case: nlogn
this depends on how effectively you choose the pivot
What advantage does quick sort have over merge sort?
it is an in place sorting algorithm which means it does not need any auxiliary data structures
Is quick sort a divide and conquer algorithm?
yes
Implement quicksort
function QuickSort(arr, left = 0, right = arr.length - 1) { let len = arr.length, index
if(len > 1) {
index = partition(arr, left, right) if(left < index - 1) { QuickSort(arr, left, index - 1) } if(index < right) { QuickSort(arr, index, right) }
}
return arr
}
function partition(arr, left, right) { let middle = Math.floor((right + left) / 2), pivot = arr[middle], i = left, // Start pointer at the first item in the array j = right // Start pointer at the last item in the array
while(i <= j) {
// Move left pointer to the right until the value at the // left is greater than the pivot value while(arr[i] < pivot) { i++ }
// Move right pointer to the left until the value at the // right is less than the pivot value while(arr[j] > pivot) { j-- }
// If the left pointer is less than or equal to the // right pointer, then swap values if(i <= j) { [arr[i], arr[j]] = [arr[j], arr[i]] // ES6 destructuring swap i++ j-- } }
return i
}
// Java program for implementation of QuickSort
class QuickSort
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j Array to be sorted,
low –> Starting index,
high –> Ending index /
void sort(int arr[], int low, int high)
{
if (low < high)
{
/ pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr, low, high);
// Recursively sort elements before // partition and after partition sort(arr, low, pi-1); sort(arr, pi+1, high); } }
/* A utility function to print array of size n */ static void printArray(int arr[]) { int n = arr.length; for (int i=0; i