博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#数据结构和算法[Basic Searching Algorithms]
阅读量:2504 次
发布时间:2019-05-11

本文共 1311 字,大约阅读时间需要 4 分钟。

 

 

 

Searching for data is a fundamental computer programming task and one

that has been studied for many years. This chapter looks at just one aspect of
the search problem—searching for a given value in a list (array).
There are two fundamental ways to search for data in a list: the sequential
search and the binary search. Sequential search is used when the items in the
list are in random order; binary search is used when the items are sorted in
the list.

搜索数据是计算机程序的根本任务,也是被研究了很多年的课题。

本章看一下搜索问题的某一个方面--搜索给定大小的数组。

这里有两种方法:顺序查找和二分查找。当数组里的数据是乱序时用顺序查找,排好序时用二分。

SEQUENTIAL SEARCHING

The most obvious type of search is to begin at the beginning of a set of
records and move through each record until you find the record you are
looking for or you come to the end of the records. This is called a sequential
search.
A sequential search (also called a linear search) is very easy to implement.
Start at the beginning of the array and compare each accessed array element
to the value you’re searching for. If you find a match, the search is over. If you
get to the end of the array without generating a match, then the value is not
in the array.

Here is a function that performs a sequential search:

顺序查找

是从数据集地开头挨个地搜索,直到找到你想要的。顺序查找也叫线性查找,是一种非常容易实现的算法。

从数组开头依次比较每一具元素的值是不是想要的,如果找到了,查找结束。如果数组遍历完了都找不到,那说明值不在数组里。

以下是代码实现:

 

 

转载地址:http://malgb.baihongyu.com/

你可能感兴趣的文章
[luogu_P2045]方格取数加强版
查看>>
android 代理模式创建Activity
查看>>
线性素数筛模板+理解
查看>>
第六次作业
查看>>
某图像分析系统
查看>>
c++课程设计之菜单选择\\
查看>>
iOS 的 XMPPFramework 简介
查看>>
hdu 3555 数位dp入门
查看>>
Git学习系列-Git基本概念
查看>>
c#多个程序集使用app.config 的解决办法
查看>>
模仿网站登录注册
查看>>
Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)
查看>>
Linux下获取本机IP地址的代码
查看>>
2014的几首小诗
查看>>
(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误
查看>>
flex布局
查看>>
CGroup 介绍、应用实例及原理描述
查看>>
python-----python的文件操作
查看>>
python数据结构与算法之单链表
查看>>
ActiveX控件
查看>>