Randomization of Algs4-2.3.29

Source: Internet
Author: User

2.3.29 randomization. Empirical studies are used to compare the effects of random selection of sharding elements and randomization of arrays at the beginning of the text. Switch when the sub-array size is m, and sort the non-repeated arrays whose size is n, where M = 10, 20, and 50, n = 10 ^ 3, 10 ^ 4, 10 ^ 5, and 10 ^ 6.
A: Because the generated array to be sorted is already randomized, it takes time for method 1 to disrupt the array before sorting. Therefore, method 1 takes longer than method 2, therefore, when the array to be sorted is randomization, the comparison between the two methods is not particularly strong.

Public class e2d3d29
{
Public static void main (string [] ARGs)
{
Int nlen [] = {1000,10000, 100000,000000 };
Int mlen [] = {10, 20, 50 };
For (INT ni = 0; Ni <nlen. length; Ni ++)
{
Int n = nlen [Ni];
For (int mi = 0; Mi <mlen. length; Mi ++)
{
Int M = mlen [MI];
Double s1times = 0.0;
Double s2times = 0.0;
For (INT time = 0; time <10; time ++)
{
Double [] a1 = new double [N];
Double [] a2 = new double [N];
For (INT I = 0; I <n; I ++)
{
A1 [I] = stdrandom. Random ();
A2 [I] = A1 [I];
}
Stopwatch timer1 = new stopwatch ();
E2d3d29s1. Sort (A1, M );
S1times = s1times + timer1.elapsedtime ();
//
Stopwatch timer2 = new stopwatch ();
E2d3d29s2. Sort (A2, M );
S2times = s2times + timer2.elapsedtime ();
}
Stdout. printf ("n = % 7D, M = % 2D, shuffleelapsedtime = %. 3f, randomelapsedtime = %. 3f, shuffle-random = %. 3f \ n ", n, m, s1times/10, s2times/10, (s1times + s2times)/10 );
}
} // End for Ni
} // End main
} // End class

Public class e2d3d29s1
{
Public static void sort (comparable [] A, int m)
{
Stdrandom. Shuffle ();
Sort (A, 0, A. Length-1, M );
}

Private Static void sort (comparable [] A, int Lo, int hi, int m)
{
If (Hi-Lo + 1 <m)
{
Insertsort (A, lo, hi );
Return;
}
Int J = partition (A, lo, hi );

Sort (A, lo, J-1, M );
Sort (A, J + 1, hi, M );
}

Private Static int partition (comparable [] A, int Lo, int hi)
{
Int I = Lo, j = Hi + 1;
Comparable v = A [lo];
While (true)
{
While (less (A [++ I], V) if (I = Hi) break;
While (less (V, A [-- J]) if (j = LO) break;

If (I> = J) break;
Exch (A, I, j );
}
Exch (A, lo, J );
Return J;
}

Private Static void insertsort (comparable [] A, int Lo, int hi)
{
For (INT I = lo + 1; 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 = 1; 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 K = 0; k <n; k ++)
A [k] = stdrandom. Random ();
Sort (A, 10 );
Stdout. println ("issorted =" + issorted ());
}
}


Public class e2d3d29s2
{
Public static void sort (comparable [] A, int m)
{
Sort (A, 0, A. Length-1, M );
}

Private Static void sort (comparable [] A, int Lo, int hi, int m)
{
If (Hi-Lo + 1 <m)
{
Insertsort (A, lo, hi );
Return;
}
Int J = partition (A, lo, hi );

Sort (A, lo, J-1, M );
Sort (A, J + 1, hi, M );
}

Private Static int partition (comparable [] A, int Lo, int hi)
{
Int I = Lo, j = Hi + 1;
Exch (A, lo, stdrandom. Uniform (Lo, hi + 1 ));
Comparable v = A [lo];
While (true)
{
While (less (A [++ I], V) if (I = Hi) break;
While (less (V, A [-- J]) if (j = LO) break;

If (I> = J) break;
Exch (A, I, j );
}
Exch (A, lo, J );
Return J;
}

Private Static void insertsort (comparable [] A, int Lo, int hi)
{
For (INT I = lo + 1; 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 = 1; 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 K = 0; k <n; k ++)
A [k] = stdrandom. Random ();
Sort (A, 10 );
Stdout. println ("issorted =" + issorted ());
}
}

Randomization of Algs4-2.3.29

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.