Select Sort (in ascending order as an example): through the inner loop the first time the group finds the smallest element to swap the position with the first element of the array, and the second time the group finds the second small element to swap the position with the second element of the array, This minimum element should be avoided the next time the memory loop finds the smallest element and swaps the position. This sort method is the time complexity of O (n²) for arrays of any structure
Public Static int[] Orderbyselect (int[] a) { for(inti=0;i<a.length;i++){ inttemp=A[i]; intflag=i; for(intj=i+1;j<a.length;j++){ if(temp>A[j]) {Temp=A[j]; Flag=J; } } if(flag!=i) {A[flag]=A[i]; A[i]=temp; } } returnA; }
Insert Sort (in ascending order as an example): Assuming that the first element is the smallest element, the second element is the size of the first element, and if the second is less than the first, then the position is swapped, when the first and second are sorted, and the third element is compared to the second element that is already lined up, If it is greater than the second, the next cycle is performed, otherwise the interchange position is compared to the first element, and the outer control loop arrives until the end of the array is reached. The advantage of this sort of ordering is that if the array itself is already partially sequenced, then the comparison in the subsequent comparisons when compared to the maximum value above the maximum value will ignore the comparison with the other elements, saving time.
Public Static int[] Orderbyinsert (int[] a) { for(inti=1;i<a.length;i++){ intTemp=a[i];//saves the value currently being used for insertion intJ=i-1;//Subscript for traversing a subset that is already ordered if(Temp<a[j]) {//determines the maximum size of a subset and the current value, and does not require a loop if the current value is large while(J>=0 && a[j]>temp) {//modifies the position of the current value if the element of the subset is greater than the current valueA[J+1]=A[J];//moves the value of the position of J forward to hold the current valuej--;//go to the next cycle} a[j+1]=temp;//all values greater than temp in the subset after the end of the loop are moved one step forward, when the j+1 position is where temp should be inserted } } returnA; }
bubble Sort (in ascending order as an example): The idea of bubbling sort is to determine the size of the adjacent elements from left to right (right to left), if the latter element is less than the previous element, the swap position, and the maximum value will be on the far right after the round loop.
Public Static int[] Orderbybubble (int[] a) { for(inti=0;i<a.length;i++){ for(intj=0;j<a.length-i-1;j++){ if(a[j]>a[j+1]){ inttemp=A[j]; A[J]=a[j+1]; A[j+1]=temp; } } } returnA; }
merge sort (in ascending order as an example): merge
Some basic sorting algorithms for Java