Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common sorting algorithms

Shulou Source: shulou.com Published: 2022-06-03 05:55:12 09月26日 Update

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!

Tags: Sorting algorithms complexity common code elements arrays locations content writing animation sequences performance performance analysis time more conditions knowledge space Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft MySQL MariaDB NVidia