C++ 先对数组排序,在进行折半查找_C 语言

第一步:输入15个整数

第二步:对这15个数进行排序

第三部:输入一个数,在后在排好序的数中进行折半查找,判断该数的位置

实现代码如下:

方法一:

选择排序法+循环折半查找法

复制代码 代码如下:

#include<iostream>
using namespace std;
int main(){
 int a[15];
 int n,i;
 void array_sort(int a[], int n);
 int zeban(int a[], int start ,int end,int n);
 cout<<"Please input 15 numbers:"<<endl;
 for(i=0;i<15;i++){
  cin>>a[i];
 }
 cout<<"Sorted order:"<<endl;
 //==============选择排序========
 array_sort(a,15);
 //=======输出排序完成的数组====
    for(i=0;i<15;i++){
  cout<<a[i]<<" ";
  }
 cout<<endl;
 cout<<"please input a number:";
 cin>>n;
 //================折半查找==========
 cout<<endl;
 cout<<"number "<<n<<" locate in "<<zeban(a,0,14,n)<<endl;
 return 0;
}
void array_sort(int a[],int n){
 int i,j,k,tool;
     for(i=0;i<n;i++){
  k=i;
  for(j=(i+1);j<n;j++){
  if(a[j]<a[k]){
     k=j;
        }
  }
  tool=a[i];
  a[i]=a[k];
  a[k]=tool;
  }
}
int zeban(int a[],int start,int end,int n){
 int tag=-1;
 for(start=0,end=14;start<=end;){
  if(n==a[(start+end)/2]){
   tag=(start+end)/2+1;
   return tag;
  }else if(n<a[(start+end)/2]){
   end=(start+end)/2;
  }else if(n>a[(start+end)/2]){
   start=(start+end)/2;
  }
 }
}

第二种方法:

冒泡排序法+递归折半查找法

复制代码 代码如下:

#include<iostream>
using namespace std;
int main(){
 int a[15];
 int n,i;
 void array_sort(int a[], int n);
    int IterBiSearch(int data[], const int x, int beg, int last);
 cout<<"Please input 15 numbers:"<<endl;
 for(i=0;i<15;i++){
  cin>>a[i];
 }
 cout<<"Sorted order:"<<endl;
 //==============选择排序========
 array_sort(a,15);
 //=======输出排序完成的数组====
    for(i=0;i<15;i++){
  cout<<a[i]<<" ";
  }
 cout<<endl;
 cout<<"please input a number:";
 cin>>n;
 //================折半查找==========
 cout<<endl;
 cout<<"number "<<n<<" locate in "<<IterBiSearch(a,n, 0, 14)<<endl;
 return 0;
}
void array_sort(int a[],int n){
 int i,j,tool;
     for(i=0;i<n;i++){
     for(j=0;j<(n-i-1);j++){
        if(a[j]>a[j+1]){
           tool=a[j];
           a[j]=a[j+1];
           a[j+1]=tool;
        }
     }
    }
}
int IterBiSearch(int data[], const int x, int beg, int last) 

    int mid = -1; 
    mid = (beg + last) / 2; 
    if (x == data[mid]) 
    { 
        return (mid+1); 
    } 
    else if (x < data[mid]) 
    { 
        return IterBiSearch(data, x, beg, mid - 1); 
    } 
    else if (x > data[mid]) 
    { 
        return IterBiSearch(data, x, mid + 1, last); 
    } 
    return -1; 

时间: 2024-07-28 14:53:04

C++ 先对数组排序,在进行折半查找_C 语言的相关文章

浅析直接插入排序与折半插入排序_C 语言

