
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Advanced Programming Tools and Techniques; Subject: Computer Science; University: Drexel University; Term: Winter 2004;
Typology: Study notes
1 / 40
This page cannot be seen from the preview
Don't miss anything!

































/* 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 */ }
unexamined p last i n- unexamined < p p
= p 0 1 last i
n- < p p
= p 0 last n-
/* 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); }
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); }
*It is more precise to use Θ for order classes
data 1 data 2 data 3 NULL^ data 4 head
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; }