Python bubble sort (4)

Source: Internet
Author: User

Python code:

"""Bubble Sort (4) in the unsorted number, by 22 comparison [transposition], the smallest number to the first n number of orders, you need to execute the N-1 wheel, the 1th round of N-1 times, the subsequent round than the previous round less than 1 times in this case: the 1th round will be 0 in the correct position; 2nd wheel will 1 in the correct position; ...... The 9th round puts 8 in the correct position; In this method, the two position number comparison, two positions are the change 1th round 1th time is the position 8, the position 9 the numerical comparison, the former large, then the interchange position; 1th Round 2nd is the position 7, the position 8 the number comparison, the former big, then the interchange position; 1th round the 9th time is the position 0, position 1 of the number comparison, the former large, then the interchange position; 2nd round 1th is position 8, position 9 of the number comparison, the former large, then swap position; The 2nd round the 8th time is the position 1, the position 2 numerical comparison, the former large, then swaps the position;"""LST= [3, 6, 9, 1, 8, 7, 2, 5, 4, 0]Print("before sorting:%s\r\n"%lst)#Wheel forIinchRange (len (LST) -1,0,-1):#[9,8,7,6,5,4,3,2,1]    Print("section%s round"% (10-i))#Times     forJinchRange (len (LST) -1,len (LST)-i-1,-1):        #If the front size is small then the swap position        ifLST[J-1] >Lst[j]: lst[j-1],LST[J] = lst[j],lst[j-1]            Print("%s times [%s]:%s,[%s]:%s comparison swap%s"% (9-j+1,j-1,lst[j],j,lst[j-1],lst))Else:            Print("first%s times [%s]:%s,[%s]:%s comparison invariant%s"% (9-j+1,j-1,lst[j-1],j,lst[j],lst))Print("\ r \ n Sort after:%s"% LST)

Output Result:

E:\python\algorithm>Python3 bubblesort4.py before sorting: [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] 1th round 1th time [8]:4,[9]:0 comparison Interchange [3, 6, 9, 1, 8, 7, 2, 5, 0, 4] 2nd time [7]:5,[8]:0 comparison Interchange [3, 6, 9, 1, 8, 7, 2, 0, 5, 4] 3rd time [6]:2,[7]:0 comparison Interchange [3, 6, 9, 1, 8, 7, 0, 2, 5, 4] 4th time [5]:7,[6]:0 comparison Interchange [3, 6, 9, 1, 8, 0, 7, 2, 5, 4] 5th time [4]:8,[5]:0 comparison Interchange [3, 6, 9, 1, 0, 8, 7, 2, 5, 4] 6th time [3]:1,[4]:0 comparison Interchange [3, 6, 9, 0, 1, 8, 7, 2, 5, 4] 7th time [2]:9,[3]:0 comparison Interchange [3, 6, 0, 9, 1, 8, 7, 2, 5, 4] 8th time [1]:6,[2]:0 comparison interchange [3, 0, 6, 9, 1, 8, 7, 2, 5, 4] 9th time [0]:3,[1]:0 comparison Interchange [0, 3, 6, 9, 1, 8, 7, 2, 5, 4] 2nd round 1th time [8]:5,[9]:4 comparison Interchange [0, 3, 6, 9, 1, 8, 7, 2, 4, 5] 2nd time [7]:2,[8]:4 comparison invariant [0, 3, 6, 9, 1, 8, 7, 2, 4, 5] 3rd time [6]:7,[7]:2 comparison Interchange [0, 3, 6, 9, 1, 8, 2, 7, 4, 5] 4th time [5]:8,[6]:2 comparison Interchange [0, 3, 6, 9, 1, 2, 8, 7, 4, 5] 5th time [4]:1,[5]:2 comparison invariant [0, 3, 6, 9, 1, 2, 8, 7, 4, 5] 6th time [3]:9,[4]:1 comparison Interchange [0, 3, 6, 1, 9, 2, 8, 7, 4, 5] 7th time [2]:6,[3]:1 comparison Interchange [0, 3, 1, 6, 9, 2, 8, 7, 4, 5] 8th time [1]:3,[2]:1 comparison Interchange [0, 1, 3, 6, 9, 2, 8, 7, 4, 5] 3rd round 1th time [8]:4,[9]:5 comparison invariant [0, 1, 3, 6, 9, 2, 8, 7, 4, 5] 2nd time [7]:7,[8]:4 comparison Interchange [0, 1, 3, 6, 9, 2, 8, 4, 7, 5] 3rd time [6]:8,[7]:4 comparison Interchange [0, 1, 3, 6, 9, 2, 4, 8, 7, 5] 4th time [5]:2,[6]:4 comparison invariant [0, 1, 3, 6, 9, 2, 4, 8, 7, 5] 5th time [4]:9,[5]:2 comparison Interchange [0, 1, 3, 6, 2, 9, 4, 8, 7, 5] 6th time [3]:6,[4]:2 comparison Interchange [0, 1, 3, 2, 6, 9, 4, 8, 7, 5] 7th time [2]:3,[3]:2 comparison Interchange [0, 1, 2, 3, 6, 9, 4, 8, 7, 5] 4th round 1th Time [8]:7,[9]:5 comparison Interchange [0, 1, 2, 3, 6, 9, 4, 8, 5, 7] 2nd time [7]:8,[8]:5 comparison Interchange [0, 1, 2, 3, 6, 9, 4, 5, 8, 7] 3rd time [6]:4,[7]:5 comparison invariant [0, 1, 2, 3, 6, 9, 4, 5, 8, 7] 4th time [5]:9,[6]:4 comparison Interchange [0, 1, 2, 3, 6, 4, 9, 5, 8, 7] 5th time [4]:6,[5]:4 comparison Interchange [0, 1, 2, 3, 4, 6, 9, 5, 8, 7] 6th time [3]:3,[4]:4 comparison invariant [0, 1, 2, 3, 4, 6, 9, 5, 8, 7] 5th round 1th time [8]:8,[9]:7 comparison Interchange [0, 1, 2, 3, 4, 6, 9, 5, 7, 8] 2nd time [7]:5,[8]:7 comparison invariant [0, 1, 2, 3, 4, 6, 9, 5, 7, 8] 3rd time [6]:9,[7]:5 comparison Interchange [0, 1, 2, 3, 4, 6, 5, 9, 7, 8] 4th time [5]:6,[6]:5 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 9, 7, 8] 5th time [4]:4,[5]:5 comparison invariant [0, 1, 2, 3, 4, 5, 6, 9, 7, 8] 6th round 1th time [8]:7,[9]:8 comparison invariant [0, 1, 2, 3, 4, 5, 6, 9, 7, 8] 2nd time [7]:9,[8]:7 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 7, 9, 8] 3rd time [6]:6,[7]:7 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 9, 8] 4th time [5]:5,[6]:6 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 9, 8] 7th round 1th Time [8]:9,[9]:8 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2nd time [7]:7,[8]:8 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 3rd time [6]:6,[7]:7 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 8th round 1th time [8]:8,[9]:9 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2nd time [7]:7,[8]:8 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 9th round 1th time [8]:8,[9]:9 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After sorting: [0,1, 2, 3, 4, 5, 6, 7, 8, 9]

===== End =====

Python bubble sort (4)

Related Article

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.