Review Sheet - Algorithms and Data Structures - Programming Tools | CS 265, Study notes of Computer Science

Material Type: Notes; Class: Advanced Programming Tools and Techniques; Subject: Computer Science; University: Drexel University; Term: Winter 2004;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-4cm
koofers-user-4cm 🇺🇸

5

(1)

10 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms and Data Structures*
Objective: To review the fundamental algorithms
and data structures that are commonly used in
programs. To see how to use and implement these
algorithms and data structures in different
languages and to see what language and library
support exists for them.
Sorting and Searching
Arrays and Vectors
Lists
–Hash Tables
Generic programming
*This material comes from chapter 2 of
Brian Kernighan and Rob Pike,
The Practice of Programming
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Review Sheet - Algorithms and Data Structures - Programming Tools | CS 265 and more Study notes Computer Science in PDF only on Docsity!

Algorithms and Data Structures*

Objective: To review the fundamental algorithmsand data structures that are commonly used inprograms. To see how to use and implement thesealgorithms and data structures in differentlanguages and to see what language and librarysupport exists for them.

  • Sorting and Searching – Arrays and Vectors – Lists – Hash Tables – Generic programming *This material comes from chapter 2 of Brian Kernighan and Rob Pike, The Practice of Programming

Topics

• Binary Search • Quicksort • Big “Oh” • Vectors • Lists • Hash Tables • C, C++, Java, Perl

Linear Search

/* lookup: sequential search for name in tab; return index */ int lookup(char *name, Nameval tab[], int ntab) {

int i; for (i = 0; i < ntab; i++)

if (strcmp(name, tab[i].name) == 0)

return i;

return –1;

/* no match */

Binary Search

/* lookup: binary search for name in tab; return index or –1 if not found. */ int lookup(char name, Nameval tab[], int ntab) { int low, high, mid, cmp; low = 0; high = ntab – 1; while (low <= hight) { mid = (low + high)/2; cmp = strcmp(name, tab[mid].name);^ if (cmp < 0) high = mid – 1; else if (cmp > 0) low = mid + 1; else / found match / return mid; } return –1; / no match */ }

Partition

unexamined p last i n- unexamined < p p

= p 0 1 last i

n- < p p

= p 0 last n-

Quicksort

/* quicksort: sort v[0]..v[n-1] into increasing order. / void quicksort(int v[], int n) { int i, last; if (n <= 1) / nothing to do / return; swap(v, 0, rand()%n); / move pivot element to v[0] / last = 0; for (i = 1; i < n; i++) / partition / if (v[i] < v[0]) swap(v, ++last, i); swap(v, 0, last); / restore pivot / quicksort(v, last); / recursively sort each part. */ quicksort(v+last+1, n-last-1); }

Libraries

• C: qsort • C++: sort (algorithm library from STL) • java.util.collections.sort • perl: sort

qsort (strings)

char str[N]; qsort(str, N, sizeof(str[0]), scmp); / scmp: string compare of *p1 and *p2 */ int scmp(const void *p1, const void *p2) { char v1, v2; v1 = ((char) p1); v2 = ((char) p2); return strcmp(v1, v2); }

Big “Oh”

Notation

Name

Example

O(1)

constant

array index

O(log n)

logarithmic

binary search

O(n)

linear

string comparison

O(nlog n)

n log n

quicksort/mergesort

O(n

quadratic

insertion sort

O(n

cubic

matrix multiplication

O(

n

exponential

set partitioning

*It is more precise to use Θ for order classes

Timing

Unix time command

  • [jjohnson@ws56 lec1]$ time sign < /usr/share/dict/words | sort | squash > temptime - 0.11user 0.01system 0:00.27elapsed 43%CPU (0avgtext+0avgdata 0maxresident)k - 0inputs+0outputs (91major+13minor)pagefaults 0swaps

clock()

counting cycles

Growing Arrays

Arrays provide O(1) access and insertion time

Sorted arrays provide O(log n) search time andO(n) insertion time [have to move elements]

If the number of elements in an array is not knownahead of time it may be necessary to resize thearray.

Involves dynamic memory allocation and copying

To minimize the cost it is best to resize in chunks

Growing Arrays in C

typedef struct Nameval Nameval; struct Nameval {

char

*name;

int

value;

}; struct NVtab {

int

nval;

/* current number of values */

int

max;

/* allocated number of values */

Nameval nameval; / array of name-value pairs */

}; enum { NVINIT = 1, NVGROW = 2 };

Lists

• A sequence of elements • Space is allocate for each new element and

consecutive elements are linked togetherwith a pointer.

• O(1) time to insert at front, O(n) to append

unless pointer to last element kept, O(n)traversal time.

data 1 data 2 data 3 NULL^ data 4 head

Lists in C

typedef struct Nameval Nameval; struct Nameval { char *name; int value; Nameval next; / in list / }; / newitem: create new item from name and value */ Nameval *newitem(char *name, int value) { Nameval *newp; newp = (Nameval *) emalloc(sizeof(Nameval)); newp->name = name; newp->value = value; newp->next = NULL; return newp; }