What is the bubble sorting algorithm code
What is bubble sort algorithm code? I believe that many inexperienced people are helpless about this. For this reason, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Bubble sorting is a relatively simple sorting algorithm in computer science that repeatedly visits a column of elements to be sorted, compares two adjacent elements in turn, and swaps them if the order (e.g., from large to small, from Z to A) is wrong.
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]){ //swap two numbers 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++){ //Each time the largest element "floats" like a bubble to the end of the array swapped = 0; for (i = 0; i
< len - 1 - j; i++){ //依次比较相邻的两个元素,使较大的那个向后移 if(arr[i] >arr[i + 1]){ //swap two numbers temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = 1; } }// if( Swapped == 0) {j = len-1;}//If there is no element swap, the sequence is sequential, exit the loop }}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--){//reverse bubble, determine minimum if(arr[i]left;i--){//reverse bubble, determine minimum if(arr[i]