bubble sort algorithm
The basic idea of exchange sorting is: compare the keywords of the records to be sorted pairwise, and when it is found that the order of the two records is reversed, the exchange is performed until there are no records in the reverse order.
The main sorting methods that apply the basic idea of exchange sort are: bubble sort and quick sort.
Bubble sort
1. Sorting method
Arrange the sorted record array R[1..n] vertically, and each record R[i] is regarded as a bubble with a weight of R[i].key. According to the principle that light bubbles cannot be under heavy bubbles, the array R is scanned from bottom to top: any light bubble that violates this principle will be "floated" upward. This is repeated until the last two bubbles are the lighter one at the top and the heavier one at the bottom.
(1) Initial
R[1..n] is an unordered area.
(2) The first scan
Compare the weights of two adjacent bubbles from the bottom of the disordered area upwards. If it is found that the lighter one is at the bottom and the heavier one is at the top, swap the positions of the two. That is, compare (R[n], R[n-1]), (R[n-1], R[n-2]),..., (R[2], R[1]) in sequence; for each pair Bubble (R[j+1], R[j]), if R[j+1].key
(3) The second scan
Scan R[2..n]. When the scan is completed, the "second lightest" bubble floats to the position of R[2]...
Finally, after n-1 scans, the ordered area R[1..n] can be obtained
Note:
The ith scan When , R[1..i-1] and R[i..n] are the current ordered area and unordered area respectively. Scanning is still from the bottom of the disordered area upward to the top of the area. When the scan is completed, the lightest bubble in the area floats to the top position R[i], and the result is that R[1..i] becomes a new ordered area.
2. Example of bubble sorting process
The process of bubble sorting the files with the keyword sequence 49 38 65 97 76 13 27 49
3. Sorting algorithm
(1) analysis
Because each sorting process uses A bubble is added to the ordered area. After n-1 sortings, there are n-1 bubbles in the ordered area, and the weight of the bubbles in the disordered area is always greater than or equal to the weight of the bubbles in the ordered area, so The entire bubble sort process requires at most n-1 sorting passes.
If no exchange of bubble positions is found in a certain sorting pass, it means that all the bubbles in the unordered area to be sorted satisfy the principle of the lighter ones at the top and the heavier ones at the bottom. Therefore, the bubble sorting process can be sorted in this pass. then terminate. To this end, in the algorithm given below, a Boolean exchange is introduced, and it is set to FALSE before each sorting starts. Set to TRUE if an exchange occurs during sorting. The exchange is checked at the end of each sorting pass. If no exchange has occurred, the algorithm is terminated and the next sorting pass is not performed.
(2) Specific algorithm
void BubbleSort(SeqList R)
{ //R (l..n) is the file to be sorted. Use bottom-up scanning to perform bubble sorting on R
int i, j;
Boolean exchange; //Exchange flag
for(i=1;i
for (j=n-1;j>=i;j--) //对当前无序区R[i..n]自下向上扫描
if(R[j+1].key
. D} // endfor (outer cycle)}} // bubblesort
For more bubbling sorting algorithms related articles, please follow PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

How to implement the bubble sort algorithm in C# Bubble sort is a simple but effective sorting algorithm that arranges an array by comparing adjacent elements multiple times and exchanging positions. In this article, we will introduce how to implement the bubble sort algorithm using C# language and provide specific code examples. First, let us understand the basic principles of bubble sort. The algorithm starts from the first element of the array and compares it with the next element. If the current element is larger than the next element, swap their positions; if the current element is smaller than the next element, keep it

Function pointer technology can improve code efficiency and reusability, specifically as follows: Improved efficiency: Using function pointers can reduce repeated code and optimize the calling process. Improve reusability: Function pointers allow the use of general functions to process different data, improving program reusability.

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

PHP array sorting algorithm complexity: Bubble sort: O(n^2) Quick sort: O(nlogn) (average) Merge sort: O(nlogn)

Go is an increasingly popular programming language that is designed to be easy to write, easy to read, and easy to maintain, while also supporting advanced programming concepts. Time complexity and space complexity are important concepts in algorithm and data structure analysis. They measure the execution efficiency and memory size of a program. In this article, we will focus on analyzing the time complexity and space complexity in the Go language. Time Complexity Time complexity refers to the relationship between the execution time of an algorithm and the size of the problem. Time is usually expressed in Big O notation

C++ function performance optimization algorithm selection: Choose efficient algorithms (such as quick sort, binary search). Optimization skills: inline small functions, optimize caching, avoid deep copies, and loop unrolling. Practical case: When searching for the maximum element position of an array, binary search and loop expansion are used after optimization, which greatly improves performance.

The use of data structures and algorithms is crucial in cloud computing for managing and processing massive amounts of data. Common data structures include arrays, lists, hash tables, trees, and graphs. Commonly used algorithms include sorting algorithms, search algorithms and graph algorithms. Leveraging the power of Java, developers can use Java collections, thread-safe data structures, and Apache Commons Collections to implement these data structures and algorithms.