首先看一下例子,将数据一个个的插入到一个列表中,插入后这个列表就排序好了 注意:这个列表是递增的,而且内存空间已分配好,只是没有填充真正的数据,如下代码: 复制代码 代码如下: int InsertSort(MergeType *L, int data){ int j;  if (!L->len) {  L->elem[++L->len] = data;  return 0; }  for (j = L->len-1; j >= 0; --j) {  if (data <

C语言之单链表的插入、删除与查找_C 语言

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.要实现对单链表中节点的插入.删除与查找的功能,就要先进行的单链表的初始化.创建和遍历,进而实现各功能,以下是对单链表节点的插入.删除.查找功能的具体实现: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef int ElemType; /** *链表通用类型 *ElemType 代表自定义的数据类型 *struct

二叉查找树的插入,删除,查找_C 语言

二叉查找树是满足以下条件的二叉树:1.左子树上的所有节点值均小于根节点值,2.右子树上的所有节点值均不小于根节点值,3.左右子树也满足上述两个条件. 二叉查找树的插入过程如下:1.若当前的二叉查找树为空,则插入的元素为根节点,2.若插入的元素值小于根节点值,则将元素插入到左子树中,3.若插入的元素值不小于根节点值,则将元素插入到右子树中. 二叉查找树的删除,分三种情况进行处理:1.p为叶子节点,直接删除该节点,再修改其父节点的指针(注意分是根节点和不是根节点),如图a. 2.p为单支节点(即只有

折半查找

//****************************************** //折半查找---传进来的表是经过排序后的有序表 // //******************************************* void Search_Bin ( SSTable ST, string key ) { int low , mid,index, high ; // 置区间初值 low=1; index=0; high=ST.length; while (low<=high)

折半查找(binary search) 算法示例

折半查找, 又称二分查找(binary search), 需要数组有序(sort), 通过比较数组的中间数据(中心偏向较小的方法), 确定查找值的范围; 直到中值等于查找值, 则查找成功; 如果未成功, 则重置数据, 判断首尾位置的大小, 再进行中值比较; 判断失败, 则数据不存在; 注意: 1. Eclipse无法重定向(redirect)输入文件(file), 只能读入数据; 2. 使用cmd重定向输入文件, 则需要解压"stdlib.jar", 取出相应的class(In, Ou

c++-折半查找程序,输入后就崩溃,谁能帮忙看一下

问题描述 折半查找程序,输入后就崩溃,谁能帮忙看一下 #include #include using namespace std; int half(int,vector ); int low=0; int high=7; int main(){ int b[10]={1,10,15,17,18,21,22,35,54,65}; vector a(b,b+10); cout<<"请输入要査找的数:"; int number; cin>>number; if(ha

数据结构例程——线性表的折半查找

本文是[数据结构基础系列(8):查找]中第3课时[线性表的折半查找]的例程. 折半查找 #include <stdio.h> #define MAXL 100 typedef int KeyType; typedef char InfoType[10]; typedef struct { KeyType key; //KeyType为关键字的数据类型 InfoType data; //其他数据 } NodeType; typedef NodeType SeqList[MAXL]; //顺序表类

指针-这道题可以用折半查找的办法做吗?麻烦大神帮忙解答一下吧!!!谢谢!!!

问题描述 这道题可以用折半查找的办法做吗?麻烦大神帮忙解答一下吧!!!谢谢!!! 有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中, 插入之后依然有序.指针实现 解决方案 插入数组意味着需要将数组插入位置后面的元素全部搬动一次,相当低效. 这里最好使用链表,当然更好的是使用二叉排序树. 解决方案二: 非要这么做也可以,参考 http://blog.csdn.net/xiaofeige567/article/details/26879075 文章是用的顺序查找,按照你说的折半查

我真的不会-我是初学,一个关于顺序查找和折半查找的算法有错,求解答

问题描述 我是初学,一个关于顺序查找和折半查找的算法有错,求解答 ```#include #define Max 256 typedef struct Keylist { int key[Max]; int len; }Keylist; void creatKlist(Keylist L) { int i=0; printf("**建立静态表**n"); printf("你需要构建多少个数据,请输入:"); scanf("%d",&L.l