AlgorithmNote

=Sorting= Refer to sorting-alg

Insertion Sort

 * 1) put 5 after 1
 * 2) put 2 after 1 before 5
 * 3) put 6 after 5


 * Best = O(n)
 * Worst = O(n^2)

While the best case is significantly faster than the worst case, the worst case is usually of the typical running time. Refer to insertion-sort

Bubble Sort
Swap between neighbour nodes, till no swap is needed.
 * Best = O(n)
 * Worst = O(n^2)

void bubblesort(E[] A) { for (int i=0; ii; j--) if ((A[j].compareTo(A[j - 1]) < 0)) swap(A, j, j - 1); } Refer to bubble-sort

Selection Sort

 * 1) left-boundary at [0]
 * 2) swap smallest with left-boundary/left-most
 * 3) move left boundary one-element-right
 * 4) loop


 * Best/worse/average O(n^2)
 * swap <= O(n)

void selectsort(E[] A) { for (int i=0; ii; j--) // Find the least value if (A[j].compareTo(A[lowindex]) < 0) lowindex = j; // Put it in place swap(A, i, lowindex); } }

Refer to selection-sort

Shell sort
void shellsort(E[] A) { for (int i=A.length/2; i>2; i/=2) // For each increment for (int j=0; j> void inssort2(E[] A, int start, int incr) { for (int i=start+incr; i=incr)&&           (A[j].compareTo(A[j-incr])<0); j-=incr) swap(A, j, j-incr); } Refer to shell-sort
 * Worst O(n^2)
 * Best O(nlogn)
 * Worse space O(n)

Merge sort

 * Best O(nlogn)
 * Worse O(nlogn)
 * Worst space O(n)

List mergesort(List inlist) { if (inlist.length <= 1) return inlist;; List L1 = half of the items from inlist; List L2 = other half of the items from inlist; return merge(mergesort(L1), mergesort(L2)); }

void mergesort(E[] A, E[] temp, int l, int r) { int mid = (l+r)/2; // Select midpoint if (l == r) return; // List has one element mergesort(A, temp, l, mid); // Mergesort first half mergesort(A, temp, mid+1, r); // Mergesort second half for (int i=l; i<=r; i++) // Copy subarray to temp temp[i] = A[i]; // Do the merge operation back to A   int i1 = l; int i2 = mid + 1; for (int curr=l; curr<=r; curr++) { if (i1 == mid+1) // Left sublist exhausted A[curr] = temp[i2++]; else if (i2 > r) // Right sublist exhausted A[curr] = temp[i1++]; else if (temp[i1].compareTo(temp[i2])<0) // Get smaller A[curr] = temp[i1++]; else A[curr] = temp[i2++]; } } Refer to merge-sort

Quick sort

 * 1) Pick an element, called a pivot, from the array.
 * 2) Reorder the array so that all elements with values less than the pivot come before the pivot
 * 3) while all elements with values greater than the pivot come after it
 * 4) equal values can go either way
 * 5) recursive (into sub arrays)

void qsort(E[] A, int i, int j) { // Quicksort int pivotindex = findpivot(A, i, j); // Pick a pivot swap(A, pivotindex, j); // Stick pivot at end // k will be the first position in the right subarray int k = partition(A, i-1, j, A[j]); swap(A, k, j); // Put pivot in place if ((k-i) > 1) qsort(A, i, k-1); // Sort left partition if ((j-k) > 1) qsort(A, k+1, j); // Sort right partition }
 * Best O(n log n) simple-partition / O(n) three-way-partition
 * Worst O(n^2)
 * Worst space O(n) auxiliary stable / O(nlogn) auxiliary in-space

int findpivot(E[] A, int i, int j) { return (i+j)/2; }

int partition(E[] A, int left, int right, E pivot) { do { // Move bounds inward until they meet while (A[++left].compareTo(pivot)<0); while ((right!=0) && (A[--right].compareTo(pivot)>0));

swap(A, left, right); // Swap out-of-place values } while (left < right); // Stop when they cross swap(A, left, right); // Reverse last, wasted swap return left; // Return first position in right partition }

Heap sort O(n log n)
void heapsort(E[] A) { // The heap constructor invokes the buildheap method MaxHeap H = new MaxHeap(A, A.length, A.length); for (int i=0; i[] B = (LList[])new LList[MaxKey]; Integer item; for (int i=0; i; for (int i=0; i<A.length; i++) B[A[i]].append(A[i]); for (int i=0; i<MaxKey; i++) for (B[i].moveToStart; (item = B[i].getValue) != null; B[i].next) output(item); }

Radix sort
void radix(Integer[] A, Integer[] B,   int digWidth, int digBase, int[] count) { // Count[i] stores number of records in bin[i] int i, j;   int digRange; // digRange=1,10,100 for (i=0, digRange=1; i=0; j--) B[--count[(A[j]/digRange)%digBase]] = A[j];

// Copy B back for (j=0; j<A.length; j++) A[j] = B[j]; } }

=Searching= =Appendix=
 * Refer to art of computer programming