[Python-algorithm] python implements bubble, insert, and select sort

Source: Internet
Author: User

I have never systematically studied data structures and algorithms from the beginning.

 

[Python]
#-*-Coding: cp936 -*-
# Python insertion sorting
Def insertSort ():
For I in range (len (a)-1 ):
# Print a, I
For j in range (I + 1, len ()):
If a [I]> a [j]:
Temp = a [I]
A [I] = a [j]
A [j] = temp
Return
 
 
# Python Bubble Sorting
Def bubbleSort (alist ):
For passnum in range (len (alist)-1, 0,-1 ):
# Print alist, passnum
For I in range (passnum ):
If alist [I]> alist [I + 1]:
Temp = alist [I]
Alist [I] = alist [I + 1]
Alist [I + 1] = temp
Return alist
 
 
# Sorting by Python Selection
Def selectionSort (alist ):
For I in range (len (alist)-1, 0,-1 ):
Maxone = 0
For j in range (1, I + 1 ):
If alist [j]> alist [maxone]:
Maxone = j
Temp = alist [I]
Alist [I] = alist [maxone]
Alist [maxone] = temp
Return alist
 
 
Alist = [54,26, 93,17, 77,31, 44,55, 20]
# Print bubbleSort (alist)
Alist = [54,26, 93,17, 77,31, 44,55, 20]
Print selectionSort (alist)

#-*-Coding: cp936 -*-
# Python insertion sorting
Def insertSort ():
For I in range (len (a)-1 ):
# Print a, I
For j in range (I + 1, len ()):
If a [I]> a [j]:
Temp = a [I]
A [I] = a [j]
A [j] = temp
Return


# Python Bubble Sorting
Def bubbleSort (alist ):
For passnum in range (len (alist)-1, 0,-1 ):
# Print alist, passnum
For I in range (passnum ):
If alist [I]> alist [I + 1]:
Temp = alist [I]
Alist [I] = alist [I + 1]
Alist [I + 1] = temp
Return alist


# Sorting by Python Selection
Def selectionSort (alist ):
For I in range (len (alist)-1, 0,-1 ):
Maxone = 0
For j in range (1, I + 1 ):
If alist [j]> alist [maxone]:
Maxone = j
Temp = alist [I]
Alist [I] = alist [maxone]
Alist [maxone] = temp
Return alist


Alist = [54,26, 93,17, 77,31, 44,55, 20]
# Print bubbleSort (alist)
Alist = [54,26, 93,17, 77,31, 44,55, 20]
Print selectionSort (alist)


 

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.