二分查找(5种方式实现二分查找),栈
1、5種方式實現二分查找,案例結構:
halfFind.h
#ifndef _HALFFIND_
#define _HALFFIND_
?
/************************************************************************/
/* 初始化長度為L的數組?????????????????????????????????????????????????*/
/************************************************************************/
extern void initArray(int *arr, int n);
?
/************************************************************************/
/* 打印數組????????????????????????????????????????????????????????????*/
/************************************************************************/
extern void printArr(int *arr, int n);
?
/************************************************************************/
/* 打印次數????????????????????????????????????????????????????????????*/
/************************************************************************/
extern void printTimes(int pos);
?
/************************************************************************/
/* 通過While循環的方式實現二分查找????????????????????????????????????????????????????????????????????*/
/************************************************************************/
extern int halfFindByWhile(int *arr, int n, int num);
?
/************************************************************************/
/* 二分查找查詢某個數值,通過do-while??????????????????????????????????? */
/************************************************************************/
extern int halfFindByDoWhile(int *arr, int n, int num);
?
/************************************************************************/
/* 二分查找,通過goto語句實現?????????????????????????????????????????? */
/************************************************************************/
extern int halfFindByGoto(int *arr, int n, int num);
?
/************************************************************************/
/* 通過For循環的方式實現二分查找??????????????????????????????????????? */
/************************************************************************/
extern int halfFindByFor(int *arr, int n, int num);
?
/************************************************************************/
/* 通過遞歸的方式進行查找????????????????????????????????????????????????????????????????????*/
/************************************************************************/
extern int halfFindByRecursion(int *arr,int n,int num);
?
#endif
halfFuncs.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "halfFind.h"
?
/************************************************************************/
/* 初始化數組的值??????????????????????????????????????????????????????*/
/************************************************************************/
//voidinitArray(int *arr,int n)
//{
//? //時間數據類型
//? time_t ts;
//? unsigned int num = time(&ts);
//? //初始化隨機數種子
//? srand(num);
//? for (int i = 0; i < n; i++) {
//????? arr[i] = rand() % n;
//? }
//}
?
/************************************************************************/
/* 初始化數組的值??????????????????????????????????????????????????????*/
/************************************************************************/
void initArray(int *arr, int n)
{
??? for (int i = 0; i < n; i++) {
??????? arr[i] = i;
??? }
}
?
/************************************************************************/
/* 打印數組????????????? ???????????????????????????????????????????????*/
/************************************************************************/
void printArr(int *arr, int n)
{
??? for (int i = 0; i < n; i++)
??? {
??????? printf("%d ",arr[i]);
??? }
}
?
/************************************************************************/
/* 打印搜索次數????????????????????????????????????????????????????????*/
/************************************************************************/
void printTimes(int pos) {
??? printf("\nposition = %d\n",pos);
}
?
/************************************************************************/
/* 二分查找查詢某個數值,通過While表達式???????????????????????????????? */
/************************************************************************/
int halfFindByWhile(int *arr, int n, int num)
{
??? //參數分別表示開始查詢位置,結束查詢位置,中間位置
??? 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;
??? }
}
?
/************************************************************************/
/* 二分查找查詢某個數值,通過do-while??????????????????????????????????? */
/************************************************************************/
int halfFindByDoWhile(int *arr, int n, int num)
{
??? //參數分別表示開始查詢位置,結束查詢位置,中間位置
??? 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)
{
??? //參數分別表示開始查詢位置,結束查詢位置,中間位置
??? 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循環的方式進行查找??????????????????????????????????????????? */
/************************************************************************/
int halfFindByFor(int *arr, int n, int num)
{
??? //參數分別表示開始查詢位置,結束查詢位置,中間位置
??? 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);
}
?
/************************************************************************/
/* 通過遞歸的方式進行二分查找?????????????????????????????????????????? */
/************************************************************************/
int halfFindByRecursion(int *arr, int n, int num)
{
??? //參數分別表示開始查詢位置,結束查詢位置,中間位置
??? 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標識要查找的值
??? int pos;
?
??? initArray(arr,N);
??? //打印數組
??? printArr(arr, N);
???
??? //查找
??? //1、通過while的方式進行查找
??? //pos = halfFindByWhile(arr, N, 60);
??? //2、通過do-while的方式進行查找
??? //pos = halfFindByDoWhile(arr, N, 60);
??? //3、通過for循環的方式進行二分查找
??? //pos = halfFindByGoto(arr,N,30);
??? //4、通過for循環的方式進行二分查找
??? //pos = halfFindByFor(arr,N,30);
??? //5、通過遞歸的方式實現二分查找
??? pos = halfFindByRecursion(arr,N,60);
?
?
??? printTimes(pos);
?
??? system("pause");
??? return 0;
}
?
2、結合結構體實現棧存儲結構,代碼如下:
#include <stdio.h>
#include <stdlib.h>
?
#define N? 50
struct mystack
{
??? int top;//棧頂
??? int data[N];//存放數據
};
struct mystack selfstack = { -1, { 0 } };//棧的初始化
?
//函數聲明
int isempty();?????? //1為空? 0非空
void setempty();???? //棧設置為空
int push(int num);?? //壓入數據,成功1,失敗返回0
int pop();?????????? //彈出數據
?
/************************************************************************/
/* 判斷棧是否為空????????????????????????????????????????????????????????????????????*/
/************************************************************************/
int isempty()
{
??? if (selfstack.top == -1)
??? {
??????? //表示是空的
??????? return 1;
??? }
??? else
??? {
??????? //表示不是空的
??????? return 0;
??? }
}
?
/************************************************************************/
/* 將棧設置成為空??????????????????????????????????????????????????????*/
/************************************************************************/
void setempty()
{
??? selfstack.top = -1;
}
?
/************************************************************************/
/* 壓入數據,成功返回1,失敗返回0?????????????????????????????????????? */
/************************************************************************/
int push(int num)
{
??? //一定要記住:要判斷棧是否溢出
??? if (selfstack.top == N - 1)
??? {
??????? //壓棧失敗
??????? return 0;
??? }
??? else
??? {
??????? selfstack.top += 1; //小標移動一下
??????? selfstack.data[selfstack.top] = num;//壓入數據
??????? return 1;
??? }
}
?
/************************************************************************/
/* 彈出數據????????????????????????????????????????????????????????????????????*/
/************************************************************************/
int pop()
{
??? //判斷棧是否為空
??? if (selfstack.top == -1)
??? {
??????? return -1;//棧為空
??? }
??? else
??? {
??????? selfstack.top -= 1;
??????? return selfstack.data[selfstack.top + 1];//彈出的數據
??? }
}
?
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++)
??? {
??????? //填充數據
??????? push(a[i]);
??? }
??? while (!isempty())
??? {
??????? printf("%d\n", pop());//輸出數據
??? }
?
??? system("pause");
??? return 0;
}
??
?
總結
以上是生活随笔為你收集整理的二分查找(5种方式实现二分查找),栈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 递归实现10进制转8进制,字符串数字互转
- 下一篇: 105级鬼泣史诗套装选择(鬼泣远古传说套