栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构9

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构9

查找算法
  • 二分法查找
    • 实现方法
  • 插值查找
    • 实现方法
  • 斐波那契查值
  • 哈希链表
    • 班级管理
      • c++语言实现代码如下

二分法查找 实现方法
	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(midvalfindVal){
			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
#include 
#include
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<<"总共有"<next;
	Student*q;
	while(p!=NULL){
		cout<classno<<"班信息如下"<student->next;
		while(q!=NULL){
			cout<<"学号"<no<<" 姓名"<name<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< 

基础结构结果如下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号