2.1.26 original data type. Write a new version that can handle the insertion sorting of int values, and compare it with the implementation provided in the body (which can implicitly convert and sort integer values by automatic packing and unpacking.
A: The raw data type has a performance improvement of more than 15% compared with the object type.
Public class insertion
{
Public static void sort (comparable [])
{
Int n = A. length;
For (INT I = 0; I <n; I ++)
{
For (Int J = I; j> 0 & less (A [J], a [J-1]); j --)
Exch (A, J, J-1 );
}
}
Private Static Boolean less (comparable V, comparable W)
{Return v. compareto (w) <0 ;}
Private Static void exch (comparable [] A, int I, Int J)
{
Comparable T = A [I];
A [I] = A [J];
A [J] = T;
}
Private Static void show (comparable [])
{
For (INT I = 0; I <A. length; I ++)
Stdout. Print (A [I] + "");
Stdout. println ();
}
Public static Boolean issorted (comparable [])
{
For (INT I = 0; I <A. length; I ++)
If (less (A [I], a [I-1]) return false;
Return true;
}
Public static void main (string [] ARGs)
{
Int n = integer. parseint (ARGs [0]);
Double [] A = new double [N];
For (INT I = 0; I <n; I ++)
A [I] = stdrandom. Uniform (0.0, N * 1.0 );
//
Sort ();
}
}
Public class insertion4
{
Public static void sort (INT [])
{
Int n = A. length;
For (INT I = 0; I <n; I ++)
{
For (Int J = I; j> 0 & less (A [J], a [J-1]); j --)
Exch (A, J, J-1 );
}
}
Private Static Boolean less (int v, int W)
{Return v <W ;}
Private Static void exch (INT [] A, int I, Int J)
{
Int T = A [I];
A [I] = A [J];
A [J] = T;
}
Private Static void show (INT [])
{
For (INT I = 0; I <A. length; I ++)
Stdout. Print (A [I] + "");
Stdout. println ();
}
Public static Boolean issorted (INT [])
{
For (INT I = 0; I <A. length; I ++)
If (less (A [I], a [I-1]) return false;
Return true;
}
Public static void main (string [] ARGs)
{
Int n = integer. parseint (ARGs [0]);
Int [] A = new int [N];
For (INT I = 0; I <n; I ++)
A [I] = stdrandom. Uniform (0, N );
//
Sort ();
For (INT I = 0; I <n; I ++)
Stdout. printf ("% 3d", a [I]);
}
}
Public class sortcompare
{
Public static double time (string Alg, double [])
{
Stopwatch timer = new stopwatch ();
If (ALG. Equals ("insertion") insertion. Sort ();
// Exercise2.1.24
If (ALG. Equals ("insertion2") insertion2.sort ();
// Exercise2.1.25
If (ALG. Equals ("insertion3") insertion3.sort ();
// Exercise2.1.26
If (ALG. Equals ("insertion4") insertion3.sort ();
If (ALG. Equals ("selection") selection. Sort ();
If (ALG. Equals ("shell") Shell. Sort ();
// If (ALG. Equals ("merge") Merge. Sort ();
// If (ALG. Equals ("quick") Quick. Sort ();
// If (ALG. Equals ("Heap") Heap. Sort ();
Return timer. elapsedtime ();
}
Public static double timerandominput (string Alg, int N, int T)
{
Double Total = 0.0;
Double [] A = new double [N];
For (int t = 0; t <t; t ++)
{
For (INT I = 0; I <n; I ++)
A [I] = stdrandom. Uniform ();
Total + = Time (ALG, );
}
Return total;
} // End timerandominput
Public static void main (string [] ARGs)
{
String alg1 = ARGs [0];
String alg2 = ARGs [1];
Int n = integer. parseint (ARGs [2]);
Int T = integer. parseint (ARGs [3]);
Double T1 = timerandominput (alg1, N, t );
Double t2 = timerandominput (alg2, N, t );
Stdout. printf ("For % d random doubles \ n % s is", N, alg1 );
Stdout. printf ("%. 2f times faster than % s \ n", T2/T1, alg2 );
}
}
Algs4-2.1.26 raw data type