Bubble sort Python implementation

Source: Internet
Author: User
Start learning python, format god horse is the cloud, directly on the data structure of the algorithm. After all, read the code to learn the fastest

1 Accept the input of the Py code, later the input of the algorithm import this file

#!/usr/bin/env python#coding=utf-8# stdininput.pyintsortarrays=[]def stdininput ():    sortArray=raw_input ("  Please input num array the want sort (use, to split every num): ")    sortarrays=sortarray.split (', ') for    num in Sortarrays:        intnum=-1        try:            intnum=int (num)        except:            print "input num array error, error element was given a default value-1 "        intsortarrays.append (Intnum)

Bubble Sort: (This implementation is bubbling from the last, you can choose whether to take a big bubble or a small bubble)

#!/usr/bin/env Python#coding=utf-8#bubblesort.py#user can choose sort Style:desc (1) or ASC (2) Import Sysimport Stdininputdef Bubblesort (Sortarray,style): Sortarraylen=len (Sortarray) whilenum=0 if Style==1:while while Num < sortarraylen-1: for num in xrange (sortarraylen-1,whilenum,-1): If Sortarray[num-1]<s Ortarray[num]: sortarray[num-1],sortarray[num]=sortarray[num],sortarray[num-1] Whilenum=whil                Enum+1 Else:while Whilenum < sortarraylen-1: for num in xrange (sortarraylen-1,whilenum,-1): If Sortarray[num-1]>sortarray[num]: Sortarray[num-1],sortarray[num]=sortarray[num],sortarr AY[NUM-1] whilenum=whilenum+1if __name__== ' __main__ ': style=1 try:style=int (sys.argv[1]) exce Pt:print "Input argv error, use default desc sort" Stdininput.stdininput () Bubblesort (Stdininput.intsortarra Ys,style) Print Stdininput.intsoRtarrays 
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.