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

What are the common sorting algorithms

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

Share

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

This article introduces the relevant knowledge of "what are the common sorting algorithms". In the operation of actual cases, many people will encounter such a dilemma. Then 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!

Bubbling sort

One of the most common sorting algorithms is to compare two adjacent elements at a time and exchange positions if necessary.

It is clear at a glance at the motion picture below.

Code implementation:

Public static int [] sort (int [] arr) {if (arr.length)

< 2){ return arr; } //定义一个标志位,主要考虑到已经排好序的数组,避免不必要的计算 boolean flag; for(int i=1;i 0; j--) { if (temp < arr[j - 1]) { //符合条件往后挪 arr[j] = arr[j - 1]; } else { //此处break不能省略,用来停止 j 的自减 break; } } arr[j] = temp; } return arr; } 上面这种写法容易直观也容易理解;如果你能理解上面的写法,那么可以进一步把if判断表达式进行提取: public static int[] sort1(int[] arr) { if (arr.length < 2) { return arr; } for (int i = 1; i < arr.length; i++) { int temp = arr[i]; int j = i; for (; j >

0 & & temp < arr [j-1]; JMub -) {/ / move arr [j] = arr [j-1];} arr [j] = temp;} return arr;} "what are the common sorting algorithms" so far, 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

Development

Wechat

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

12
Report