1. Bubble sort
Bubble sort is a simple sort algorithm, its basic idea is as follows:
1) starting from the first, compare the size of the adjacent two elements, if the previous one is larger than the next, then the exchange (that is, the small jump up).
2) and then compared with the previous adjacent elements, if small, then take up.
2) Repeat the above operation until the last element.
1 Public Static voidBubblesort (Long[] arr) {2 LongTMP = 0;3 for(inti=0;i<arr.length-1;i++){4 for(intj=arr.length-1;j>i;j--){5 if(Arr[j] < arr[j-1]){6TMP =Arr[j];7Arr[j]=arr[j-1];8arr[j-1]=tmp;9 Ten } One } A } -}
2. Select sort
The choice of sorting is also a simple sort algorithm, the basic idea is as follows:
1) The outer layer of the first loop, the first element as a temporary comparison element, the inner loop to find the smallest element, to exchange.
2) The outer layer carries out a second loop, the second element as a temporary comparison element, and the inner loop finds the smallest element for exchange.
3) Repeat the above operation until the loop ends.
1 Public Static voidSelectsort (Long[] arr) {2 intK=0;3 LongTMP = 0;4 for(inti=0;i<arr.length-1;i++){5k=i;6 for(intj=i;j<arr.length;j++){7 if(arr[j]<Arr[k]) {8k=J;9 }Ten } OneTMP =Arr[i]; Aarr[i]=Arr[k]; -arr[k]=tmp; - } the}
3. Insert Sort
Insert sort is also a simple sort algorithm, its basic idea is as follows:
1) Start the loop from the second element (because it is inserted from the previous comparison after the trip) as a temporary comparison element, and then compare the previous element with the temporary comparison element.
2) If the previous element is larger than the temporary comparison element, the previous element is assigned to the latter element.
3) Then assign the temporary comparison element to the front.
1 Public Static voidInsertsort (Long[] arr) {2 intTMP = 0;3 for(inti=1;i<arr.length;i++){4TMP =Arr[i];5 intj=i;6 while(J>0 && arr[j]>=tmp) {7Arr[j]=arr[j-1];8j--;9 }Tenarr[j]=tmp; One } A}
Three simple sorting