What is the code of bubble sort algorithm?

coldplay.xixi
Release: 2020-06-20 15:05:18
Original
3263 people have browsed it

What is the code of bubble sort algorithm?

What is the bubble sort algorithm code?

The bubble sort algorithm code is:

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;iarr[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; swapped = 1; bound = i; } } right=bound;//缩小遍历边界 for(i=right;i>left;i--){//反向冒泡,确定最小值 if(arr[i]
        
Copy after login

Recommended tutorial: "C Language Video Tutorial"

The above is the detailed content of What is the code of bubble sort algorithm?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!