\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{jasondias} \pdfinfo{ /Title (comp250.pdf) /Creator (Cheatography) /Author (jasondias) /Subject (COMP250 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{COMP250 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{jasondias} via \textcolor{DarkBackground}{\uline{cheatography.com/21209/cs/5468/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}jasondias \\ \uline{cheatography.com/jasondias} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 20th October, 2015.\\ Updated 9th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{4} \begin{tabularx}{3.833cm}{x{1.40753 cm} x{2.02547 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} Definition & unambiguous procedure executed in a finite number of steps \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} What makes a good algorithm? & Correctness, Speed, Space, Simplicity \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} Speed: & time it takes to solve problem \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} Space: & amount of memory required \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} Simplicity: & easy to understand, easy to implement, easy to debug, modify, update \tn % Row Count 12 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.6066 cm} x{1.2132 cm} x{1.2132 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{3.833cm}}{\bf\textcolor{white}{Running Time}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Definition} & measurement of the speed of an algorithm & \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{Dependent} \seqsplit{variables:} & size of input \& content of input & \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Best Case: & time on the easiest input of fixed size & meaningless \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Average Case: & time on average input & good measure, hard to calculate \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} Worst Case: & time on most difficult input & good for safety critical systems, easy to estimate \tn % Row Count 15 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.0299 cm} x{2.4031 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Loop Invariants}} \tn % Row 0 \SetRowColor{LightBackground} Definition & loop property that holds before and after every iteration of a loop. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{Steps:} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} 1. \seqsplit{Initialization} & If it is true prior to the iteration of the loop \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} 2. Maintenance & If it is true before an iteration of the loop, it remains true before the next iteration \tn % Row Count 10 (+ 4) % Row 4 \SetRowColor{LightBackground} 3. Termination & When the loop terminates, the invariant gives us a useful property that helps show the algorithm is correct \tn % Row Count 14 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.75526 cm} x{2.67774 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{In Place Sorting}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Definition:} & Uses only a constant amount of memory in addition of that used to store the input \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{Importance:} & Great for large data sets that take up large amounts of memory \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Examples:} & Selection Sort, Insertion Sort (Only moving elements around the array) \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{MergeSort:} & Not in place: new array required \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{QuickSort}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Algorithm partition(A, start, stop) \newline Input: An array A, indices start and stop. \newline Output: Returns an index j and rearranges the elements of A \newline such that for all i\textless{}j, A{[}i{]} ! A{[}j{]} and \newline for all k\textgreater{}i, A{[}k{]} " A{[}j{]}. \newline pivot \# A{[}stop{]} \newline left \# start \newline right \# stop - 1 \newline while left ! right do \newline while left ! right and A{[}left{]} ! pivot) do left \# left + 1 \newline while (left ! right and A{[}right{]} " pivot) do right \# right -1 \newline if (left \textless{} right ) then exchange A{[}left{]} \$ A{[}right{]} \newline exchange A{[}stop{]} \$ A{[}left{]} \newline return left} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Time Complexities: \newline • Worse case: \newline – Already sorted array (either increasing or decreasing) \newline – T(n) = T(n-1) + c n + d \newline – T(n) is O(n2) \newline • Average case: If the array is in random order, the \newline pivot splits the array in roughly equal parts, so the \newline average running time is O(n log n) \newline • Advantage over mergeSort: \newline – constant hidden in O(n log n) are smaller for quickSort. \newline Thus it is faster by a constant factor \newline – QuickSort is easy to do "in-place"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.51495 cm} x{2.91805 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{QuickSort}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Divide:} & choose an element of the array for pivot \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & divide into 3 sub-groups; those smaller, those larger and those equal to pivot \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{Conquer} & recursively sort each group \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{Combine} & concatenate the 3 lists \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Proofs by Induction (Examples)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{3.833cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/jasondias_1445380086_Screen Shot 2015-10-20 at 6.28.15 PM.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{0.92691 cm} x{2.50609 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Object Orientated Programming}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Definition:} & User defined types to complement primitive types \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & Called a class \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} Contains: & Data \& methods \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} Static members: & members shared by all objects of the class \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{0.89258 cm} x{2.54042 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Recursion Programming}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Definition} & using methods that call themselves \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{Structure:} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} base case & a simple occurrence that can be answered directly \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} recursive case & A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem. \tn % Row Count 11 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.51495 cm} x{2.91805 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Divide \& Conquer}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Divide} & the problem into sub problems that are similar to the original but smaller in size \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{Conquer} & the sub-problems by solving them recursively. If they are small enough, solve them in a straightforward manner \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{Combine} & the solutions to create a solution to the original problem \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.51052 cm} x{1.92248 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{BIG O Definition}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{f(n) \& g(n) are two non negative functions defined on the natural numbers N} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} f(n) is O(g(n)) if and only if: & there exists an integer n0 and a real number c such that Ɐn\textgreater{}= n0 f(n) \textless{}= c * g(n) \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} & N.B. The constant c must not depend on n \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Big O Visualization}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{3.833cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/jasondias_1445523925_Screen Shot 2015-10-22 at 10.25.11 AM.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Big O Recurrence}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{3.833cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/jasondias_1445529071_camera_capture.jpg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Sum of a\textasciicircum{}i from 0 to n = (a\textasciicircum{}(n+1) -1 )/(a-1)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Limitations of Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Size has to be known in advance} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{memory required may be larger than number of elements} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{inserting or deleting an element takes up to O(n)} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.6866 cm} x{2.7464 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{ADT: Queues}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{FIFO(First in first out)} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{3.833cm}}{Any first come first serve service} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{Operations} & enqueue() - add to rear \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} & dequeue() - removes object at front \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} & front() - returns object at front \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} & size() - returns number of objects O(n) \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} & isEmpty() - returns true if empty \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{Double Ended Queues(deque): Allows for insertions and removals from front and back \newline - By adding reference to previous node - removals occur in O(1)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.78959 cm} x{2.64341 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{ATD: Stacks}} \tn % Row 0 \SetRowColor{LightBackground} Def: & Operations allowed at only one end of the list (top) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & LIFO: (Last in first out) \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{Operations:} & push() - inserts element at top \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} & pop() - removes object at top \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} & top() - returns top element without removing it \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} & size() - returns number of elements \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} & isEmpty() - returns True if empty \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{Applications} & page visited history in web browser \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} & JVM - keeps track of chain of active elements (allows for rec) \tn % Row Count 17 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{Performance:} & space used: O(n) \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} & operations: O(1) \tn % Row Count 20 (+ 1) % Row 11 \SetRowColor{white} \seqsplit{Limitations} & max size must be defined prior \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} & pushing to a full stack causes implementation specific error \tn % Row Count 24 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{BinarySearch}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{BinarySearch(A{[}0..N-1{]}, value) \{ \newline low = 0 \newline high = N - 1 \newline while (low \textless{}= high) \{ \newline mid = (low + high) / 2 \newline if (A{[}mid{]} \textgreater{} value) \newline high = mid - 1 \newline else if (A{[}mid{]} \textless{} value) \newline low = mid + 1 \newline else \newline return mid \newline \} \newline return not\_found // value would be inserted at index "low" \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Invariants: \newline `value \textgreater{} A{[}i{]} for all i \textless{} low ` `value \textless{} A{[}i{]} for all i \textgreater{} high` \newline Worst case performance: O(log n) \newline Best case performance: O(1) \newline Average case performance: O(log n)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{BinarySearch (Recursive)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{int bsearch(int{[}{]} A, int i, int j, int x) \{ \newline if (i\textless{}j) \{ \newline int e = {[}(i+j)/2{]}; \newline if (A{[}e{]} \textgreater{} x) \{ \newline return bsearch(A,i,e-1); \newline \} else if (A{[}e{]} \textless{} x) \{ \newline return bsearch(A,e+1,j); \newline \} else \{ \newline return e; \newline \} \newline \} else \{ return -1; \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Time Complexity: log(base2)(n)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Insertion Sort (Iterative)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{For i ← 1 to length(A) - 1 \newline j ← i \newline while j \textgreater{} 0 and A{[}j-1{]} \textgreater{} A{[}j{]} \newline swap A{[}j{]} and A{[}j-1{]} \newline j ← j - 1 \newline end while \newline end for} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Time complexity: O(n\textasciicircum{}2\textasciicircum{})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Merge-then-sort}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Algorithm ListIntersection (A,m, B,n) \newline Input: Same as before \newline Output: Same as before \newline inter ← 0 \newline Array C{[}m+n{]}; \newline for i ← 0 to m-1 do C{[}i{]} ← A{[}i{]}; \newline for i ← 0 to n-1 do C{[} i+m {]} ← B{[}i{]}; \newline C ← sort( C, m+n ); \newline ptr ← 0 \newline while ( ptr \textless{} m+n-1 ) do \{ \newline if ( C{[}ptr{]} = C{[}ptr+1{]} ) then \{ \newline inter ← inter+1 \newline ptr ← ptr+2 \newline \} \newline else ptr ← ptr+1 \newline \} \newline return inter} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Time Complexity: (m+n) * (log(m+n)) + m + n -1} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{MergeSort (Recursive)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{MergeSort (A, p, r) // sort A{[}p..r{]} by divide \& conquer \newline if p \textless{} r \newline then q ← ⎣(p+r)/2⎦ \newline MergeSort (A, p, q) \newline MergeSort (A, q+1, r) \newline Merge (A, p, q, r)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Time Complexity: 2T(n/2) + k • n + c'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Primitive Operations}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{assignment} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{calling method} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{returning from method} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{arithmetic} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{comparisons of primitive types} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{conditional} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{indexing into array} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{following object reference} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Assume each primitive operation holds the same value = 1 primitive operation} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Prove Big - Oh}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{3.833cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/jasondias_1445524538_camera_capture.jpg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}