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 apply Java Bubble sorting method and selective sorting method

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Java bubble sorting method and selection sorting method how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Java bubbling sorting method and selection sorting method how to use" it!

Bubbling sorting method

Bubble sorting algorithm, which is talked about a lot in traditional C language textbooks, is a relatively stable sorting algorithm. When you use this sorting algorithm, you can associate its implementation form from its name. When it comes to bubbling, the first thing that comes to mind is a small fish swimming in the water, and Brubro spits out a string of small bubbles and rises to the surface. In fact, the bubbling sorting method is also the same as less than spitting bubbles, spitting out only one at a time, and spitting one after another continuously. The central idea of the bubble sorting algorithm is that two adjacent numbers are compared and the positions are exchanged according to the size. Start with the array of the two simplest elements, and then draw an example. Suppose there are only two elements inside an array, "int array = {8,0};". When sorting it, we only need to make a judgment to know which element is big and which is small. Suppose we arrange it from small to large, then the result should be "array = {0,8};". And look at an array with three elements. Suppose there are only two elements inside an array, "int array = {8,0,1};". Then let's make a pairwise comparison. The first comparison shows that the array should be "array = {0,1,8};", and the array can be sorted only once. But if the array changes the position of the elements, that is, "int array = {8,1,0};", then let's take a look again, the first two-element comparison becomes "array = {1,0,8};", so in this extreme case, the bubbling method cannot complete the order at one time, so we should make a second comparison, and we can get the result "array = {0,1,8}". "Let's take a look at the sorting of the array of the four elements. This time we take an extreme case, that is, an array arranged from large to small to ordered from small to large. The array is "int array = {9,8,1,0};". Then the first comparison of two adjacent elements can get "array = {8,1,0,9};", the second pairwise comparison of adjacent elements can get "array = {1,0,8,9};", and the third pairwise comparison can get "array = {0,1,8,9};". Based on the above analysis, we can know that if there are n elements in an array that need to be sorted, the extreme case of sorting should be nmuri once. The specific sorting process is as follows.

The process of bubbling sorting method

Therefore, according to the above analysis, we can write the code as follows.

Next, we modify the program so that it is printed out in the process of comparing two adjacent elements at each step, as shown in figure 5-4-3. We can see that the larger elements will slowly "float" to the top of the sequence (ascending or descending order) through the exchange, just as the bubbles spit out by the little goldfish in the water slowly surface, hence the name "bubble sort".

Bubble sorting single-step printing

Selective sorting method

Select sort, commonly known as "tough sort", of course, this "tough sort" is the name I gave it, because it is the most intuitive sorting method, a perfect interpretation of the word "aesthetics of violence". To understand the order of choice, imagine how teachers line up when they are in PE class in primary school. First, randomly pull out a student who the teacher thinks is the shortest among the children, let him be the head of the row, and then compare the other students with him in turn. If they are taller than him, put them behind them, shorter than him, put them in the front, and then take a visual look at the second one, and so on. Of course, the above paragraph describes the inner train of thought of the PE teacher. We can also use this method when sorting arrays. We can first designate a vanguard, and if we want to arrange from small to large, then we first assume that the first element is the smallest element in the array, and then compare it with the remaining elements, and if we find something smaller than it, then swap itself with that element, in this way, you only need to traverse the entire array, and you can put the smallest element in the position of the first element. As shown below.

Select sort to do an ergodic comparison

In the figure above, we arrange the smallest elements to the far left of the array through the first traversal comparison, and the next thing to do is to compare the remaining nine elements at once, find the minimum value, put it to the right of 0, and so on. Finally, we can write the selection sorting program shown in the following figure.

Thank you for your reading, the above is the content of "how to use Java bubbling sorting method and selective sorting method". After the study of this article, I believe you have a deeper understanding of how to use Java bubbling sorting method and selective sorting method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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