Python bubble, select, insert sort by instance-Python tutorial

Source: Internet
Author: User
This section briefly introduces how to use bubble, select, and insert sorting in Python. it is recommended to your partners for reference. I recently learned the basics of python and wrote about the following three sorting trainer skills:

The code is as follows:


'''
Created on
@ 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 sorting
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 sorting
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]
Sequence [i-1] = tmp
I = I-1
//
If _ name _ = "_ main __":
Print "-------- bubble_sort -------------"
Seq = [,]
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 are three bubbles in Python: Select, insert the sorting code and use method. I hope my friends will like it.

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.