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

An example Analysis of Java Bubble sorting

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

Share

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

This article mainly explains the "Java bubble sort example analysis", 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 bubble sort example analysis" bar!

Bubbling sorting principle

① compares adjacent elements, and if the previous element is larger than the latter, swap the positions of the two elements

② cycles through the above steps for each pair of adjacent elements, and the final last element is the maximum value.

Bubble sort API Design Class name Bubble Construction method Bubble: create Bubble object member method

1.public static void sort (Comparable [] a): sorts elements in an array

2.private static void greater (Comparable v comparison w); determine whether v is greater than w

3.private static void exchange (Comparable [] a drawing int x int y): swap the values at index x and index y in array a

The code for bubble sorting realizes that public class Bubble {/ / sort array a public static void sort (Comparable [] a) {for (int itima.futhmuri 1 Comparable I > 0 Comparable iMub -) {for (array element x and y exchange position private static void exchange (Comparable [] a mai int x int y) {Comparable tinca [x]; a [x] = a [y] A [y] = t;}} / / Test code class Test {public static void main (String [] args) {Integer [] a = {4, 5, 6, 3, 2, 1}; Bubble.sort (a); System.out.println (Arrays.toString (a));}}

Test results:

Analysis of time complexity of Bubble sorting

Although bubble sorting uses double-layer for loop traversal, the code that really completes the sorting is in the inner loop, so you can mainly analyze the execution times of the inner loop body.

In a worst-case scenario. That is to say, the reverse order of the array is {6 ~ 5 ~ 4 ~ 3 ~ 2 ~ 1}.

The number of comparisons of elements is: (NMU1) + (NMU2) + (NMUL3) +. + 2cm 1 =

((Nmur1) + 1) * (NMur1) / 2 = N ^ 2 / 2-N/2

The number of exchanges of elements is: (NMU1) + (NMUL2) + (NMUL3) +. + 2cm 1 =

((Nmur1) + 1) * (NMur1) / 2 = N ^ 2 / 2-N/2

The total number of execution is: 2 * (N ^ 2 / 2-N/2) = N ^ 2-N

According to the big O derivation rule, the highest order term is retained, that is, the time complexity of bubbling sorting is O (N ^ 2).

Thank you for your reading, the above is the content of "Java Bubble sorting example Analysis". After the study of this article, I believe you have a deeper understanding of the problem of Java Bubble sorting example analysis, 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

Development

Wechat

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

12
Report