先对数组排序,在进行折半查找(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;
}
先对数组排序,在进行折半查找(C++)的更多相关文章
- 折半查找,binarySearch
折半查找法也称为二分查找法,它充分利用了元素间的次序关系,采用分治策略,可在最坏的情况下用O(log n)完成搜索任务.它的基本思想是,将n个元素分成个数大致相同的两半,取a[n/2]与欲查找的x作比 ...
- 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找
今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...
- 折半查找(java)(边学习边更新)
---恢复内容开始--- class ArrayTest3 { public static void main(String[] args) { //int [] arr=new int[]{54,4 ...
- C语言之实现函数返回一个数组,以及选择排序,还有折半查找。这是同学的一个作业。。。
作业的具体要求如下: 编写一个完整的程序,实现如下功能.(1) 输入10个无序的整数.(2) 用选择排序法将以上接收的10个无序整数按从大到小的顺序排序.(3) 要求任意输入一个整数 ...
- c语言折半查找
折半查找又称为二分查找,它的前提是线性表中的记录必须是有序的(通常从小到大有序),线性表必须采用顺序存储. 折半查找的基本思想是 : 在有序表中,取中间记录作为比较对象,若给定值与中间记录的关键字相等 ...
- 折半查找&clock函数
#include <stdio.h>#include <time.h> #define CLOCKS_PER_SEC ((clock_t)1000) int binsearch ...
- 二分查找or折半查找
package com.gxf.search; /** * 测试折半查找or二分查找 * @author xiangfei * */ public class BiSearch { /** * 非递归 ...
- Java 实现折半查找
package search; import java.util.*; /*折半查找要求线性表是有序的,假设递增 * 基本思路:R[low...high]是当前的查找区间,首先确定中间位置mid=(l ...
- c - 折半查找(二分法检索)
#include <stdio.h> #define LEN 10 /* 折半查找(二分法检索). */ int index_of(int *a, int k) { ; ; int m; ...
随机推荐
- 尚学堂Java面试题整理
博客分类: 经典分享 1. super()与this()的差别? - 6 - 2. 作用域public,protected,private,以及不写时的差别? - 6 - 3. 编程输出例如以 ...
- 天体程序猿叹息——变化hosts对
没有解释更新一SDK像贼,我真诚地希望在天上Android.ios我们已经禁止了.要玩不能玩才最寂寞 安装成功不寂寞hosts文件: 74.125.237.1 dl-ssl.google.com173 ...
- LeetCode——Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 安卓Monkey源码分析之运行流程
在<MonkeyRunner源码分析之与Android设备通讯方式>中,我们谈及到MonkeyRunner控制目标android设备有多种方法,其中之一就是在目标机器启动一个monkey服 ...
- codeforces #262 DIV2 C称号Present(二分法+贪婪)
职务地址:http://codeforces.com/contest/460/problem/C 这个题是用二分枚举最小值.然后推断是否能在规定的次数内使得全部的数都达到这个值.推断的时候要用贪心的方 ...
- Visual Studio 单元测试之三---压力测试
原文:Visual Studio 单元测试之三---压力测试 我们都知道大名鼎鼎的LoadRuner,但是很少有人知道Visual Studio自带的Test也可以做些简单的压力测试,下面我们就介绍一 ...
- leetcode第二题--Median of Two Sorted Arrays
Problem:There are two sorted arrays A and B of size m and n respectively. Find the median of the two ...
- 对于发Github的contributions贡献不会增加
最近发现每天在 Github 做代码提交,可是 contributions 的面板(贡献图)上的绿点(即贡献值)却没有增长了.擦~ 有两个礼拜了. 例如以下图并且.同一时候发现曾经的绿点也是稀稀拉拉的 ...
- ADFS 2.0 配置简介 PartⅡ – 配置 ADFS 信任关系
ADFS 与应用程序间的各种验证是基于信任关系的,在 ADFS 服务器配置好要信赖的应用程序(以 URL 为标识)后,应用程序再通过指定认证服务器来将用户引导至 ADFS 登录页,登录完成后再将用户的 ...
- 个人总结js客户端验证
//郭泽峰个人总结总结(2012-12-5): //备注:当 regu是字符串时应示例对象RegExp,否则的话 var emailReg =/在此加上正则/ //验证邮箱 function Chec ...