Lecture Notes for Computer Algorithms I

4 December 2003 - Assignment 4 Code Review


Outline


The Problem


Wishin' for Addition


Let's Try Printing Instead


Making The Decision


Really Making the Decision


Sorting

printing words


Word Sorting

static sort_node * sort_by_words(
  sort_node * words_s, sort_node *& words_e)

  // Compress nulls.
  for snp = words_e - 1; words_s <= snp; --snp
    if (not snp->word)
      *snp = *--words_e;

  // Sort by word.
  for snp = words_s; snp < words_e; snp++
    min = snp
    for j = snp + 1; j < words_e; j++
      if min->word->word > j->word->word
	min = j
    swap(snp, min)

  // Find leftmost run.
  for snp = words_s; snp < words_e; snp++
    if snp->word->word != words_s->word->word
      break
  sort_by_count(words_s, snp)

  return snp


Printing

void
print_pages(std::ostream & os)

  sort_node * const words_s, * words_e = 
    top_words(pages_visited)

  while true
    sort_node * const words_m 
      = sort_by_words(words_s, words_e)
    if (words_s >= words_e) break

    for snp = words_s; snp < words_m; snp++
      assert(snp->word and snp->page)
      os << snp->word->word << " "
         << snp->page->page_name << " "
         << snp->word->cnt << " "
         << snp->page->word_count << "\n"
      snp->word = snp->word->next

  delete [] words_s


Consider the Alternative

word-page lists cross links


Test Cases


The Results


Count the Errors

long findBodyStart(
long start, long end, string totalString)

  for (long pos = start; pos < end; pos++)
   if totalString[pos] == '<'
    if toupper(totalString[pos+1]) == 'B'
     if toupper(totalString[pos+2]) == 'O' 
      if toupper(totalString[pos+3]) == 'D' 
       if toupper(totalString[pos+4]) == 'Y' 
        pos +=5
        while (totalString[pos] != '>') pos++
        if totalString[pos] == '>' && pos < end
         return(pos+1)

long findBodyEnd(
  long start, long end, string totalString)

  for long pos = start; pos + 6 < end; pos++
   if totalString[pos] == '<'
    if totalString[pos+1] == '/'
     if toupper(totalString[pos+2]) == 'B' 
      if toupper(totalString[pos+3]) == 'O' 
       if toupper(totalString[pos+4]) == 'D' 
        if toupper(totalString[pos+5]) == 'Y'
         if totalString[pos+6] == '>'
          return pos


A Tale of Two Sorts, I

void StrLinkedList::Sort()

  LinkedNode 
    * Current = Head_Node,
    * SmallestNode,
    * Next
  int intsmallestcount
  std::string strsmallestsword

  while Current != NULL
    SmallestNode = Current
    intsmallestcount = Current->count
    strsmallestsword = Current->word
    Next = Current->Next_Node

    while Next != NULL
      if Next->count < intsmallestcount
        SmallestNode = Next
	intsmallestcount = Next->count
	strsmallestsword = Next->word
      Next = Next->Next_Node

    SmallestNode->count = Current->count
    SmallestNode->word = Current->word
    Current->count = intsmallestcount
    Current->word = strsmallestsword
    Current = Current->Next_Node


A Tale of Two Sorts, II

void AddressLinkedList::Sort()

  Node * Current = Head,
       * GreatestNode,
       * Next;

  int intGreatestcount

  while Current != NULL

    GreatestNode = Current
    intGreatestcount = Current->Count
    Next = Current->Next

    while Next != NULL
      if Next->Count > intGreatestcount
        GreatestNode = Next
	intGreatestcount = Next->Count
    Next = Next->Next

    GreatestNode = Current
    Current->Count = intGreatestcount
    Current = Current->Next


Two Sorts Compared


Know Your Tools


Hello Garbage


Hello Hyperspace


Points to Remember


This page last modified on 5 December 2003.