c语言折半查找输出坐标,数据结构(C语言版)——有序表查找(折半查找)(代码版)...
數據結構(C語言版)——有序表查找(折半查找)(代碼版)
數據結構(C語言版)——有序表查找(折半查找)(代碼版)
#include
#include
#define ERROR 0
#define OK 1
#define MAXSIZE 20
typedef int Status;
Status binarySearch(int arr[],int arrLenght,int wantSearchElement);
int main(int argc, char *argv[]) {
int data[MAXSIZE],size,i,wantSearch,result;
printf("請輸入初始化數組大小(1-%d):",MAXSIZE);
scanf("%d",&size);
printf("請輸入數組元素(空格隔開):");
for(i=1;i<=size;i++)
{
if(scanf("%d",&data[i])==0)
{
printf("輸入錯誤\n");
fflush(stdin);
return ERROR;
}
}
printf("請輸入你要查找的內容:");
if(scanf("%d",&wantSearch)==0)
{
printf("輸入錯誤\n");
fflush(stdin);
return ERROR;
}
if((result=binarySearch(data,size,wantSearch))==ERROR)
{
printf("你要查找的元素不在數組中\n");
return ERROR;
}
printf("元素在數組中%d位置\n",result);
return 0;
}
Status binarySearch(int arr[],int arrLenght,int wantSearchElement)
{
int low=1,hight=arrLenght,mid;
while(hight>=low)
{
mid=(low+hight)/2;
if(arr[mid]>wantSearchElement)
{
hight=mid-1;
}
else if(arr[mid]
{
low=mid+1;
}else
{
return mid;
}
}
return ERROR;
}
(一)、核心代碼
Status binarySearch(int arr[],int arrLenght,int wantSearchElement)
{
int low=1,hight=arrLenght,mid;
while(hight>=low)
{
mid=(low+hight)/2;//獲取數組下標中間值
if(arr[mid]>wantSearchElement)
{//如果數組中間值,大于wantSearchElement
hight=mid-1;
}
else if(arr[mid]
{
low=mid+1;
}else
{
return mid;
}
}
return ERROR;
}
數據結構(C語言版)——有序表查找(折半查找)(代碼版)相關教程
數據結構(3):隊列的原理和實現
數據結構(3):隊列的原理和實現 完整代碼拉到最底下 隊列顧名思義就像我們生活中排隊一樣,先進先出。 如上圖所示,25、16、5、9依次在隊列中,按照順序拿出的數據也分別是25、26、5、9。 底層使用數組來實現,實現的功能有插入數據到隊尾、移除隊首數據、
數據結構(C語言版)——有序表查找(插值查找)(代碼版)
數據結構(C語言版)——有序表查找(插值查找)(代碼版) #include stdio.h#include stdlib.h#define ERROR 0#define OK 1#define MAXSIZE 20typedef int Status;Status binarySearch(int arr[],int arrLenght,int wantSearchElement); int main(int argc, char *
67道數據結構題-劍指offer-二叉樹
67道數據結構題-劍指offer-二叉樹 先補習一下二叉樹的知識吧(2020-10-3) 二叉樹是n個節點構成的集合,可以為空樹也可以為非空樹/*空樹就沒有結點,非空樹起碼一個節點空樹*/只有一個節點/*只有一個父親*/二叉樹的獨有特性: 1. 每個結點至多/*注意是至多*/
六十七.深度優先遍歷C語言實現(有向圖)
六十七.深度優先遍歷C語言實現(有向圖) #includestdio.hint min = 9999999,book[101],n,e[101][101]; //假設9999999為正無窮 void dfs(int cur,int dis) //cur是當前所在的頂點編號,dis是當前已經走過的路程 {int j; if(dismin) //如果當前走過的路程已經
【數據結構】圖的深度優先遍歷
【數據結構】圖的深度優先遍歷 以無向圖為例 鄰接矩陣,該鄰接矩陣縱坐標(i)為訪問的起點,而橫坐標(j)代表訪問終點 (1)起點為2,先將Visited數組(Bool)當中,表示為2的結點,標記為1,代表已經訪問 (2)查看縱坐標i=2的行,其中橫坐標j=1的列,發現
數據結構(C語言版)——有序表查找(斐波那契查找)(代碼版)
數據結構(C語言版)——有序表查找(斐波那契查找)(代碼版) #include stdio.h#include stdlib.h#define ERROR 0#define OK 1#define MAXSIZE 20typedef int Status; Status fbi(int number);Status fbiSearch(int fbiArr[],int fbiArrLenght,int dataArr[],int
【leetcode千題】21. 合并兩個有序鏈表
【leetcode千題】21. 合并兩個有序鏈表 將兩個升序鏈表合并為一個新的 升序 鏈表并返回。新鏈表是通過拼接給定的兩個鏈表的所有節點組成的。 示例: 輸入:1-2-4, 1-3-4 輸出:1-1-2-3-4-4 思路:直接一個個遍歷唄 # Definition for singly-linked list.# cla
C語言第8題:變量作用閾
C語言第8題:變量作用閾 #includestdio.hint a = 20;//全局變量void test1(){int a1 = 0;int a = 100;}void test2(){int a2 = 100;}int main(){int a = 0;a = 10;system(pause);return 0;} 文件中的變量 extern int a ; 出現在括號外面的變量就是全局變量 不
總結
以上是生活随笔為你收集整理的c语言折半查找输出坐标,数据结构(C语言版)——有序表查找(折半查找)(代码版)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 遍历strs数组,并判断数组中每一个元素
- 下一篇: html5 自定义 datepicker