Java Data Structure Series-simple sorting: bubbling, simple selection, direct insertion

Source: Internet
Author: User

Package Simplesort;public class Simplesort {/** * bubble sort: Each cycle, the small number of rows in the back will be as slowly as the bubbles in the water, so named bubble sort, I guess this is ... public void Bubblesort (int[] array) {for (int. i=0;i<array.length;i++) {for (int j=array.length-1;j>i;j--) {// Note here that J is from the back-forward loop if (Array[j-1]>array[j]) {//If the preceding number is larger than the number after it, swap their two positions swap (ARRAY,J);}}} /** * Simple selection Sort: The basic idea is to start with the first number, each time you choose the smallest number that is smaller than his/her position * @param array */public void Selectsort (int[] array) {int min;for (int i=                                0;i<array.length-1;i++) {min=i;for (int j=i+1;j<=array.length-1;j++) {//Select the ratio of the array after I Array[i] Small minimum number and keep its subscript in min if (Array[min]>array[j]) {min=j;}} if (min!=i) {swap (array,i,min);}}} /** * Direct Insertion Sort: the idea of direct insertion sort is similar to the way we constantly touch and arrange the landlord when we are in daily life. * We will follow the cards in the back of the card from small to large insert into the corresponding position * @param array */public void Insertsort (int[] array) {int i,j,temp;for (i=1;i<array.length;i++) {if (array[i]<array[i-1]) {temp=array[i];for (j=i-1;j>=0&&array[j]>temp;j--) {//Be careful not to ignore j>=0, otherwise Ken can appear j=-1, causing the array to cross ARRAY[J+1]=ARRAY[J];} array[j+1]=temp;}}} Swap PubLIC void swap (int array[],int i,int j) {int temp=array[i];array[i]=array[j];array[j]=temp;} Exchange public void Swap (int array[],int j) {int temp=array[j-1];array[j-1]=array[j];array[j]=temp;}}

Java Data Structure Series-simple sorting: bubbling, simple selection, direct insertion

Related Article

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.