Advertisement
paulogp

Lista ordenada

Aug 7th, 2011
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # paulogp
  2. def inserir_ordenado(A):
  3.     ''' entrada ordenada '''
  4.     for j in range(1, len(A)):
  5.         key = A[j]
  6.         i = j - 1
  7.  
  8.         while (i >=0) and (A[i] > key):
  9.             A[i+1] = A[i]
  10.             i = i - 1
  11.            
  12.         A[i+1] = key
  13.    
  14.     return A
  15.  
  16. # exemplo
  17. a = [2, 5, 4, 1, 9, 7]
  18. print(inserir_ordenado(a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement