Three simple sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.