二分查找(5种方式实现二分查找),栈
1、5種方式實(shí)現(xiàn)二分查找,案例結(jié)構(gòu):
halfFind.h
#ifndef _HALFFIND_
#define _HALFFIND_
?
/************************************************************************/
/* 初始化長度為L的數(shù)組?????????????????????????????????????????????????*/
/************************************************************************/
extern void initArray(int *arr, int n);
?
/************************************************************************/
/* 打印數(shù)組????????????????????????????????????????????????????????????*/
/************************************************************************/
extern void printArr(int *arr, int n);
?
/************************************************************************/
/* 打印次數(shù)????????????????????????????????????????????????????????????*/
/************************************************************************/
extern void printTimes(int pos);
?
/************************************************************************/
/* 通過While循環(huán)的方式實(shí)現(xiàn)二分查找????????????????????????????????????????????????????????????????????*/
/************************************************************************/
extern int halfFindByWhile(int *arr, int n, int num);
?
/************************************************************************/
/* 二分查找查詢某個數(shù)值,通過do-while??????????????????????????????????? */
/************************************************************************/
extern int halfFindByDoWhile(int *arr, int n, int num);
?
/************************************************************************/
/* 二分查找,通過goto語句實(shí)現(xiàn)?????????????????????????????????????????? */
/************************************************************************/
extern int halfFindByGoto(int *arr, int n, int num);
?
/************************************************************************/
/* 通過For循環(huán)的方式實(shí)現(xiàn)二分查找??????????????????????????????????????? */
/************************************************************************/
extern int halfFindByFor(int *arr, int n, int num);
?
/************************************************************************/
/* 通過遞歸的方式進(jìn)行查找????????????????????????????????????????????????????????????????????*/
/************************************************************************/
extern int halfFindByRecursion(int *arr,int n,int num);
?
#endif
halfFuncs.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "halfFind.h"
?
/************************************************************************/
/* 初始化數(shù)組的值??????????????????????????????????????????????????????*/
/************************************************************************/
//voidinitArray(int *arr,int n)
//{
//? //時間數(shù)據(jù)類型
//? time_t ts;
//? unsigned int num = time(&ts);
//? //初始化隨機(jī)數(shù)種子
//? srand(num);
//? for (int i = 0; i < n; i++) {
//????? arr[i] = rand() % n;
//? }
//}
?
/************************************************************************/
/* 初始化數(shù)組的值??????????????????????????????????????????????????????*/
/************************************************************************/
void initArray(int *arr, int n)
{
??? for (int i = 0; i < n; i++) {
??????? arr[i] = i;
??? }
}
?
/************************************************************************/
/* 打印數(shù)組????????????? ???????????????????????????????????????????????*/
/************************************************************************/
void printArr(int *arr, int n)
{
??? for (int i = 0; i < n; i++)
??? {
??????? printf("%d ",arr[i]);
??? }
}
?
/************************************************************************/
/* 打印搜索次數(shù)????????????????????????????????????????????????????????*/
/************************************************************************/
void printTimes(int pos) {
??? printf("\nposition = %d\n",pos);
}
?
/************************************************************************/
/* 二分查找查詢某個數(shù)值,通過While表達(dá)式???????????????????????????????? */
/************************************************************************/
int halfFindByWhile(int *arr, int n, int num)
{
??? //參數(shù)分別表示開始查詢位置,結(jié)束查詢位置,中間位置
??? int start_pos = 0, end_pos = n - 1, middle_pos = 0;
?
??? while (start_pos <= end_pos)
??? {
??????? middle_pos = (start_pos + end_pos) / 2;
?
??????? //如果中間值恰好是要找的值
??? ??? if (num == arr[middle_pos])
??? ??? {
??????????? return middle_pos;
??? ??? }
??????? else if (num > arr[middle_pos])
??? ??? {
??????????? start_pos = middle_pos + 1;
??????? }
??????? else
??????? {
??????????? end_pos = middle_pos - 1;
??????? }
??? }
???
??? if (start_pos > end_pos)
??? {
??????? printf("\n沒有找到\n");
??????? return -1;
??? }
}
?
/************************************************************************/
/* 二分查找查詢某個數(shù)值,通過do-while??????????????????????????????????? */
/************************************************************************/
int halfFindByDoWhile(int *arr, int n, int num)
{
??? //參數(shù)分別表示開始查詢位置,結(jié)束查詢位置,中間位置
??? int start_pos = 0, end_pos = n - 1, middle_pos = 0;
?
??? do
??? {
??????? middle_pos = (start_pos + end_pos) / 2;
?
??????? //如果中間值恰好是要找的值
??????? if (num == arr[middle_pos])
??????? {
??????????? return middle_pos;
??????? }
??????? else if (num > arr[middle_pos])
??????? {
??? ??????? start_pos = middle_pos + 1;
??????? }
??????? else
??????? {
??????????? end_pos = middle_pos - 1;
??????? }
??? } while (start_pos <= end_pos);
???
??? if (start_pos > end_pos)
??? {
??????? printf("\n沒有找到\n");
??????? return -1;
??? }
}
?
/************************************************************************/
/* 通過goto語句查找????????????????????????????????????????????????????????????????????*/
/************************************************************************/
int halfFindByGoto(int *arr, int n, int num)
{
??? //參數(shù)分別表示開始查詢位置,結(jié)束查詢位置,中間位置
??? int start_pos = 0, end_pos = n - 1, middle_pos = 0;
???
??? flag:middle_pos = (start_pos + end_pos) / 2;
??? //如果中間值恰好是要找的值
??? if (num == arr[middle_pos])
??? {
??????? return middle_pos;
??? }
??? else if (num > arr[middle_pos])
??? {
??????? start_pos = middle_pos + 1;
??? }
??? else
??? {
??????? end_pos = middle_pos - 1;
??? }
?
??? if (start_pos <= end_pos)
??? {
??????? goto flag;
??? }
??? else
??? {
??????? printf("\n對不起,沒有找到!\n");
??????? return -1;
??? }
}
?
/************************************************************************/
/* 通過for循環(huán)的方式進(jìn)行查找??????????????????????????????????????????? */
/************************************************************************/
int halfFindByFor(int *arr, int n, int num)
{
??? //參數(shù)分別表示開始查詢位置,結(jié)束查詢位置,中間位置
??? int start_pos = 0, end_pos = n - 1, middle_pos = 0;
?
??? for (; start_pos <= end_pos;)
??? {
??????? middle_pos = (start_pos + end_pos) / 2;
??????? //如果中間值恰好是要找的值
??????? if (num == arr[middle_pos])
??????? {
??????????? return middle_pos;
??????? }
??????? else if (num > arr[middle_pos])
??????? {
??????????? start_pos = middle_pos + 1;
??????? }
??????? else
??????? {
??????????? end_pos = middle_pos - 1;
??????? }
??? }
?
??? if (start_pos > end_pos)
??? {
??????? printf("\n對不起,沒有找到!\n");
??????? return -1;
??? }
}
?
/************************************************************************/
/* 通過遞歸的方式二分查找??????????????????????????????????????????????*/
/************************************************************************/
int recursion(int *arr, int n, int num, int start_pos,int end_pos,int middle_pos)
{
??? if(start_pos <= end_pos)
??? {
??????? middle_pos = (start_pos + end_pos) / 2;
??????? //如果中間值恰好是要找的值
??????? if (num == arr[middle_pos])
??????? {
??????????? return middle_pos;
??????? }
??????? else if (num > arr[middle_pos])
??????? {
??????????? start_pos = middle_pos + 1;
??????? }
??????? else
??????? {
??????????? end_pos = middle_pos - 1;
??????? }
??? }
??? else
??? {
??????? printf("\n對不起,沒有找到!\n");
??????? return -1;
??? }
??? recursion(arr, n, num, start_pos, end_pos, middle_pos);
}
?
/************************************************************************/
/* 通過遞歸的方式進(jìn)行二分查找?????????????????????????????????????????? */
/************************************************************************/
int halfFindByRecursion(int *arr, int n, int num)
{
??? //參數(shù)分別表示開始查詢位置,結(jié)束查詢位置,中間位置
??? int start_pos = 0, end_pos = n - 1, middle_pos = 0;
????
??? //接收遞歸返回來的值
??? return recursion(arr, n, num, start_pos, end_pos, middle_pos);
}
halfFind.c
#include <stdio.h>
#include <stdlib.h>
#include "halfFind.h"
#define N 45
?
int main(int argc,char *argv[]){
??? int arr[N];
??? //times表示查找了多少次
??? //start_pos表示開始查找位置
??? //end_pos最后位置
??? //num標(biāo)識要查找的值
??? int pos;
?
??? initArray(arr,N);
??? //打印數(shù)組
??? printArr(arr, N);
???
??? //查找
??? //1、通過while的方式進(jìn)行查找
??? //pos = halfFindByWhile(arr, N, 60);
??? //2、通過do-while的方式進(jìn)行查找
??? //pos = halfFindByDoWhile(arr, N, 60);
??? //3、通過for循環(huán)的方式進(jìn)行二分查找
??? //pos = halfFindByGoto(arr,N,30);
??? //4、通過for循環(huán)的方式進(jìn)行二分查找
??? //pos = halfFindByFor(arr,N,30);
??? //5、通過遞歸的方式實(shí)現(xiàn)二分查找
??? pos = halfFindByRecursion(arr,N,60);
?
?
??? printTimes(pos);
?
??? system("pause");
??? return 0;
}
?
2、結(jié)合結(jié)構(gòu)體實(shí)現(xiàn)棧存儲結(jié)構(gòu),代碼如下:
#include <stdio.h>
#include <stdlib.h>
?
#define N? 50
struct mystack
{
??? int top;//棧頂
??? int data[N];//存放數(shù)據(jù)
};
struct mystack selfstack = { -1, { 0 } };//棧的初始化
?
//函數(shù)聲明
int isempty();?????? //1為空? 0非空
void setempty();???? //棧設(shè)置為空
int push(int num);?? //壓入數(shù)據(jù),成功1,失敗返回0
int pop();?????????? //彈出數(shù)據(jù)
?
/************************************************************************/
/* 判斷棧是否為空????????????????????????????????????????????????????????????????????*/
/************************************************************************/
int isempty()
{
??? if (selfstack.top == -1)
??? {
??????? //表示是空的
??????? return 1;
??? }
??? else
??? {
??????? //表示不是空的
??????? return 0;
??? }
}
?
/************************************************************************/
/* 將棧設(shè)置成為空??????????????????????????????????????????????????????*/
/************************************************************************/
void setempty()
{
??? selfstack.top = -1;
}
?
/************************************************************************/
/* 壓入數(shù)據(jù),成功返回1,失敗返回0?????????????????????????????????????? */
/************************************************************************/
int push(int num)
{
??? //一定要記住:要判斷棧是否溢出
??? if (selfstack.top == N - 1)
??? {
??????? //壓棧失敗
??????? return 0;
??? }
??? else
??? {
??????? selfstack.top += 1; //小標(biāo)移動一下
??????? selfstack.data[selfstack.top] = num;//壓入數(shù)據(jù)
??????? return 1;
??? }
}
?
/************************************************************************/
/* 彈出數(shù)據(jù)????????????????????????????????????????????????????????????????????*/
/************************************************************************/
int pop()
{
??? //判斷棧是否為空
??? if (selfstack.top == -1)
??? {
??????? return -1;//棧為空
??? }
??? else
??? {
??????? selfstack.top -= 1;
??????? return selfstack.data[selfstack.top + 1];//彈出的數(shù)據(jù)
??? }
}
?
int main(int argc, char *argv[])
{
??? int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
??? int i = 0;
??? for (i = 0; i < 10; i++)
??? {
??????? //填充數(shù)據(jù)
??????? push(a[i]);
??? }
??? while (!isempty())
??? {
??????? printf("%d\n", pop());//輸出數(shù)據(jù)
??? }
?
??? system("pause");
??? return 0;
}
??
?
總結(jié)
以上是生活随笔為你收集整理的二分查找(5种方式实现二分查找),栈的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 递归实现10进制转8进制,字符串数字互转
- 下一篇: 动态开辟指定数量的线程来查找动态开辟的数