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);
}
}