Python and Bubble sorting, python Bubble Sorting

Source: Internet
Author: User

Python and Bubble sorting, python Bubble Sorting

In the previous article, we introduced a very fast sorting algorithm-bucket sorting, but its disadvantage is that it consumes too much resources. The algorithm to be implemented this time does not consume too much resources, it is a bubble sort.

Question:

Sort the following data in ascending order: 9, 2, 8, 6, 4

 

Bubble sorting principle:

The bubble sort is to traverse the data, and compare it with the next number at a time. If the order of the two numbers is incorrect, it is exchanged.

For the above question, the numbers are listed in ascending order. When two numbers are compared, if the last number is smaller than the current number, the order is incorrect. The traversal process is as follows:

For the first time, compare the first and second numbers, 9 and 2, 9 is bigger than 2, and if the order is incorrect, the position is switched.

For the second comparison, the second and third digits are compared. Because 9 is changed to the second digit, 9 is compared with 8, 9 is large, and the order is incorrect.

And so on, the last 9 is like a bubble rising to the last place. We call it a one-stop comparison.

 

Because one trip is only a number, if there are n numbers, n-1 queries are required.

Because the number after normalization does not need to be compared, each trip only needs to compare the n-1-i times (I is the number of executed workers ).

Two loops are the key steps for Bubble Sorting:

1 for(i = 0; i < n-1; i++){2   for(j = 0; j < n-1-i; j++)3     if(a[j] > a[j+1]){4       temp = a[j];5       a[j] = a[j+1];6       a[j+1] = temp;7     }8 } 

Python implementation:

Based on the above ideas, python is also the key to implementation:

1 # assume that all variables have been defined 2 for I in range (len-1): 3 for j in range (len-1-i): 4 if a [j]> a [j + 1]: 5 a [j], a [j + 1] = a [j + 1], a [j]

The complete code is as follows: (you can download the https://github.com/DIGCreat/pythonAndAlgorithms.git from github)

1 #! /Usr/bin/env python 2 #-*-coding: utf8-*-3 ''' 4 Introduction: This program uses python to Implement Bubble sorting, the function of the program is to implement 5 descending order. 6 7 Author: King Date: 2016/08/01 version 1 8 ''' 9 10 class BubbleSort (object): 11''' 12 self. datas: list of data to be sorted 13 self. datas_len: Data urgent length 14 _ sort (): Sorting function 15 show (): output result function 16 17 usage: 18 BubbleSort (datas) instantiate a sorting Object 19 BubbleSort (datas ). _ sort () starts sorting, because sorting directly operates 20 self. datas, so the sorting result 21 is also saved in self. 22 BubbleSort (datas) in datas ). show () output result 23 ''' 24 def _ init _ (self, datas): 25 self. datas = datas26 self. datas_len = len (datas) 27 28 def _ sort (self): 29 # Bubble Sorting sorts n numbers. Since only one number is arranged for each traversal, 30 # You need to traverse n-1 bits, so the outermost layer of the loop is to loop for n-1 times, while 31 # Each traversal needs to compare the number of each returned bits, to subtract the sorted I-bit number from the n-1 comparison 32 #, the second-layer loop is to traverse the n-1-i 33 for I in range (self. datas_len-1): 34 for j in range (self. datas_len-1-i): 35 if (self. datas [j] <self. datas [j + 1]): 36 self. datas [j], self. datas [j + 1] = \ 37 self. datas [j + 1], self. datas [j] 38 39 def show (self): 40 print 'result is: ', 41 for I in self. datas: 42 print I, 43 print ''44 45 if _ name _ = '_ main _': 46 try: 47 datas = raw_input ('Please input some number: ') 48 datas = datas. split () 49 datas = [int (datas [I]) for I in range (len (datas)] 50 rows t Exception: 51 pass52 53 rows = BubbleSort (datas) 54 forbidden. _ sort () 55 rows. show ()

Summary:

Bubble Sorting is directly performed on the original array, so it occupies a small amount of space resources, which is quite good in the case of a small amount of data. However, because the algorithm involves a dual loop, the running time of the program is quite long in case of a large amount of data, because the data needs to be traversed at a time.

Finally, if you are interested, you can follow my public account and read my article at any time. * ^_^ *

Scan the QR code to follow or search for the number King_diary.

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.