First, the sort of python1, reversed ()This is very well understood,reversed English means:adj. reversed; reverse; (judgment, etc) revoked Print List (reversed (['dream','a','have','I')])# [' I ', ' have ', ' a ', ' dream '] 2. Confusing sort () with sorted ()In Python sorted is the built-in function (BIF), and s
#!/usr/bin/env python#-*-coding:utf-8-*-def heap_sort (arr): L = Len (arr) for I in Xrange (L/2,-1,-1): for M_heap (arr, I, l-1) for i in Xrange (l-1, 0,-1): arr[i],arr[0] = arr[0],arr[i] form_heap (arr, 0, i-1) ret Urndef form_heap (arr, start, end): While (Start Python implements heap ordering
The time complexity of bubble sorting is the idea of O (n^2) bubble sort: compare two adjacent elements each time, and swap them if they're in the wrong order.For example, there are five numbers: 12, 35, 99, 18, 76, from the big to the small sort, compare the adjacent two bits
First trip:
First time comparisons: 35, 12, 99, 18, 76
Second comparisons: 35, 99, 12, 18, 76
Third comparisons
Summed up the common centralized sorting algorithm
Merge sort
Merge sort is also called merging sort, which is a typical application of partition method. The idea of divide-and-conquer is to decompose each problem into small problems, resolve each small problem, and then merge.
The specific merge sort is to decomp
ll和lr都会接到一个元素的列表 # before the last recursion the LL and RL will receive the ordered subsequence LL = Merge_sort (left) RL =merge_sort (right) # We return the two split results after sorting and returning the correct order of the sub-list # Here we call to carry a function to help us in order to merge LL and LR return merge (LL, RL) #这里接收两个列表def merge (left, right): # from two sequential lists in order to take the data comparison and put Resul t # each time we compare the smallest number of tw
In the Python manual:Sorted (Iterable[,cmp,[,key[,reverse=true]])Function: Return a new sorted list from the items in iterable.The first parameter is a iterable, and the return value is a list that sorts the elements in iterable. The optional parameters are three, CMP, key, and reverse. 1) CMP Specifies a custom comparison function that receives two parameters (elements of iterable), returns a negative number if the first argument is less than the sec
sort 2 4 7 9Eighth round sort 2 4 7Nineth Round sort 2 4As you can see, the order of each round, the largest number of elements participating in the comparison in this round, will float to the end. and the name of the bubble sort comes from here, too.# # Selection Sorting Method (Selectionsort) # #What does the so-cal
a comparison value key, which takes the first value in the list. Let the other values in the list compare them.If it is less than it is placed in the right list,If it is greater than it is placed in the left list.and then recursion. (Not very understanding of recursion.) Only know that the function itself calls itself. )Finally, left, compare value key (need to convert to list type), right is connected together.An error has occurred:Runtimeerror:maximum recursion depth exceeded while calling a
Reverse () methodReverses the order of the elements in the list, such as the following>>> x = [1,5,2,3,4]>>> X.reverse ()>>> x[4, 3, 2, 5, 1]Reverse list reversal sort : The order of the elements in the original list is re-deposited from left to right without sorting the parameters in the List. If you need to organize the parameters in the list, you need to sort the list in a different
The Python list built-in sort () method is used for sorting, or the Python built-in global sorted () method can be used to generate a new sequence for an iterative sequence ordering.Sorted (Iterable,key=none,reverse=false), returns a new list that is valid for all objects that can be iteratedSort (key=none,reverse=false) change list in place reverse:true reverse
The Python list built-in sort () method is used for sorting, or the Python built-in global sorted () method can be used to generate a new sequence for an iterative sequence ordering.Sorted (Iterable,key=none,reverse=false), returns a new list that is valid for all objects that can be iteratedSort (key=none,reverse=false) change list in place reverse:true reverse
DescribeThe sort () function is used to sort the original list, and if a parameter is specified, the comparison function specified by the comparison function is used.GrammarThe sort () method syntax:List.sort ([func]) Parameters
Func--An optional parameter that is ordered using the method of the parameter if specified.
return valueThe method does not retu
change in range, where the length-1-long comparison is required, noting the meaning of-I (can reduce the elements that are already ordered)9 forJinchRange (0,length-1-i):Ten One #Exchange A ifMYLIST[J] > Mylist[j+1]: -TMP =Mylist[j] -Mylist[j]=mylist[j+1] theMYLIST[J+1] =tmp - - #print the list after each round of exchange - forIteminchmyList: + Print(item) - Print("============================="
-*-__author__='Hzq'LST= [6,1,2,7,9,3,4,5,10,8]defMinddle_index (Lst,low,high): Start_index=low#far leftEnd_index=high#left and right side ifStart_index#precondition right greater than leftkey=Lst[start_index] #设置参照key whileEnd_index>Start_index: whileLst[end_index]>=key andEnd_index>Start_index:end_index-=1Lst[start_index],lst[end_index]=Lst[end_index],lst[start_index] whileLst[start_index] andEnd_index>Start_index:start_index+=1Lst[start_index],lst[end_index]=Lst[end_index],lst[start_index]#
Hill sort first selects the increment, groups the original list, and then sorts the same array using the direct insertion method, the last increment is 1, and the global sort is achieved.1 " "2 Hill Sort3 Space Complexity O (1)4 The Worst of Time complexity (POW (n,2))5 time Complexity general situation (POW (n,1.3))6 " "7 defxier_px (arr):8Size=Len (arr)9Dk=int (SIZE/2)Ten whileDk>=1: One for
(large) on the child node, i.e.A[parent (i)] >= a[i] ... ①The latter makes it easy to say nothing about redundancy, only the maximum heap.So the first goal of the heap is to establish a maximum heap that satisfies the condition, and to establish the maximum heap, it is necessary to have the maximum heap criterion, nam
Sort is the function of the container: sort (Cmp=none, Key=none, Reverse=false)Sorted is a python built-in function: Sorted (iterable, Cmp=none, Key=none, Reverse=false)Parameter resolution:CMP: compare function, compare what parameter is determined by key. For example: CMP (E1, E2) is a comparison function with two parameters, the return value: Negative (E1 Key:
Python to Implement Bubble, insert, select simple sort instance, python bubble
The Python implementation in this article bubble, insert, and simple sorting examples are suitable for beginners of Python to learn data structures and algorithms from the basics. The example is s
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.