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、有序列表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章