Kaidul

Untitled

Nov 9th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4.  
  5. This is a temporary script file.
  6. """
  7. import sys
  8.  
  9. def bidirectional_bubble_sort(a):
  10.     left = -1
  11.     right = len(a)
  12.     while left < right:
  13.         swap = False
  14.         left += 1
  15.         right -= 1
  16.         for i in xrange(left, right):
  17.             if a[i] > a[i + 1]:
  18.                 t = a[i]
  19.                 a[i] = a[i + 1]
  20.                 a[i + 1] = t
  21.                 swap = True
  22.         if not swap:
  23.             return
  24.         else:
  25.             swap = False
  26.         for i in xrange(right, left, -1):
  27.             if a[i] < a[i - 1]:
  28.                 t = a[i]
  29.                 a[i] = a[i - 1]
  30.                 a[i - 1] = t
  31.                 swap = True
  32.         if not swap:
  33.             return
  34.            
  35.  
  36. def main(argv):
  37.     bidirectional_bubble_sort(argv)
  38.  
  39. if __name__ == "__main__":
  40.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment