找指定元素
static bool SeqSearch(int[] arr, int sValue) {
for (int index = 0; index < arr.Length-1; index++)
if (arr[index] == sValue)
return true;
return false;
}
找最大值
static int FindMax(int[] arr) {
int max = arr[0];
for(inti=0;i<arr.Length-1; i++)
if (arr[index] > max)
max = arr[index];
return max;
}
找最小值
static int FindMin(int[] arr) {
int min = arr[0];
for(inti=0;i<arr.Length-1; i++)
if (arr[index] < min)
min = arr[index];
return min;
}
改进的顺序查找,使用2/8定律,将最常要搜索的数据,放到数组的前百分之20的位置上,类似冒泡排序
static int SeqSearch(int sValue) {
for(int index = 0;i<arr.Length-1; i++)
if (arr[index] == sValue && index > (arr.Length * 0.2)){
swap(index, index-1);
return index;
} else
if (arr[index] == sValue)
return index;
return -1;
}
static void swap(ref int item1, ref int item2) {
int temp = arr[item1];
arr[item1] = arr[item2];
arr[item2] = temp;
}