Bubble Sort
1. Algorithm description
Bubble sorting: Compare adjacent data in turn, put small data in front and big data in the back; that is, compare the 1st and 2nd numbers in the first pass, and put the big numbers in the back , with the decimal first, then compare the second number with the third number, with the larger number at the end and the decimal first, and so on, the largest number will be "rolled" to the last position; in the second pass, the next largest number will be "rolled" Scroll to the second to last position... The sorting can be completed in the n-1 (n is the number of unordered data) times.
Take the following 5 unordered data as an example:
40 8 15 18 12 (The article only details the comparison process of the first pass)
First pass: 8 15 18 12 40
No. 2nd trip: 8 15 12 18 40
3rd trip: 8 12 15 18 40
4th trip: 8 12 15 18 40
2. Algorithm analysis
average time complexity: O(n2)
space Complexity: O(1) (for exchange)
Stability: Stable
3. Algorithm implementation
//交换data1和data2所指向的整形 void DataSwap(int* data1, int* data2) { int temp = *data1; *data1 = *data2; *data2 = temp; } /******************************************************** *函数名称:BubbleSort *参数说明:pDataArray 无序数组; * iDataNum为无序数据个数 *说明: Bubble Sort *********************************************************/ void BubbleSort(int* pDataArray, int iDataNum) { for (int i = 0; i < iDataNum - 1; i++) //走iDataNum-1趟 for (int j = 0; j < iDataNum - i - 1; j++) if (pDataArray[j] > pDataArray[j + 1]) DataSwap(&pDataArray[j], &pDataArray[j + 1]); }
4. Algorithm optimization
You can also simply optimize the bubble sort algorithm and record it with a mark Whether there is exchange during a comparison process, if there is no exchange, the entire array has exited the sorting process in order, otherwise, the next comparison will continue.
/******************************************************** *函数名称:BubbleSort *参数说明:pDataArray 无序数组; * iDataNum为无序数据个数 *说明: Bubble Sort *********************************************************/ void BubbleSort(int* pDataArray, int iDataNum) { BOOL flag = FALSE; //记录是否存在交换 for (int i = 0; i < iDataNum - 1; i++) //走iDataNum-1趟 { flag = FALSE; for (int j = 0; j < iDataNum - i - 1; j++) if (pDataArray[j] > pDataArray[j + 1]) { flag = TRUE; DataSwap(&pDataArray[j], &pDataArray[j + 1]); } if (!flag) //上一趟比较中不存在交换,则退出排序 break; } }
For more bubble sort related articles, please pay attention to the 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.

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 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

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.
