Java tests the efficiency of heap sorting, Bubble sorting, and quick sorting.

Source: Internet
Author: User

Package Ds;

Public class heapsort {

Public static void main (string [] ARGs ){

Long n = 10000;
Int [] arr = {11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38 };

System. Out. println ("integer array length:" + arr. Length );
System. Out. println ("sorting times:" + n );

Testheapsort (N );

Testbubblesort (N );

Testquicksort (N );

}

Private Static void testbubblesort (long n ){
Long start = system. currenttimemillis ();
For (long T = 0; t <n; t ++ ){

Int [] arr = {11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38 };
Bubblesort (ARR );
}
Long end = system. currenttimemillis ();
System. Out. println ("bubblesort:" + (end-Start) + "Ms ");
}

Private Static void bubblesort (INT [] ARR ){
For (INT I = 0; I <arr. Length-1; I ++ ){
For (Int J = I + 1; j <arr. length; j ++ ){
If (ARR [I]> arr [J]) {
Dsutils. Swap (ARR, I, j );
}
}
// Dsutils. printarray (ARR );
}
}

Private Static void testheapsort (long n ){
Long start = system. currenttimemillis ();
For (long T = 0; t <n; t ++ ){

Int [] arr = {11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38 };
Heapsort (ARR );
}
Long end = system. currenttimemillis ();
System. Out. println ("heapsort:" + (end-Start) + "Ms ");
}

Private Static void heapsort (INT [] ARR ){
For (INT I = arr. Length-1; I> 0; I --){
Builtheap (ARR, I );
Dsutils. Swap (ARR, 0, I );
// Dsutils. Print (I );
// Dsutils. printarray (ARR );
}
}

Private Static void builtheap (INT [] IA, int POS ){
Int c = Pos;
Int p = 0;
While (C> 0 ){
P = (c-1)> 1;
If (IA [c]> Ia [p]) {
Dsutils. Swap (IA, C, P );
}
C --;
}
}

Private Static void testquicksort (long n ){
Long start = system. currenttimemillis ();
For (long T = 0; t <n; t ++ ){

Int [] arr = {11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5,
6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7,
9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89,
14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99,
54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,
123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13,
23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38,
11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67,
89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43,
99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44,
17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8,
13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22,
38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55,
67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78,
43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6,
44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14, 22, 38, 11, 7, 9,
8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54, 55, 67, 89, 14,
22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123, 78, 43, 99, 54,
55, 67, 89, 14, 22, 38, 11, 7, 9, 8, 13, 23, 5, 6, 44, 17,123,
78, 43, 99, 54, 55, 67, 89, 14, 22, 38 };
Quicksort (ARR );
}
Long end = system. currenttimemillis ();
System. Out. println ("quicksort:" + (end-Start) + "Ms ");
}

Private Static void quicksort (INT [] IA ){
Quicksort (IA, 0, IA. Length-1 );
}

Private Static void quicksort (INT [] IA, int K1, int K2 ){

If (k1 <k2 & k1>-1 ){

If (k1 = k2-1 ){
If (IA [k1]> Ia [k2]) {
Dsutils. Swap (IA, K1, K2 );
}
} Else {

Int I = k1;
Int M1 = k1 + 1;
Int m2 = k2;
While (M1 <m2 ){

While (IA [M1] <Ia [I] & M1 <= m2 ){
Dsutils. Swap (IA, M1, I );
I = m1 ++;
}
While (IA [m2]> Ia [I] & M1 <= m2 ){
M2 --;
}
If (IA [m2] <Ia [I] & M1 <= m2 ){
Dsutils. Swap (IA, I, M1, M2 );
I = m1 ++;
}
M2 --;
}

Quicksort (IA, K1, I-1 );
Quicksort (IA, I + 1, K2 );

}
}
}

}

Integer array length: 672
Sorting times: 10000
Heapsort: 19875 MS
Bubble sort bubblesort: 25328 MS
Quick Sort quicksort: 3750 MS

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.