Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Bubble sorting method of C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report