Java implementation of sorting algorithm

Source: Internet
Author: User

The following are the Java implementations of several sorts:

Bubble sort

public class Bubblesortdemo
{
/**//*
* The previous to back scan sequence, comparing the adjacent two element size, if found in reverse order swap position,
* so that the largest reached the final position of the sequence 。 And then scan the rest of the sequence from the back to the front, so.     
* Do not continue looping after sequence
*/
private void Bubblesort (int[] numlist)
{
int in,out,temp;
Boolean flag = false;
//From the last to the second scan sequence.       
for (out=numlist.length-1; out>0;out--)
{
flag = true;
From the first to the out item scan sequence
for (in=0; in<out; in++)
{
//current item is greater than bottom, swap location.           
if (numlist[in]>numlist[in+1])
{
temp = numlist[in];
Numlist[in] = numlist[in+1];
Numlist[in+1] = temp;
flag = false;
}
}
if (flag) break;
      
for (int i=0; i<numlist.length; i++)
System.out.print (numlist[i]+ "");
System.out.println ();
}
}
//output sequence
public void display (int[] numlist)
{
for (int i=0; i<numlist.length; i++)
System.out.print (numlist[i]+ "");
System.out.println ();
}

public static void Main (string[] args)
{
Bubblesortdemo sort = new Bubblesortdemo ();
int num[] = {4,11,17,6,1,2,13,15};     

System.out.println ("Original order is:");
Sort.display (num);
System.out.println (after each bubble sort is:);
Sort.bubblesort (num);
}
}

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.