Sort algorithm-bubble sort algorithm

Source: Internet
Author: User

Sort algorithm-bubble sort algorithm

Bubble Introduction

Algorithm principle

Algorithm analysis

Algorithm implementation

Example

First, Bubbling introduction

Name: Bubble sort;

English: Bubble Sort

  Complexity of Time: O (n2)

Algorithm Stability: stable sorting algorithm;

Essence: The small (large) element is moved forward (back)

# Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.  # It has repeatedly visited the column of elements to be sorted, comparing two adjacent elements at a time, exchanging them if their order (e.g., from the big to the small, the first letter from A to Z) is wrong. The work of a visiting element is repeated until no adjacent elements need to be exchanged, that is, the element has been sorted.  # This algorithm is named because the larger elements will slowly "float" to the top of the series (in ascending or descending order), just as the bubbles of carbon dioxide in carbonated drinks will eventually rise to the top, hence the name "bubble sort". 
Bubble Sort Specific Introduction

Second, the principle of the algorithm

A. Compare adjacent elements. If the first one is bigger than the second, swap them both;

B. Do the same work for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the largest number;

C. Repeat the above steps for all elements except the last one;

D. Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare;

Three, algorithm analysis

1. Complexity of Time:

n is the number of elements;

A. When the initial state of the file is a positive sequence, a scan can be completed, the required number of keyword comparisons C and record the number of moves m have reached the minimum value:

So, the best time complexity of bubble sort is O (n);

B. When the initial state of the file is reversed, a sequencing is required, and the sub-keyword comparison (1≤i≤n-1) is performed for each order, and each comparison must be moved three times to reach the Exchange record location. In this case, the comparison and the number of moves have reached the maximum value:

    

The worst time complexity for bubble sorting is; 2. Algorithm Stability: The bubble sort is to move the small element forward or the large element to the back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. Therefore, if the two elements are equal, they will not be exchanged, and if the two equal elements are not adjacent, then even if the two are adjacent by the preceding 22 interchanges, this time it will not be exchanged, so the order of the same elements is not changed, so the bubble sort is a stable sort algorithm. Four, the algorithm realization:
defBubble_sort (nums): forIinchRange (len (nums)-1):#This loop is responsible for setting the number of times the bubble sort is performed         forJinchRange (len (nums)-i-1):#J for List subscript            ifNUMS[J] > nums[j + 1]: nums[j], nums[j+ 1] = nums[j + 1], Nums[j]returnNumsPrint(Bubble_sort ([45, 32, 8, 33, 12, 22, 19, 97]))--------------------The result of the output---------------------------------------[8, 12, 19, 22, 32, 33, 45, 97]
Algorithm Implementation

Sort algorithm-bubble sort algorithm

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.