P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
 
Class Notes

Wednesday November 15 , 2000
 
Synopsis
This class discussed the latest programming challenge,  Libary Classes and how we implement the rest of the Word and Dictionary classes.   Presented was a graphic example of how the dictionary is stored.   Also discused were searching and sorting methods and how they work.
 

A search method
Write a method to search an array of integers for a given integer.   Unlike the search methods of your Dictionary class,  this will be a static method.   This is also the same style of method used for the 1 st search of the Dictionary class.   It is a sequential search.  

  static private boolean search (int x,  int a [])
  {
  for (int i = 0;  i < a.length;  i + +)
  {
  if (a [i] == x)
  {
  return true;
 
}  
}   return false;
 
}
 

A sort method
This sort method which will be used in the Dicitonary class.   It is a two index sort.   The prime index points to the first number and the second index is the number being compared.   If the second number is greater than the first then the two are swapped with a use of a temp variable.  

The Code
  for (int p = 0;  p < a.length;  p + +)
  {
  for (int s = p + 1;  s < a.length;  s + +)
  {
  if (a [p] > a [s])
  {
  int temp = a [p];
  a [p] = a [s];
  a [s] = temp;
 
}  
}  
}