"Java_base" Common sorting algorithm: Bubble sort, select sort, insert sort

Source: Internet
Author: User

Bubble sort
Compares the adjacent elements. If the first one is bigger than the second one, swap them both. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number. Repeat the above steps for all elements, except for the last one. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

Select sort
Compare the previous element in the array with the size of the next element, if the following element is smaller than the previous element with a variable K to remember his position, and then the second comparison, the front "next element" has now become "the previous element", continue with his "latter element" To compare if the next element is smaller than he would use the variable K to remember its position in the array (subscript), and when the loop ends, we should find the lowest number of the subscript, and then to judge, if the index of the element is not the first element subscript, let the first element to exchange values with him, This will find the smallest number in the array. Then find the second small number in the array, let him swap the value with the second element in the array, and so on.
Insert Sort
Starting with the first element of the array, a[0], insert the next element a[1] into the front or back of a[0], and continue the process. Insert A[i] into the appropriate position in the sorted a[0]~a[i-1] every time

1  Public classsort{2      Public Static voidMain (string[] args) {3         intarray[]={12,76,34,66,44,23,88,77,56,99};4SYSTEM.OUT.PRINTLN ("Array to be sorted as:");5          for(intArr:array) {6System.out.print (arr+ "");7         }8         9         //Bubble SortTenSystem.out.println ("\ n" + "bubble sort (from large to small):"); One Bubblesort (array); A         //Select Sort -System.out.println ("\ n" + "select sort (from small to Large):"); - Selectionsort (array); the         //Select Sort -System.out.println ("\ n" + "insert sort (from small to Large):"); - Insertsort (array); -     } +     //Bubble Sort -      Public Static voidBubblesort (int[] a) { +         intn=a.length; A         //I is the number of comparisons, a total comparison of n-1 times at          for(inti=1;i<n;i++){ -             //J is the subscript for the first element to be compared -              for(intj=0;j<n-1;j++){ -                 if(a[j]<a[j+1]){ -                     intTemp=a[j+1]; -a[j+1]=A[j]; ina[j]=temp; -                 } to             } +         } -         //traversing sorted arrays the          for(intk=0;k<n;k++){ *System.out.print (a[k]+ ""); $         }Panax Notoginseng     } -     //Select Sort the      Public Static voidSelectionsort (int[] a) { +         intn=a.length; A         //I was to exchange worthy subscript, the          for(inti=0;i<n-1;i++){ +             //K Record the subscript of the current minimum value -             intk=i; $             //find the location of the minimum value from the position of the k+1 $              for(intj=k+1;j<n;j++){ -                 //from small to large -                 if(a[j]<A[k]) { thek=J; -                 }Wuyi             } the             //If K is not the subscript for the first element, let the first element swap with him for a value -             if(i!=k) { Wu                 inttemp=A[i]; -a[i]=A[k]; Abouta[k]=temp; $             } -         } -         //traversing sorted arrays -          for(intk=0;k<n;k++){ ASystem.out.print (a[k]+ ""); +         } the     } -     //Insert Sort $      Public Static voidInsertsort (int[] a) { the         intn=a.length; the         //I is the subscript for the position to be inserted the          for(inti=1;i<n;i++){ the              for(intj=i;j>0;j--){ -                 //sort from small to large in                 if(a[j]<a[j-1]){ the                     intTemp=a[j-1]; thea[j-1]=A[j]; Abouta[j]=temp; the                 } the             } the         } +         //traversing sorted arrays -          for(intk=0;k<n;k++){ theSystem.out.print (a[k]+ "");Bayi         } the     } the}

"Java_base" Common sorting algorithm: Bubble sort, select sort, insert sort

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.