Python数据结构——查找和排序

1.1线性查找
在Python中查看一个元素是否在一个序列中,我们可以使用‘in’操作符,如:

if key in theArray :
print( "key在 array中." )
else :
print( "key 不在 array中." )

不难想象,其实‘in’操作符是基于线性查找实现的。接下来看一个在无序序列上的线性查找的实现。

def linearSearch( theValues, target ):
  n = len( theValues )
  for i in range( n ) :
      #如果欲查找的元素存在于列表中,返回Ture
      if theValues[i] == target
          return True
      return False    #如果没找到,返回False

接下来再看一个有序序列上的线性查找的实现

 def sortedLinearSearch( theValues, item ) :
   n = len( theValues )
   for i in range( n ) :
     # 如果欲查找的元素存在于列表中,返回Ture
     if theValues[i] == item :
       return True
 # 如果欲查找的元素大于当前元素, 则不在序列中.
     elif theValues[i] > item :
       return False

   return False # 元素不在序列中.

寻找最小值
假设要在一个无需列表中寻找最小值,这可以直接应用Python的内置方法’min()’.内部机制仍然是线性查找。接下来看一个在无需列表中寻找最小值得实现:

 def findSmallest( theValues ):
   n = len( theValues )
   # 假设序列中第一个元素时最小.
   smallest = theValues[0]
   # 查看序列中其他更小的元素.
   for i in range( 1, n ) :
       if theList[i] < smallest :
         smallest = theValues[i]

    return smallest # 返回最小值.

线性查找的时间复杂度为O(n)
1.2、二分查找
其应用的思想是分治策略,下面是一个在有序序列中应用二分查找的列子

 def binarySearch( theValues, target ) :
   low = 0
   high = len(theValues) - 1
   #重复使用二分法知道找到元素
   while low <= high :
     # 确定序列中间值.
    mid = (high + low) // 2
     # 中间值是目标元素么?
    if theValues[mid] == target :
      return True
    # 目标元素小于中间值?
    elif target < theValues[mid] :
      high = mid - 1
   # 目标元素在中间值后面?
    else :
      low = mid + 1

 # 如果序列不能再分,则结束.
   return False

二分查找的时间复杂度为O(logn),比线性查找更高效。

2、排序
冒泡排序
.

def bubbleSort( theSeq ):
  n = len( theSeq )
  # Perform n-1 bubble operations on the sequence
  for i in range( n - 1 ) :
  # Bubble the largest item to the end.
  for j in range( i + n - 1 ) :
    if theSeq[j] > theSeq[j + 1] : # swap the j and j+1 items.
      tmp = theSeq[j]
      theSeq[j] = theSeq[j + 1]
      theSeq[j + 1] = tmp

选择排序
.

def selectionSort( theSeq ):
  n = len( theSeq )
  for i in range( n - 1 ):
   # Assume the ith element is the smallest.
    smallNdx = i
   # Determine if any other element contains a smaller value.
    for j in range( i + 1, n ):
      if theSeq[j] < theSeq[smallNdx] :
        smallNdx = j

   # Swap the ith value and smallNdx value only if the smallest value is
  # not already in its proper position. Some implementations omit testing
  # the condition and always swap the two values.
   if smallNdx != i :
     tmp = theSeq[i]
     theSeq[i] = theSeq[smallNdx]
     theSeq[smallNdx] = tmp

插入排序

def insertionSort( theSeq ):
  n = len( theSeq )
  # Starts with the first item as the only sorted entry.
  for i in range( 1, n ) :
  # Save the value to be positioned.
    value = theSeq[i]
   # Find the position where value fits in the ordered part of the list.
    pos = i
    while pos > 0 and value < theSeq[pos - 1] :
   # Shift the items to the right during the search.
       theSeq[pos] = theSeq[pos - 1]
       pos -= 1

 # Put the saved value into the open slot.
   theSeq[pos] = value

3、有序列表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值