public class Directrank {
public static void Main (string[] args) {
Directrank aa=new Directrank ();
try {
Aa.judge (args);
} catch (Testdefinexception E2) {
E2.printstacktrace ();
}
Integer[] A=new integer[args.length];
try{for (int i=0;i<args.length;i++)
{System.out.print (args[i]+ "");
A[i]=a[i].parseint (Args[i]);//Convert the read-in string type to Intgr
}
System.out.println ();
}
catch (NumberFormatException e)
{System.out.println ("\ n input data is incorrect \ n");}
catch (NullPointerException E1)
{e1.getmessage ();}
Aa. Rank (a);
System.out.println ("Direct sorting results");
for (int j=0;j<a.length;j++)
System.out.print (a[j]+ "*");
Aa.mopao (a);
System.out.println ("\ n bubble sort");
for (int j=0;j<a.length;j++)
System.out.print (a[j]+ "#");
}
/*
* Direct Insert Sort
*/
void Rank (Integer[]a) {
for (int i=1;i<a.length;i++)//insert starting with the second element
{
int temp=a[i];//set a staging variable
int j=i-1;//Determine the element to compare
while (J>=0&&temp<a[j])//Note here must first compare j>0 otherwise will be a group out of bounds exception array Indx out of Bound Exception
{a[j+1]=a[j];//data moves right
j--;//move left to continue next element comparison
}
a[j+1]=temp;//inserting the elements of the comparison into the appropriate position
}
}
void Judge (String[]args) throws testdefinexception{
if (args.length<=0)
throw new Testdefinexception ("Please enter data! ");
}
/*
* Bubble Sort
*/
void Mopao (Integer []a) {
for (int i=0;i<a.length-1;i++)
{for (int j=1;j<a.length-i;j++)
{
if (A[j]<a[j-1])
{int temp=a[j-1];
A[J-1]=A[J];
A[j]=temp;
}
}
}
}
}
public class Xierrank {
public static void Main (string[] args) {
int s[]={49,38,65,97,76,13,27,49,78,34,12,64,1};
Xierrank x=new Xierrank ();
X.shsort (s);
for (int a=0;a<s.length;a++)
{
System.out.println ("\t[" +s[a]+ "]");
}
}
public void Shsort (int s[]) {
int d=s.length/2;//determining step size
while (d>=1)
{for (int i=d;i<s.length;i++)//Compare starting with the first D element
{
int temp=s[i];//set a staging variable
int j=i-d;//left Shift D
while (j>=0&&s[j]>temp)
{
s[j+d]=s[j];//Data Shift Right
j=j-d;//continue to the left comparison
}
s[j+d]=temp;//inserting a determined position
}
D=D/2;
}
}
}
Direct Insert sort && bubble sort && Hill sort (reduce incremental sort) source code