数据结构---五大排序---哈希表---二分查找法

2024-09-02 21:44

本文主要是介绍数据结构---五大排序---哈希表---二分查找法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、五大排序

1.1.冒泡排序 

1.2.选择排序

1.3.插入排序

1.4.希尔排序

1.5.快速排序

二、哈希表

2.1.哈希表结构的定义

2.2.初始化哈希表

2.3.插入元素

2.4.打印哈希表

2.5.查找元素

2.6.销毁哈希表

三、二分查找法(折半查找法)


一、五大排序

1.1.冒泡排序 

时间复杂度:o(n^2)

稳定性:稳定

int BubbleSort(int *pArray, int MaxLen)
{int j = 0;int i = 0;int temp = 0;for (j = 0; j < MaxLen-1; j++){for (i = 0; i < MaxLen-1-j; i++){if (pArray[i] > pArray[i+1]){temp = pArray[i];pArray[i] = pArray[i+1];pArray[i+1] = temp;}}}return 0;
}

1.2.选择排序

时间复杂度:o(n^2)

稳定性:不稳定

int SelectSort(int *pArray, int MaxLen)
{   int Min = 0;int Temp = 0;int i = 0;int j = 0;for (j = 0; j < MaxLen-1; j++){Min = j;for (i = j+1; i < MaxLen; i++){if (pArray[i] < pArray[Min]){Min = i;}}if (Min != j){Temp = pArray[j];pArray[j] = pArray[Min];pArray[Min] = Temp;}}return 0;
}

1.3.插入排序

 时间复杂度 :O(n^2)    已经有序的数据使用插入排序时间复杂度为:O(n)

稳定性:稳定

int InsertSort(int *pArray, int MaxLen)
{int i = 0;int j = 0;int Temp = 0;for (j = 1; j < MaxLen; j++){Temp = pArray[j];for (i = j; i > 0 && Temp < pArray[i-1]; i--){pArray[i] = pArray[i-1];}pArray[i] = Temp;}return 0;
}

1.4.希尔排序

时间复杂度:O(nlogn) 

稳定性:不稳定

int ShellSort(int *pArray, int MaxLen)
{int step = 0;int i = 0;int j = 0;int temp = 0;for (step = MaxLen / 2; step > 0; step /= 2){for (j = step; j < MaxLen; j++){temp = pArray[j];for (i = j; i >= step && temp < pArray[i-step]; i -= step){pArray[i] = pArray[i-step];}pArray[i] = temp;}}return 0;
}

1.5.快速排序

时间复杂度:O(nlogn) 

稳定性:不稳定

int QuickSort(int *pArray, int Low, int High)
{int Key = 0;int j = High;int i = Low;Key = pArray[Low];while (i < j){while (i < j && pArray[j] >= Key){j--;}pArray[i] = pArray[j];while (i < j && pArray[i] <= Key){i++;}pArray[j] = pArray[i];}pArray[i] = Key;if (i-1 > Low){QuickSort(pArray, Low, i-1);}if (i+1 < High){QuickSort(pArray, i+1, High);}return 0;
}

二、哈希表

2.1.哈希表结构的定义

#define INDEX   10struct list_head hashtable[INDEX];typedef struct Data
{struct list_head node;int data;
}Data_t;

2.2.初始化哈希表

int InitHashTable(void)
{int i = 0;for (i = 0; i < INDEX; i++){INIT_LIST_HEAD(&hashtable[i]);}return 0;
}

2.3.插入元素

//按照顺序插入
int compare(struct list_head *pNewNode, struct list_head *pTmpNode)
{if (NULL == pTmpNode){return 1;}return list_entry(pNewNode, Data_t, node)->data - list_entry(pTmpNode, Data_t, node)->data;
}//插入哈希表
int InsertHashTable(int Num)
{int index = 0;Data_t *pNewNode = NULL;//申请节点 pNewNode = malloc(sizeof(Data_t));if (NULL == pNewNode){return -1;}pNewNode->data = Num;//获得插入数据的键值index = Num % INDEX;//插入数据list_add_order(&pNewNode->node, &hashtable[index], compare);return 0;
}

2.4.打印哈希表

int ShowHashTable(void)
{int index = 0;Data_t *pData = NULL;for (index = 0; index < INDEX; index++){printf("%d:", index);list_for_each_entry(pData, &hashtable[index], node){printf("%d ", pData->data);}printf("\n");}return 0;
}

2.5.查找元素

int FindHashTable(int Num)
{int index = 0;Data_t *pData = NULL;int ret = 0;index = Num % INDEX;list_for_each_entry(pData, &hashtable[index], node){if (pData->data == Num){ret = 1;break;}if (pData->data > Num){ret = 0;break;}}return ret;
}

2.6.销毁哈希表

int DestroyHashTable()
{int i = 0;Data_t *pTmpNode = NULL;Data_t *pNextNode = NULL;for (i = 0; i < INDEX; i++){list_for_each_entry_safe(pTmpNode, pNextNode, &hashtable[i], node){free(pTmpNode);}}return 0;
}

三、二分查找法(折半查找法)

只适合顺序表

折半查找的基本思想:首先将给定值key与表中中间位置的元素(mid的指向元素)比较。mid=low+high/2(向下取整)

若key与中间元素相等,则查找成功,返回该元素的存储位置,即mid;
若key与中间元素不相等,则所需查找的元素只能在中间元素以外的前半部分或后半部分。(至于是前半部分还是后半部分要看key与mid所指向元素的大小关系)
a.在查找表升序排列的情况下,若给定值key大于中间元素则所查找的元素只可能在后半部分。此时让low=mid+1;
b.若给定值key小于中间元素则所查找的元素只可能在前半部分。此时让high=mid-1;

int MidSearch(int *pArray, int Low, int High, int tmpdata)
{int Mid = 0;if (Low > High){return -1;}Mid = (Low + High) / 2;if (tmpdata < pArray[Mid]){return MidSearch(pArray, Low, Mid-1, tmpdata);}else if (tmpdata > pArray[Mid]){return MidSearch(pArray, Mid+1, High, tmpdata);}else if (tmpdata == pArray[Mid]){return Mid;}
}

这篇关于数据结构---五大排序---哈希表---二分查找法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1131152

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

hdu 1285(拓扑排序)

题意: 给各个队间的胜负关系,让排名次,名词相同按从小到大排。 解析: 拓扑排序是应用于有向无回路图(Direct Acyclic Graph,简称DAG)上的一种排序方式,对一个有向无回路图进行拓扑排序后,所有的顶点形成一个序列,对所有边(u,v),满足u 在v 的前面。该序列说明了顶点表示的事件或状态发生的整体顺序。比较经典的是在工程活动上,某些工程完成后,另一些工程才能继续,此时

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c