Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Bubble sorting method of C language

Shulou Source: shulou.com Published: 2022-06-01 11:23:18 09月26日 Update

This article mainly explains "how to use the bubble sorting method of C language". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to use the bubble sorting method of C language" together.

Title: bubble hair on any input of 10 numbers from small to large sorting.

I. Algorithm principle: (extracted from Baidu encyclopedia)

Bubble sorting algorithm works as follows: (back to front)

Compare adjacent elements. If the first one is bigger than the second one, swap them both.

Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element should be the largest number.

Repeat the above steps for all elements except the last one.

Continue repeating the above steps for fewer and fewer elements at a time until there are no pairs of numbers to compare.

II. Algorithm analysis:

Time complexity: O(n^2)

III. Algorithm implementation:

1. C language code:

/* Day 10, Bubble Sort */#include #include /*Bubble_Sort function declaration */int* Bubble_Sort(int* pDataArray, int iDataNum);/* main function */void main(void){int i,a[10];int *p; printf("Please enter 10 numbers:\n");for(i = 0;i

< 10;i++) scanf("%d",&a[i]); Bubble_Sort(a,10); printf("排序后的顺序是:\n");for(i = 0;i < 10;i++) printf("%5d",a[i]); printf("\n"); system("pause");}/***********************************函数名称:BubbleSort **参数说明:pDataArray 无序数组 ** iDataNum为无序数据个数 * *说明: 冒泡排序 ************************************/ int* Bubble_Sort(int* pDataArray, int iDataNum) { int i,j,temp;for (i = 0; i < iDataNum - 1; i++) //变量i代表比较的趟数for (j = 0; j < iDataNum - i - 1; j++) //变量j代表每趟两两比比较的次数 if (pDataArray[j] >

pDataArray[j + 1]) { temp = pDataArray[j + 1]; pDataArray[j + 1] = pDataArray[i]; //Use intermediate variables to achieve binary interchange pDataArray[i] = temp; }return pDataArray; //return array pointer}

2. The results showed that:

Thank you for reading, the above is "C language bubble sort method how to use" the content of the, after learning this article, I believe we have a deeper understanding of how to use the C language bubble sort method, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Sort language element method algorithm number function learning content variable array step input complexity two code representative principle parameter name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Shulou Tech Info Microsoft Linux