Search

The Sequential Search

To be able to explain and implement sequential search and binary search.
To be able to explain and implement selection sort, bubble sort, merge sort, quick sort, insertion sort, and shell sort.

In Python, there is a very easy way to ask whether an item is in a list of items. We use the in operator.

15 in [3,5,2,4,1]
False
3 in [3,5,2,4,1]
True

sequential search

Starting at the first item in the list, we simply move from item to item, following the underlying sequential ordering until we either find what we are looking for or run out of items. If we run out of items, we have discovered that the item we were searching for was not present.

def sequentialSearch(alist,item):

    pos=0#start
    found=False

    while pos<len(alist)and not found:
        if alist[pos]==item:
            found=True
        else:
            pos=pos+1

    return found

#testing
testlist=[1,2,3,4,5,6]
print(sequentialSearch(testlist,6))
print(sequentialSearch(testlist,7))

这里写图片描述

最好的情况是要找的元素是第一个,最不好的情况是最后一个。

How about search in an ordered list?

Assume that the list of items was constructed so that the items were in ascending order, from low to high

def orderedSequentialSearch(alist,item):

    pos=0#指针

    found=False
    stop=False

    while pos<len(alist) and not found and not stop:
        #当指针小于List的长度,并且,没有找到,也没有停止-》循环
        if alist[pos]==item:#找到
            found=True
        else:
            if alist[pos]>item:#如果没找到,
            # 并且指针指的元素,大于所要寻找的元素, 停止寻找
                stop=True#停止搜索
            else:
                pos=pos+1 #如果不是上个情况,找下一个
    return found

#testing
testlist=[1,2,3,4,5,6]
print(orderedSequentialSearch(testlist,6))
print(orderedSequentialSearch(testlist,7))

这里写图片描述

在有序列表查找元素,如果找到了就停止搜索。如果没有找到,并且列表元素大于寻找的元素,停止。没有必要搜寻完整个列表。

问题:
Q-51: Suppose you are doing a sequential search of the ordered list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18]. How many comparisons would you need to do in order to find the key 13? 7个

The Binary Search

a binary search will start by examining the middle item.
If that item is the one we are searching for, we are done.
If it is not the correct item, we can use the ordered nature of the list to eliminate half of the remaining items.
If the item we are searching for is greater than the middle item, we know that the entire lower half of the list as well as the middle item can be eliminated from further consideration.
The item, if it is in the list, must be in the upper half.

__author__ = 'jenny'

def binarySearch(alist,item):
    first=0
    last=len(alist)
    found=False

    while first<=last and not found:
        midpoint=(first+last)//2 #把list从中间分为2个部分
        #这里的First and last ,midpoint都是动态变化的
        if alist[midpoint]==item:#如果LIST中间位置的元素为所找元素
            found=True#找到
        else:
            if item<alist[midpoint]:#如果所找元素小于列表的中间元素
                last=midpoint-1#那中位为最后一位,无需找后面的元素
            else:
                first=midpoint#如果不是,第一位为中间,从后半部分查找,前面没有必要找了
    return found

#testing
testlist=[1,2,3,4,5,6]
print(binarySearch(testlist,6))
print(binarySearch(testlist,7))

Divide and conquer means that we divide the problem into smaller pieces, solve the smaller pieces in some way, and then reassemble the whole problem to get the result.

Recursive binary search algorithm

def binarySearchRec(alist,item):
    if len(alist)==0: #当链表为空时
        return False
    else:
        midpoint=len(alist)//2#找到中点
        if alist[midpoint]==item:#如果是中间的元素为所要找的元素
            return True
        else:
            if item<alist[midpoint]:#所找元素小于列表中点元素
                return binarySearchRec(alist[:midpoint],item)
                #递归
                #从中点前找
            else:
                return binarySearchRec(alist[midpoint+1:],item)
                #递归
                #从中点后找


#testing
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(binarySearchRec(testlist, 3))

当查找元素为3的时候
1. midpoint.index=4->没有找到,再前半部REC
2. midpoint.index=2->没有找到,再在前半部REC
3. midpoint.index=0->没有找到,再在前半部REC
4. list 为空,返回FALSE

The maximum number of comparisons is logarithmic with respect to the number of items in the list. Therefore, the binary search is O(logn).

Q-40: Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using the recursive binary search algorithm. Which group of numbers correctly shows the sequence of comparisons used to find the key 8.
12, 6, 11, 8

midpoint=len(alist)//2 得到的是INDEX,
list index 从0开始

总结:
从一个列表中找元素,无序列表,一个个搜索,时间为O(n)
如果从有序列表,从左到右搜索,找到之后,停止,找不到,比较目前元素的值和所要搜索的值。如果超出范围,停止。
二分查找,分为两半搜索,递归重复过程, 时间为O(logN)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值