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

Example Analysis of Bubble sorting in C language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shares with you the content of an example analysis of bubbling sorting in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

(1) Bubble sort 1.1 Design of Bubble sort

Bubble sorting (Bubble Sort) is also a simple and intuitive sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence is repeated until there is no need for swapping, that is, the sequence has been sorted. The algorithm gets its name because smaller elements slowly "float" to the top of the sequence by swapping.

1.2 steps to bubble sort

Compare adjacent elements. If the first is bigger than the second, exchange the two of them.

Do the same for each pair of adjacent elements, from the first pair to the last pair. After this step is done, the last element will be the maximum number.

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

Continue to repeat the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.

? Moving picture demo 1 (data is small)

? Moving picture demo 2 (large data)

1.3 implementation of bubble sorting

Tips: each bubble sort reduces the number that needs to be compared-> because each bubble sort moves the maximum / minimum of the comparison to the end of the array

(II) the array is used as a function parameter

Often when we write code, we will pass the array as an argument to a function, for example: I want to implement a bubble sort (here we want to talk about the algorithm idea) function to sort an integer array. Then we will use the function as follows:

2.1 wrong Design of Bubble sorting function

Let's take a look at an example.

The final result:

Did not achieve the function of ranking as expected.

In our previous study, we knew the address of the first element of the array represented by the array name.

When passed to a function, the array name is degraded to the address of the first element

When the array list is placed within the sizeof alone, such as: sizeof (arr), where the arr represents the entire array rather than the address of the first element

Perform the operation on the array name & after taking the address

We know that self-incrementing a pointer to an array element is skipping an element (the address of the number of bytes occupied), but essentially skipping the size of its pointer type. After taking the address of the array name, you get the address of the array, and if you print it with the address of the first element of the array and the name of the array, you will find that their values are the same. But if you do operations such as + 1 on & arr, you will find that what is skipped is not the size of an element, but the size of an array.

2.2 correct Design of Bubble sorting function

When an array passes parameters, it actually passes the address of the first element of the array. So even if the parameter part of the function is written as an array: int arr [] still represents a pointer: int * arr. So, the sizeof (arr) result inside the function is 4.

(3) extended parsing of array names

The array name is the address of the first element, with two exceptions

Sizeof (array name), where the array name represents the entire array, and sizeof (array name) represents the size of the entire array

& the array name. The array name here represents the entire array and the address of the entire array is taken out.

& arr fetches the address of the entire array, so arr + 1 skips the size of the entire array as

4 * 10 = 40 byte address size

Thank you for reading! This is the end of this article on "sample Analysis of Bubble sorting in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

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

Development

Wechat

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

12
Report