Python bubble, select, insert sort use instance

Source: Internet
Author: User
Recently learned the basics of Python, write a 3 sort of practice practiced hand:

Copy the Code code as follows:


'''
Created on 2013-8-23
@author: Codegeek
'''
Bubble sort
def bubble_sort (seq):
For I in range (len (seq)):
For j in range (I,len (seq)):
If SEQ[J] < Seq[i]:
TMP = Seq[j]
SEQ[J] = Seq[i]
Seq[i] = tmp
Select sort
def selection_sort (seq):
For I in range (len (seq)):
Position = I
For j in range (I,len (seq)):
If seq[position] > Seq[j]:
Position = J
If position! = I:
TMP = Seq[position]
Seq[position] = Seq[i]
Seq[i] = tmp
Insert Sort
def insertion_sort (seq):
If Len (seq) > 1:
For I in range (1,len (seq)):
While I > 0 and Seq[i] < Seq[i-1]:
TMP = Seq[i]
Seq[i] = seq[i-1]
SEQ[I-1] = tmp
i = I-1
//
if __name__ = = "__main__":
Print "--------bubble_sort-------------"
SEQ = [22,1,33,4,7,6,8,9,11]
Bubble_sort (seq)
Print seq
Print "--------selection_sort-------------"
SEQ = [88,44,33,4,7,6,8,9,11]
Selection_sort (seq)
Print seq
Print "--------insertion_sort-------------"
SEQ = [777,44,33,4,7,6,1111,100,11]
Insertion_sort (seq)
Print seq

The above is the 3 Python bubble, select, insert the sort of code and use the method, I hope that the small partners can enjoy.

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