Python selection Sorting Algorithm example summary

Source: Internet
Author: User
This paper summarizes the Python selection sorting algorithm. Share to everyone for your reference. Specific as follows:

Code Listing 1:

def ssort (V): #V is the list of sorted  j = 0 #j is the ' current ' ordered position, starting with the first one in th E list while  j! = Len (V): #this is the replacing so ends when it reaches    the end of the list for I in range (J, l  En (V)):   #here It replaces the minor value that it finds with J position      if V[i] < V[j]:     #but it does it for Every value minor than position J        V[j],v[i] = v[i],v[j]    j = j+1 #and Here's the   addiction that limits the Ver Ification to the next values  

Code Listing 2:

def selection_sort (list): l=list[:] # Create a copy of the list sorted=[] # This new list would hold the results wh Ile Len (L): # While there is elements to sort ... lowest=l[0] # Create a variable to identify lowest for x in L: # and check every item in the list ... if x
 
  

Code 3

A=input ("Enter The length of the list:") # too ask the user length of the list l=[]# take a emty list for G in range (a): # For append the values from user b=input ("Enter the Element:") # to ask the user to give list values L.append (b) # To append a values in a empty list L print ' The given eliments list is ', L for I in Range (Len (L)): # To repeat the Loop tak E length of L Index=i # to store the values I in string index Num=l[i] # to take first value in list and store in Nu M for j in Range (I+1,len (L)): # to-Find out-the small value in a list-read all values if NUM&GT;L[J]: # to Compa Re the values which store in num and List Index=j # to store the small value of the Loop J in Index Num=l [j] # to store small charecter is value in num tem=l[i] # to Swaps the list take the Temparary list Stor list Vlau Es L[i]=l[index] # to take first value as another l[index]=tem print "After the swping the list by selection sort is" , l

Hopefully this article will help you with Python programming.

  • 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.