#include <iostream.h>
/**
 * Description: liner sort.
 * Date: 2007-11-10
 */
int linerSort(int List[], int n, int num)
{
 for (int i = 0; i < n; i++)
 {
  if (List[i] == num)
  {
   return i;
  }
 }

 return -1;
}

/**
 * Description: half sort.
 * Date: 2007-11-10
 */
int halfSort(int list[], int low, int high, int num)
{
 int mid;

 while (low <= high)
 {
  mid = (low + high) / 2;

  if (num == list[mid])
  {
   return mid;
  }
  else if (num < mid)
  {
   high = mid - 1;
  }
  else
  {
   low = mid + 1;
  }
 }

 return -1;
}

void main()
{
 int list[] = {1,3,5,7,9};

 cout << "linerSort find: " << linerSort(list, sizeof(list), 10) << endl;
 cout << "halfSort find: " << halfSort(list,0, sizeof(list) - 1, 3) << endl;
}

posted on 2007-11-10 19:00  IT Person  阅读(311)  评论(0)    收藏  举报