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 realize Bubble sorting and selective sorting by Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how Java to achieve bubble sorting and selection sorting, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

one。 Bubble sort 1. Concept

In fact, the key word of Bubble sorting method lies in the word Bubble. As the name implies, the number is constantly compared and then the largest protruding, that is to say, the two adjacent numbers are compared. When a number is larger than the adjacent number on the right, exchange their positions. When a number and the number on the right side are less than or equal to, do not exchange.

two。 Graphic illustration

About bubbling sorting, I drew a picture myself to describe his round of process.

Here I give five disordered numbers {7, 3, 6, 5, 5, 4]

From this we can see that he constantly compares with the adjacent numbers on the right, and when he is larger than the number on the right, he will exchange, otherwise he will not exchange, so he will continue to sort for many rounds with this method, and finally get an ordered sequence {2, 4, 5, 5, 6, 7}.

3. The idea of the code

Bubbling cycle is an orderly sequence. With the picture above, it is not difficult to see that a bubble cycle needs to be compared once and then open the next layer, and after you have finished comparing in one round, the last maximum number will no longer have to participate in the comparison cycle, so the next cycle can be reduced one time.

So my idea is to use two for loops, one loop to take the number of the second loop, and then reduce the number of the second for loop in turn, which is a lot easier, and then compare the two adjacent numbers of the array in turn, and the one on the right is larger than the exchange position of the next bit, otherwise it will not be exchanged, so the code for my two for loops is as follows (in fact, this is also a universal code. (for bubbling loops) of course, iMagol k _ (b) needs to be defined, where n is the number of elements in the comparison sequence.

This code also uses two for loops, so it is not difficult to calculate the time complexity as o (n ^ 2).

For [I + 1]) / / {b = a [I]; a [I] = a [I + 1]; a [I + 1] = b } printf ("discharge sequence is:"); for (I = 0; I

< 10; i++) printf("%d ", a[i]); return 0;} 最后运行也是成功的 二.选择排序1.概念 选择循环的主要是选择,选择一个元素去进行比较,假如这个数比他小,换成嘞个数继续比较,最后找出最小值与数组第一位的数进行交换。然后一直这样循环下去,直到代码排序完成。也就是通过一个中间量从带排序的的数中找出最大或最小的交换到对应位置。 2.图解 关于选择排序我自己花了一幅图来描述他的一轮比较 我用了五个无序数来做例子{2,1,4,-3,3}

From this we can see the process of selecting and comparing, here I use the pointer to refer to the required intermediate quantity, at first the pointer points to 2, then 2 to compare, 1

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