Python Sort and search

Source: Internet
Author: User

Python Sort and search

Learn a bit of sorting and search, do a summary. If it's not right there, please give me more advice.

Sorting algorithm: is an algorithm that can arrange a string of data in a particular order.

stability : A stable sorting algorithm maintains a relative order of records that have equal key values. That is, if a sorting algorithm is stable, when there are two equal key values of the record r and S, and in the original list R appears before s, in the sorted list R will also be before S.

For example (1,3) (2,3) (2,1) to sort

  Bubble Sort :

Compares the adjacent elements. If the first one is larger than the second (ascending), swap them both. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

#Coding=utf-8defShenxu (alist): forJinchRange (len (alist) -1,0,-1):#Range (start number, end number, step) Len length         forIinchRange (j):ifAlist[i] > Alist[i+1]:                Print "before Transformation", I,alist, Alist[i], alist[i+1] = alist[i+1], alist[i]#Two number Exchange                Print "after Transformation", I,alistlist1= [1,9,8,2,3,7,4,6,0,5]shenxu (List1)

Results

Bubbling improved once called directional bubbling sort; It feels like bubbling and doesn't say much.

    • Worst time complexity: O (n2)
    • Stability: Stable

Select Sort: is a simple and intuitive sorting algorithm. It works as follows. First find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest (large) element from the remaining unsorted elements, and place it at the end of the sorted sequence. And so on until all elements are sorted.

(Continue writing next time)

Python Sort and search

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.