二分法查找
实现方法
public static int binarysearch(int []a,int left,int right,int findVal) {
int mid=(left+right)/2;
if(left>right)return -1;
int midval=a[(left+right)/2];
if(midval<findVal) {
return binarysearch(a, mid+1, right, findVal);
}else if(midval>findVal){
return binarysearch(a, left, mid-1, findVal);
}
return mid;
}
插值查找
插值查找方法和二分法类似,在实现过程中mid值变成了left+(findVal-arr[left])/(arr[right]-arr[left])*(right-left)
实现方法
public static int insertValueSearch(int[] arr, int left, int right, int findVal) {
if (left > right || findVal < arr[0] || findVal > arr[arr.length - 1]) {
return -1;
}
// 求出mid, 自适应
int mid = left + (right - left) * (findVal - arr[left]) / (arr[right] - arr[left]);
System.out.println("mid:"+mid);
int midVal = arr[mid];
if (findVal > midVal) { // 说明应该向右边递归
return insertValueSearch(arr, mid + 1, right, findVal);
} else if (findVal < midVal) { // 说明向左递归查找
return insertValueSearch(arr, left, mid - 1, findVal);
} else {
return mid;
}
}
斐波那契查值
斐波那契(黄金分割法)原理:斐波那契查找原理与前两种相似,仅仅改变了中间结点(mid)的位置,mid不再是中间或插值得到,而是位于黄金分割点附近,即mid=low+F(k-1)-1
//初始化斐波那契数列
public static int[] fib() {
int[] f = new int[maxSize];
f[0] = 1;
f[1] = 1;
for (int i = 2; i < maxSize; i++) {
f[i] = f[i - 1] + f[i - 2];
}
return f;
}
public static int fibSearch(int[] a, int key) {
int low = 0;
int high = a.length - 1;
int k = 0; //表示斐波那契分割数值的下标
int mid = 0; //存放mid值
int f[] = fib(); //获取到斐波那契数列
//获取到斐波那契分割数值的下标
while(high > f[k] - 1) {
k++;
}
//因为 f[k] 值 可能大于 a 的 长度,因此我们需要使用Arrays类,构造一个新的数组,并指向temp[]
//不足的部分会使用0填充
int[] temp = Arrays.copyOf(a, f[k]);
//实际上需求使用a数组最后的数填充 temp
//举例:
//temp = {1,8, 10, 89, 1000, 1234, 0, 0} => {1,8, 10, 89, 1000, 1234, 1234, 1234,}
for(int i = high + 1; i < temp.length; i++) {
temp[i] = a[high];
}
// 使用while来循环处理,找到我们的数 key
while (low <= high) { // 只要这个条件满足,就可以找
mid = low + f[k - 1] - 1;
if(key < temp[mid]) { //我们应该继续向数组的前面查找(左边)
high = mid - 1;
//为什么是 k--
//说明
//1. 全部元素 = 前面的元素 + 后边元素
//2. f[k] = f[k-1] + f[k-2]
//因为 前面有 f[k-1]个元素,所以可以继续拆分 f[k-1] = f[k-2] + f[k-3]
//即 在 f[k-1] 的前面继续查找 k--
//即下次循环 mid = f[k-1-1]-1
k--;
} else if ( key > temp[mid]) { // 我们应该继续向数组的后面查找(右边)
low = mid + 1;
//为什么是k -=2
//说明
//1. 全部元素 = 前面的元素 + 后边元素
//2. f[k] = f[k-1] + f[k-2]
//3. 因为后面我们有f[k-2] 所以可以继续拆分 f[k-1] = f[k-3] + f[k-4]
//4. 即在f[k-2] 的前面进行查找 k -=2
//5. 即下次循环 mid = f[k - 1 - 2] - 1
k -= 2;
} else { //找到
//需要确定,返回的是哪个下标
if(mid <= high) {
return mid;
} else {
return high;
}
}
}
return -1;
}
哈希链表
public class hashTest {
public static void main(String[] args) {
People people=new People();
people.addpeople(people, 1, "小明", "男", " ");
people.addpeople(people, 2, "小美", "女", " ");
people.addpeople(people, 3, "小丽", "女", " ");
people.addpeople(people, 4, "小肖", "女", " ");
people.addpeople(people, 5, "小李", "男", " ");
people.addpeople(people, 6, "小刘", "男", " ");
people.display(people);
}
}
class People{
int no;
String name;
String sex;
String address;
People next;
public People() {
super();
}
public People(int no, String name, String sex, String address) {
super();
this.no = no;
this.name = name;
this.sex = sex;
this.address = address;
}
public void addpeople(People peo,int no,String name,String sex,String address) {
People p=peo;
while(p.next!=null) {
p=p.next;
}
People p1=new People(no,name,sex,address);
p.next=p1;
p1.next=null;
}
public void display(People peo) {
People p=peo.next;
while(p!=null) {
System.out.println(p.no+" "+p.name+" "+p.sex+" "+p.address);
p=p.next;
}
}
}
链表的可塑性非常强可自由编写方法来管理链表
班级管理
c++语言实现代码如下
#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std;
typedef struct stu{
int no;
string name;
struct stu*next;
}Student;
typedef struct Node{
int classno;
struct Node*next;
Student*student;
}ClassList;
void addClass(ClassList*cla,int no){
ClassList*p=cla;
ClassList*class_p=(ClassList*)malloc(sizeof(ClassList));
class_p->classno=no;
class_p->next=NULL;
class_p->student=new Student;
class_p->student->next=NULL;
while(p->next!=NULL){
p=p->next;
}
p->next=class_p;
}
void addStudent(ClassList*cla,int classno,int stuno,string name){
ClassList*p=cla->next;
while(p->classno!=classno){
p=p->next;
}
Student*q=p->student;
Student*stu2=new Student;
stu2->no=stuno;
stu2->name=name;
stu2->next=NULL;
while(q->next!=NULL){
q=q->next;
}
q->next=stu2;
}
int classlength(ClassList*cla){
int count=0;
ClassList*p=cla->next;
while(p!=NULL){
count++;
p=p->next;
}
return count;
}
int studentlength(Student*stu){
Student*p=stu->next;
int count=0;
while(p!=NULL){
count++;
p=p->next;
}
return count;
}
void dispList(ClassList*cla){
cout<<"总共有"<<classlength(cla)<<"个班"<<endl;
ClassList*p=cla->next;
Student*q;
while(p!=NULL){
cout<<p->classno<<"班信息如下"<<endl;
q=p->student->next;
while(q!=NULL){
cout<<"学号"<<q->no<<" 姓名"<<q->name<<endl;
q=q->next;
}
p=p->next;
}
}
int main(){
ClassList*cl;
cl=(ClassList*)malloc(sizeof(ClassList));
cl->next=NULL;
addClass(cl,1);
addClass(cl,2);
addClass(cl,3);
addClass(cl,4);
addStudent(cl,1,1001,"xiaoming");
addStudent(cl,1,1002,"xiaohong");
addStudent(cl,2,2001,"xiaogang");
addStudent(cl,3,3001,"xiaoli");
addStudent(cl,3,3002,"xiaoliu");
addStudent(cl,3,3003,"xiaowang");
addStudent(cl,4,4001,"xiaolie");
addStudent(cl,4,4002,"xiaosong");
addStudent(cl,4,4003,"xiaoyao");
addStudent(cl,4,4004,"xiaojun");
dispList(cl);
// cout<<studentlength(stu);
free(cl);
return 0;
}
基础结构结果如下