You are on page 1of 2

Linear Search: compare each element to target Binary Search: find mid, compare to target.

If mid>target, upper = mid ||||| if mid < target, lower = mid Selection sort: search for smallest element, then swap to next position. Helper functions needed: find min, swap Quicksort: find arbitrary pi ot, separate all elements into: !""less than pi ot""# !pi ot# !""greater than pi ot""#, pi ot is in correct place $epeat %uic&sort on 'less than pi ot( and 'greater than pi ot(, merge all partitions at the end. Stacks/Queues: is)xxx*stac&+%ueue ,-, xxx)new*-,pop+push*for stac&s-,en%+de%*for %ueuesLinked List: has front, bac&, pointers to lists. .ront, bac&, and /ueue itself must 012 be 0344, also must be 5segment5 Segment: start at front, step through lists, must end at bac&, if == 034 during step, then not a segment 6hec& circularity with tortoise+hare, one ptr mo es at 7 list, one ptr mo es at 8 lists Unbounded arrays: 9<=si:e<=limit, double si:e when full, hal e si:e when 7+; full Hash tables: elem>> get &ey for that elem >> int hash*&ey <- >> int for array index, 5client5: &ey*elem-, hash*&ey-, &ey)compare Is)ht: si:e, capacity >9= length of h"> table = capacity= each element is a alid chain= all elements are non"0344 Priority Queues: remo e highest priority, min heap = smallest int, max heap = largest int, depends on implementation Heap ordering in ariants for >I0 heap: &ey of node <= children?s &eys |||||| *or- &ey of node >= parent?s &ey. 2o get left child, append '9( to binary representation of node @, right child is append '7(. 4eft child = 8i, right = 8iA7, parent = i+8 Btruct headerC int limit= int next= elem!# dataD, where next = index of array of where next insert goes. 7<= next<limit. If n=4, .344 Bifting+swapping needed when insert+delete. Insert: E next, sift up. Felete: swap*root, next"7- sift down. Heapsort>> &eep deleting Trees: 1rdering in ariant: all in left tree < node, all in right tree > node. 0ode is described by <G,. Fata = 2"> data, 012 <G, Btruct tree)nodeC int left, int right, elem dataD Type casting: same type, diff si:e ""> preser e alue ||||| diff type, same si:e ""> preser e bit pattern. Hhen casting, si:e, then typeII A L tree: ordering, same as JB2+trees. HGIKH2 in ariant: height of left+right subtrees differ by at most 7>> must rotate if >7 2ries:

!raphs: L ertices, G edges 6onnected = reach any ertex from another ia edges+++++++ directed = connection from one to the other, but not necc. Lice ersa 3ndirected = edge relation between n7, n8 is symmetric++++++++ path length = @ of edges, 4G0K2H=+= HGIKH2 Implicit graph: more of an application of the graph concept, such as peg solitaire MdNacency matrix: n x n table with if edge connects row, col. US" i# many edges $dense% MdNacency list: array with 0 elements, lin&ed list for each, or 0344OOOO use i# many &ertices' #e( edges $sparse% E graphs, must a oid cycles""> chec& with 5bool array, where 2+. for each node, node represented by index @, M=9, J=7, etc .or searching in general: %ueue = breadth, stac& = depth Spanning tree: connected graph with no cycles )a* edges = n*n"7-+8, where n = @ of ertices+++++++++++ min edges = n"7. G = 1*nP8- ""> dense OOOOO G = 1*n- """"> sparse >a&ing spanning tree: &erte* or edge centric algorithm: Lertex: start with any ertex, add edges to reach unadded ertices. $epeat until no unadded ertices. $epeats n"7 times Gdge: start with 0 single trees, connect two trees together with edge if @ of trees > 7 )inimum (eight spanning tree: weight is determined <rus&al?s algorithm: sort edges in increasing order+++++ consider edges in order from smallest, if it doesnt ma&e a cycle *ie, doesnt connect together 8 trees-, add it. 1therwise, discard it. Hhen n+, edges added' F10G. Union -ind: to chec& if in &rus&als algorithm if adding an edge will create a cycle: G%ui alence relation between two nodes if connected *reflexi e, symmetric, transiti e-. Jrea&down: if Noining 8 nodes, they must ha e different e%ui alence classes, or cycle will be formed

If merging two e%ui classes, Nust change primary ertex+node to point to longer chain. Mll others of shorter chain will point to the 'representati e(

III0oteIII: last member o# struct can be array o# unkno(n si.e: xcalloc*7, si:eof*struct-A @-, @ = si:e of array

You might also like