日期:2014-05-18  浏览次数:20691 次

小女子跪求求高手帮忙找错啊!!
题目:在CArray类里增加顺序查找方法,加快顺序查找方法,二分法以及递归二分法,并设一个compcount的私有整型变量,初始值为0,在每种查找的算法内,在执行完关键性比较后增加一行代码,对compcount进行加1操作。运行完四种方法后,比较compcount的值。 
我的代码如下:
  class CArray
  {
  private static int[] arr;
  private int numElements=0;
  private static int compCount=0;
  public CArray(int size)
  {
  arr = new int[size];
  }
  public void Insert(int item)
  {
  arr[numElements] = item;
  numElements++;
  }
  public void DisplayElements()
  {
  for (int i = 0; i <arr.Length; i++)
  Console.Write(arr[i] + " ");
  }
  public void BubbleSort()
  {
  for (int outer = arr.Length-1; outer >= 1; outer--)
  {
  for (int inner = 0; inner <= outer - 1; inner++)
  {
  if ((int)arr[inner] > arr[inner + 1])
  {
  swap(inner, inner + 1);
  }
  }
  }
  }
  private static void swap(int item1, int item2)
  {
  int temp = arr[item1];
  arr[item1] = arr[item2];
  arr[item2] = temp;
  }
  public int seqSearch(int sValue)
  {
  for (int index = 0; index < arr.Length; index++)
  {
  if (arr[index] > sValue)
  {
  compCount = compCount + 1;
  continue;
  }
  else
  if (arr[index] < sValue)
  {
  compCount = compCount + 1;
  continue;
  }
  else
  {
  return compCount;
  }
  }
  return compCount;

  }

  public static int SeqSearch(int sValue)
  {
  for (int index = 0; index < arr.Length; index++)
  {
  if (arr[index] > sValue)
  {
  compCount = compCount + 1;
  continue;
  }
  else
  if (arr[index] < sValue)
  {
  compCount = compCount + 1;
  continue;
  }
  else
  {
  compCount = compCount + 1;
  swap(index, 0);
  return compCount;
  }
  }
  return compCount;
  }

  public int binSearch(int value)
  {
  int upperBound, lowerBound, mid;
  upperBound = arr.Length - 1;
  lowerBound = 0;
  while (lowerBound <= upperBound)
  {
  mid = (upperBound + lowerBound) / 2;