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 C language to realize bubbling sorting

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

Share

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

This article introduces the relevant knowledge of "how to use C language to achieve bubble sorting". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Principle:

Take the number of 6 as an example: bubble sort of 4 56 25 13 8 6 (1 and 2, 2 and 3, 3 and 4, 4 and 5, 5 and 6)

First round:

No.1:56 4 25 13 8 6

No.2:56 25 4 13 8 6

No.3:56 25 13 4 8 6

No.4:56 25 13 8 4 6

No.5:56 25 13 8 6 4 (minimum)

Through the first round, we can see that a minimum number can be found through five comparisons.

Second round:

No.1:56 25 13 8 6

No.2:56 25 13 8 6

No.3:56 25 13 8 6

No.4:56 25 13 8 6 (minimum)

Thus it can be seen that the minimum number is determined after four comparisons in the second round.

..

The following is not a comparison, it can be inferred that each round to determine a minimum value, then six numbers, it takes five rounds of comparison to determine the order. If there are n numbers, it is necessary to compare n-1 rounds to determine the order of the numbers.

It can also be introduced that the first round needs to be compared 5 times, and the second round needs to be compared 4 times, so if there is n number, the first round needs to compare n-1 times and the second round needs to compare n-2 times.

Important: if I represents the first round and j represents the comparison of j times per round, then the cycle should be designed like this

For (I = 0; I < n-1; I + +)

{

For (j = 0; j < n-1-I; j + +)

{

.

.

.

}

}

Let's use a simple practical example to illustrate:

Write a program with c to read 10 numbers from the keyboard and sort them by bubbling sort.

Program:

/ *

17:31:59 on 27 October 2017

Function: enter 10 numbers and sort them by bubbling method.

, /

# include

# include

# define NUM 10

Int main ()

{

Int num = NUM

Int array [NUM]

Int i, j

Int temp

Printf ("Please enter% d integer numbers:", num)

For (I = 0; I < NUM; I + +)

{

Scanf ("% d", & array [I])

}

/ / sort

For (I = 0; I < NUM-1; I + +)

{

For (j = 0; j < NUM-1-I; j + +)

{

If (Array [j] < array [j + 1])

{

Temp = array [j]

Array [j] = array [j + 1]

Array [j + 1] = temp

}

}

}

Printf ("The numbers after being sorted:\ n")

For (I = 0; I < NUM; I + +)

{

Printf ("% d\ t", array [I])

If ((I + 1)% 5 = = 0) / / 5 digits per row

{

Printf ("\ n")

}

}

Return 0

}

/ *

The output in Code::Blocks is as follows:

Please enter 10 integer numbers:12 25 65 32 24 59 85 64 49 5

The numbers after being sorted:

85 65 64 59 49

32 25 24 12 5

17:49:05 on 27 October 2017

Experience: the inventor of bubble ranking must have a high IQ, and it is a challenge to intelligence for the first time. In short, if you want to understand the bubble sorting algorithm, you must understand its principle.

, /

The above example is to use an array to store 10 numbers. Use the simplest programming method to achieve bubble sorting, and do not use pointers and dynamically open up memory space to write this program. One is to think that it is unhealthy.

It is difficult to understand; second, it may not be able to make it up.

This is the end of the content of "how to use C language to achieve bubble sorting". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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