Home >Web Front-end >JS Tutorial >Example summary of four basic sorting methods in JAVA_javascript skills
The examples in this article describe the four basic sorting methods in JAVA. Share it with everyone for your reference. The details are as follows:
JAVA has four basic sorting methods, including bubble method, insertion method, selection method, and SHELL sorting method. The selection method is an improvement of the bubble method, and the SHELL sorting method is an improvement of the insertion method. So fundamentally it can be summarized There are two different sorting methods: insertion method & bubble method
1 Insertion method:
Traverse the sorted collection. Every time an element is reached, this element is traversed and compared with all the elements before it, and the elements that meet the sorting order are moved one by one to the position where it should appear in the current range. Exchange is implemented by adjacent traversal movement and double loop control. This sorting method belongs to the local snake type. On my land cards, I have to arrange everything in a certain order. Come one over and arrange the other.
The processing code is as follows:
public void sort(int[] data) { int temp; for(int i=1; i〈data.length; i++){ for(int j=i; (j〉0)&&(data[j]〉data[j-1]); j--){ temp=date[j]; data[j]=data[j-1]; data[j-1]=temp; } } }
2 Bubble method:
It’s relatively easy. Its inner loop ensures that after one traversal, the smallest (largest) element in the set appears in its correct position, and the next time it is the next smallest element. . . This method has basically the same number of exchange moves under various circumstances of set distribution, and is the slowest sort. The implementation is also double loop control. This sorting method belongs to the Dragon Crossing the River, which means finding the extreme. However, the Dragon Crossing the River also has an eldest brother, a second brother, etc., so they can only choose the eldest brother and the second brother.
The processing code is as follows:
public static int [] maopao(int[] data) { int temp; for(int i=0; i〈data.length-1; i++){ for(int j=i+1; j〈data.length; j++){ if(data[i]〈data[j]){ temp=data[i]; data[i]=data[j]; data[j]=temp; } } } return data;
Three selection methods:
This method only records the position of the smallest (large) element by traversing the set. After one traversal, it performs a position swap operation, which is similar to bubbling. However, during the comparison process, no swap operation is performed, and only the position of the element is recorded. Only one exchange operation is performed per traversal. This pair is more suitable for elements whose exchange order is time-consuming. This sorting method is much deeper than the bubble method. I remember the extreme data first, and then process it after traversing the data. Unlike the bubble method, which only needs to be processed if it is a little more extreme than myself, the selection method only Handles the most extreme data within its scope.
public static void xuanze(int[] data) { int temp; for (int i = 0; i 〈 data.length; i++) { int lowIndex = i; for (int j = data.length - 1; j 〉 i; j--) { if (data[j] 〉 data[lowIndex]) { lowIndex = j; } } temp=data[i]; data[i]=data[lowIndex]; data[lowIndex]=temp; } }
Four Shell sorting:
It is an improvement on insertion sort. It considers dividing the set elements into groups according to a certain base for sorting, so that each group is first arranged in basic order within the local scope, and finally all elements are sorted once. Insertion sort.
public void sort(int[] data) { for(int i=data.length/2; i〉2; i/=2){ for(int j=0; j〈i; j++){ insertSort(data,j,i); } } insertSort(data,0,1); } private void insertSort(int[] data, int start, int inc) { int temp; for(int i=start+inc; i〈data.length; i+=inc){ for(int j=i; (j〉=inc)&&(data[j]〈data[j-inc]); j-=inc){ temp=data[j]; data[j]=data[j-inc] data[j-inc]=temp; } } }
I hope this article will be helpful to everyone’s Java programming design.