The classical sorting method based on Python language (bubbling method and selection sorting method)

Source: Internet
Author: User
Tags array length

Ago

Every weekend on the rainy period, spare time, the next python, the heart of the heart to learn, today on the computer installed a 2.7, learning the next classic algorithm, bubble and select sorting method

The first time to write about the Python article, said the wrong place, many corrections, I actively correct

Prior to that, I wrote the two sorting methods using Java and C language, see https://www.cnblogs.com/hong-fithing/p/7615907.html for details.

Set up the environment, preparation work is not redundant introduction, online a lot of

DirectoryBubble Sort MethodSelect Sort MethodBubble Sort Method

Bubbling is similar to the phenomenon of soda, shaking, there are a lot of bubbles to go up, the concept of not much to say, directly read the results of the implementation of the diagram, look at the graph analysis, as follows

The picture will be every time the sorting process is printed out, such a look, it is very concise, such as the first order, the next two numbers to compare, until 100 is greater than 2 o'clock, do the exchange, and then no more than 100 larger, so 100 rows at the end, similar to find the largest value, the second sort, is to find the second largest value, next to the penultimate one, and so on.

Code implementation is simpler than Java, the public's words are true, there are no Java constraints, such as declaring variables, Python is directly used, see the code as follows:

1 #Coding=utf-82Lis = Raw_input ('Please enter a 10 positive integer:')3Arrays=[int (a) forAinchlis.split ()]4 Print(arrays)5  forAinchRange (len (arrays)):6      forBinchRange (len (arrays)-1):7         ifARRAYS[B] > Arrays[b+1]:8temp =Arrays[b]9ARRAYS[B] = arrays[b+1]TenARRAYS[B+1] =Temp One  forCinchArrays: A     Print(str (c)),

This is a very simple sort, the input data, through the For loop sort, the output, the text before the picture, is on the basis of adding a few lines of code, the purpose is to make the sorting process more intuitive, the code is as follows:

#Coding=utf-8Lis = Raw_input ('\ n Please enter a 10 positive integer:')#version 2 requires the use of raw_input inputArrays=[int (a) forAinchLis.split ()]#separate the input into an array with a space keyPrint("the 10 integers entered are:") Print(arrays) forAinchRange (len (arrays)):#take the length of the arrays as a cyclic condition     forBinchRange (len (arrays)-1):#take the length of the arrays-1 as a cyclic condition        ifARRAYS[B] > Arrays[b+1]:#if the first value in the array is greater than the second valuetemp = Arrays[b]#The large value arrays[b] is assigned to temp .ARRAYS[B] = arrays[b+1]#assigns a small value of arrays[b+1] to the first valueARRAYS[B+1] = Temp#Assigns a value in temp to the second value, implementing a replacement position    Print("\ n%d"% (A + 1) +"the second order is:")     forCinchRange (len (arrays)):#loop to get the length of an array        PrintARRAYS[C],#the process of printing sortingPrint("\ n The final sort is:") forCinchArrays#Loop get array length    PrintSTR (c),#Print the final sort result, enter a comma to display the line without wrapping

When I was wondering about Python, I encountered two minor problems and was also unfamiliar with the Python language.

Question one:

Since the 2.7 version of the python,input is required to be implemented using Raw_input, when the input function is used, the program is reported as "<string>", line 1, <module> error

Question two:

Use print Arrays[c] Output array sorting, not a row of output, see the results are not intuitive, through the degree Niang query, more is to end with end, found that the language only support 3, and then query to the output statement after the addition, comma can

Two very small and small problems, but for I have just learned python, I have thought about it, so this record

Select Sort method

Bubble sort after the success of the stumble, the choice of sorting method is relatively simple, the sort logically slightly different

The selection sort is to be, the maximum or minimum value (depending on the condition) and the first value exchange order, unlike bubbling, see:

In the first order, it is found that the maximum value 100 is exchanged in the order with the first value, the second is the second order, and then the secondary

Look directly at the code, as follows:

1 #Coding=utf-82Lis = Raw_input ('\ n Please enter a 10 positive integer:')3Arrays=[int (a) forAinchlis.split ()]4 Print("the 10 integers entered are:")           5 Print(arrays)6  forAinchRange (0,len (arrays)-1):       7max = Arrays[a]#set a maximum value Max8Count = a#count is to remember the corner mark9      forBinchRange (a+1, Len (arrays)):Ten         ifMax < Arrays[b]:#if the default maximum value is less than the array value arrays[b] Onemax = Arrays[b]#The value in Arrays[b] is assigned to Max ACount = b#The Corner label is assigned to Count -temp = Arrays[a]#The large value arrays[a] is assigned to temp . -Arrays[a] = Arrays[count]#assigns a small value of arrays[count] to the first value theArrays[count] = Temp#Assigns a value in temp to the second value, implementing a replacement position - Print("\ n The final sort is:") -  forCinchArrays#Loop get array length -     PrintSTR (c),#Print the final sort result, enter a comma to display the line without wrapping

This is the basic syntax to implement the selection sort, and then look at the code as follows:

1 #Coding=utf-82Lis = Raw_input ('\ n Please enter a 10 positive integer:')#version 2 needs to use raw_input input, separated by a space between each number3Arrays=[int (a) forAinchLis.split ()]#separate the input into an array with a space key4 Print("the 10 integers entered are:")         5 Print(arrays)6  forAinchRange (0,len (arrays)-1):#take the length of the arrays-1 as a cyclic condition7max = Arrays[a]#set a maximum value Max8Count = a#count is to remember the corner mark9      forBinchRange (A+1,len (arrays)):#take the length of the a+1,arrays as a cyclic conditionTen         ifMax < Arrays[b]:#if the default maximum value is less than the array value arrays[b] Onemax = Arrays[b]#The value in Arrays[b] is assigned to Max ACount = b#The Corner label is assigned to Count -temp = Arrays[a]#The large value arrays[a] is assigned to temp . -Arrays[a] = Arrays[count]#assigns a small value of arrays[count] to the first value theArrays[count] = Temp#Assigns a value in temp to the second value, implementing a replacement position -     Print("\ n%d"% (A + 1) +"the second order is:") -      forCinchRange (len (arrays)):#loop to get the length of an array -         PrintARRAYS[C],#the process of printing sorting + Print("\ n The final sort is:") -  forCinchArrays#Loop get array length +     PrintSTR (c),#Print the final sort result, enter a comma to display the line without wrapping
Knot

The first time to use Python, see the convenience, but also see their own shortages, need to learn better. The above is the content of today, content is not very novel, but very classic, most of the interview process will ask. Naturally, these two code can be optimized, can increase the interrupt cycle, when the situation is already orderly, jump out of the loop, save sequencing time, learning is a very interesting process, refueling!

This article is for the author's opinion only, the author at the temperature of a pot of sake published. Reprint Please specify source: http://www.cnblogs.com/hong-fithing/

The classical sorting method based on Python language (bubbling method and selection sorting method)

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.