Bubble sorting is a relatively simple sorting algorithm in the field of computer science. It repeatedly visits the column of elements to be sorted and compares two adjacent elements in sequence. If the order [such as from large to Small, initials from Z to A] If you make a mistake, swap them over.
void vBubbleSort(int arr[], int len){ int i, j, temp; for (j = 0; j < len - 1; j++){ //每次最大元素就像气泡一样"浮"到数组的最后 for (i = 0; i < len - 1 - j; i++){ //依次比较相邻的两个元素,使较大的那个向后移 if(arr[i] > arr[i + 1]){ //交换两个数 temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } } void vBubbleSortChange(int arr[], int len){ int i,j,temp; int swapped = 1; for (j = 0; swapped; j++){ //每次最大元素就像气泡一样"浮"到数组的最后 swapped = 0; for (i = 0; i < len - 1 - j; i++){ //依次比较相邻的两个元素,使较大的那个向后移 if(arr[i] > arr[i + 1]){ //交换两个数 temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = 1; } } // if( swapped == 0) {j = len-1;}//如果没有元素交换,说明序列是顺序的,退出循环 } } void vCockTailSort(int arr[],int len){ int tmp,i,left=0,right = len-1; while(left < right){ for(i=left;i<right;i++){//正向冒泡,确定最大值 if(arr[i]>arr[i+1]){ tmp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tmp; } } right--; for(i=right;i>left;i--){//反向冒泡,确定最小值 if(arr[i]<arr[i-1]){ tmp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = tmp; } } left++; } } void vCockTailSortChange(int arr[],int len){ int tmp,i,left=0,right = len-1; int swapped = 1; int bound = 0;//记录某趟遍历的最后一次交换元素的位置,优化减少循环次数 while(swapped){//如果没有元素交换,说明序列是顺序的 swapped = 0; for(i=left;i<right;i++){//正向冒泡,确定最大值 if(arr[i]>arr[i+1]){ tmp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tmp; swapped = 1; bound = i; } } right=bound;//缩小遍历边界 for(i=right;i>left;i--){//反向冒泡,确定最小值 if(arr[i]<arr[i-1]){ tmp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = tmp; swapped = 1; bound = i; } } left=bound;//缩小遍历边界 } }
The above is the detailed content of Bubble sort algorithm code. For more information, please follow other related articles on 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

Dreamweaver Mac version
Visual web development tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
