CS 137

Operator Table

precedence operator associativity
1 ++,-- (post) left
2 ++,-- (pre) left
+,- (unary) right
3 *,/,% left
4 +,- (binary) left
5 =,+=,-=,*=,/= right

Left Associativity

Right Associativity

Logical Expressions
in C: false = 0, true = non-zero

operator precedence associativity
! same as unary right
relational lower than arithmetic left
equality lower than relational left
&&, \ \


An assertion is assumed to be true at any given point in a program.

#include <assert.h>

bool leap(int year){
    assert(year>1573); //if true, continues. if false, terminates program
                 //with a message stating file name, line #, function 
 //name and expression. Mostly for debugging.
    if(year % 400 ==0)

Separate Compilation

e.g. power.c

int square(int num) {return num*num;}

#include <stdio.h>
int square(int num); //function declaration, with no definition

void testSquare(int num){
    printf(“%d^4 = %d\n”, num, square(num));
int main(){

$ gcc -o powers main.c power.c

order doesn’t matter. Links files together.

Header Files

Sieve of Erotosthenes


Insertion Sort

  for 2..n 
    move element left into sorted section until the the left is smaller

Selection Sort

  for 1..n
    iterate through all remaining elements for smallest and swap with current

Always quadratic, but minimizes swaps


  for i=1..n
    for j=n..(i+1)
      if a[j] < a[j-1] swap
    if no swaps, finished

Best case O(n) linear

Naive Quicksort

2-way partition

k = 1
for i = 2:n, if a[i] < a[1], swap a[++k,i]
swap a[1,k]
→ invariant: a[1..k-1] < a[k] <= a[k+1..n]

recursive sorts

sort a[1..k-1]
sort a[k+1,n]