The process of bubbling sorting is to first compare the keywords of the first record with the keywords of the second record, and, in reverse order, Exchange two records and then compare the keywords for the second record and the third record. And so on, until the n-1 record and the nth record's keywords have been compared. The above process is called the first bubbling sort, then the second trip to the previous n-1 the same operation, ...The quick
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 compar
] forIinchRange (len (data_set)):#Big Circle determines the number of rounds that we chooseMinindex =I forJinchRange (I+1,len (data_set)):#a small circle is the number of times we compare each time we choose #//If the subsequent element is smaller than the element I chose, swap the position ifDATA_SET[J] Data_set[minindex]: Minindex=J Temp=Data_set[i] Data_set[i]=Data_set[minindex] Data_set[minindex]=TempPrint(Data_set)" "The basic idea of inserting
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 beg
Optimal time complexity: O (n) (in ascending order, sequence already in ascending state)
Worst time complexity: O (n2)
Stability: Stable
Code:"" "Insert sort Time complexity: O (n*n) Stability: Stable" "" "" Import randomimport timedef Insert_sort (list): n = len (list) # from the second position start inserting subscript 1 for J in Range (1,n): # Start comparison from the first J element if it is less than the p
Two days ago just loaded Python 3.1.1, can't help itched write point code.1. Select sort
Copy Code code as follows:
>>> def selsort (L):
Length=len (L)
For I in Range (length-1):
Minidx=i
Minval=l[i]
J=i+1
While JIf MINVAL>L[J]:
Minidx=j
MINVAL=L[J]
J=j+1
L[i],l[minidx]=l[minidx],l[i]
Return L
2. Bubble
1 defInsertsort (A):2' Insert sort algorithm: Pass in a list, sort the numbers in list '3 Print(' Insert sort before list element order: ', A)4Length=len (A)5For I in Range (1,length): #从第二个开始6Key=a[i]7J=i-18While J>=0 and A[j]>key:9A[J+1]=A[J]TenJ=j-1 OneA[j+1]=key A Print(' Insert sorted list element order: ', A) -#插入排序时间复杂度: n^2, Spatial complexity: 1, same el
Recommend a Visual Web site "Visual Algo": Url= ' https://visualgo.net/en/sorting 'This website gives the principles and processes of various sorting algorithms, which are visualized through dynamic forms. The related pseudo-code are also given, as well as the specific steps to execute to code."Bubble Sort"You need to repeatedly visit the sequence of columns that need to be sorted. The size of the adjacent
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 M
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
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("============================="
I have never systematically studied data structures and algorithms from the beginning.
[Python]#-*-Coding: cp936 -*-# Python insertion sortingDef insertSort ():For I in range (len (a)-1 ):# Print a, IFor j in range (I + 1, len ()):If a [I]> a [j]:Temp = a [I]A [I] = a [j]A [j] = tempReturn# Python Bubble SortingDef bu
to the last one, we call this a trip, there are several comparisons in this trip.Since a trip is only classified as one number, if there are n digits, a n-1 trip is required.Because the number after the return does not have to compare again, so each trip only need to compare n-1-i times (I is the number of executed trips).The key step that can be derived from the bubbling sort is two loops:1 for(i =0; I 1; i++){2 for(j =0; J 1-I.; J + +)3 if(
#-*-Coding:utf-8-*-# # Bubble Sort Resolution"""the necessary knowledge for bubbling sorting:swap position with temp temporary variableA1 = 456A2 = 123temp = A1A1 = A2A2 = tempPrint (A1)print (A2)the intermediate variable temp can not be used in Python. The position can be exchanged directly. A1 = 456A2 = 123if A1 > A2: # If A1 is larger than A2, then swap positi
Python code:"""Bubble Sort (5) A variant of two-way bubbling, bubbling sort. In unsorted numbers, each round finds the minimum and maximum number of digits to be placed in the end. In this example: 1th round: First put 9 in the correct position, then put 0 in the correct position, the 2nd round: first put 8 in the corr
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.