JAVA Implementation of Sorting Algorithm bubble, selection, insert, merge, Hill, and quick

Source: Internet
Author: User

Package source. datastruct_algorithm;

Import java. util. Random;

Public class sortutil {
Static int size = 10000;
Static virtualobj [] persion;
Static long [] arr;

// Initialize Test Data
@ Suppresswarnings ("unchecked ")
Static synchronized void Init (){
Persion = new virtualobj [size];
Arr = new long [size];
Random ran = new random ();
For (INT I = 0; I <size; I ++ ){
Char num = (char) (math. Random () * 196 + 'A ');
Persion [I] = new virtualobj ("Tom" + (char) Num, num, num );
}
For (INT I = 0; I <size; I ++ ){
Arr [I] = ran. nextint (10000 );
}
Proxy. serviceproxy (). initlongarr (ARR );
Proxy. serviceproxy (). initobjarr (persion );
}

// Set an integer Array
Public void setbasearray (long [] array ){
Arr = array;
Size = array. length;
}
// Set the spot Array
Public void setobjarray (object [] ARR ){
Persion = (virtualobj []) Arr;
Size = persion. length;
}

// Bubble sort
Public static void bubblesort (){
Proxy. serviceproxy (). basebubblesort ();
Proxy. serviceproxy (). objbubblesort ();
}

// Select sorting
Public static void selectsort (){
Proxy. serviceproxy (). baseselectsort ();
Proxy. serviceproxy (). objselectsort ();
}

// Insert sorting
Public static void insertsort (){
Proxy. serviceproxy (). baseinsertsort ();
Proxy. serviceproxy (). objinsertsort ();
}

// Merge and sort
Public static void mergesort (){
Proxy. serviceproxy (). baseunionsort ();
Proxy. serviceproxy (). objunionsort ();
}

// Sort by hill
Public static void shellsort (){
Proxy. serviceproxy (). baseshellsort ();
Proxy. serviceproxy (). objshellsort ();
}

// Quick sorting
Public static void quicksort (){
Proxy. serviceproxy (). basequicksort ();
Proxy. serviceproxy (). objquicksort ();
}
Public static void main (string [] ARGs ){
Init ();
System. Out. println (system. currenttimemillis ());
Bubblesort ();
System. Out. println (system. currenttimemillis ());
System. out. println ("bubble sort .............................. ");
Init ();
System. Out. println (system. currenttimemillis ());
Selectsort ();
System. Out. println (system. currenttimemillis ());
System. out. println ("select sort .............................. ");
System. Out. println (system. currenttimemillis ());
Init ();
Insertsort ();
System. Out. println (system. currenttimemillis ());
System. out. println ("insert sort .............................. ");
Init ();
System. Out. println (system. currenttimemillis ());
Mergesort ();
System. Out. println (system. currenttimemillis ());
System. out. println ("merge sort .............................. ");
Init ();
System. Out. println (system. currenttimemillis ());
Shellsort ();
System. Out. println (system. currenttimemillis ());
System. out. println ("shell sort .............................. ");
Init ();
System. Out. println (system. currenttimemillis ());
Quicksort ();
System. Out. println (system. currenttimemillis ());
System. out. println ("quick sort .............................. ");
}

}
// Sort proxy
Class proxy {
@ Suppresswarnings ("rawtypes ")
Private Static sortservice service = new sortservice ();
@ Suppresswarnings ("rawtypes ")
Public static sortservice serviceproxy (){
Return service;
}
}

// Sorting implementation class
Class sortservice <t extends virtualobj> {
Private long [] longarr; // simulate an integer Array
Private T [] objarr; // simulate an array of Objects
Private int size;
Public void initlongarr (long [] ARR ){
Longarr = arr;
Size = arr. length;
}
Public void initobjarr (T [] ARR ){
Objarr = arr;
Size = arr. length;
}

Public void basequicksort (){
Baserecquicsort (0, size-1 );
Displaylongarr ();
}
// Quick sorting of Basic Types
Private void baserecquicsort (INT left, int right ){
If (right-left + 1 <= 10 ){
Baseinsertsort (left, right );
} Else {
Long povit = basegetpivot (left, right );
// Long povit = longarr [right];
Int compareindex = basepartition (left, right, povit );
Baserecquicsort (left, compareindex );
Baserecquicsort (compareindex + 1, right );
}
}
// Insert sorting during quick sorting
Private void baseinsertsort (INT left, int right ){
Int J = 0;
For (INT I = left + 1; I <= right; I ++ ){
Long temp = longarr [I];
For (j = I; j> left & longarr [J-1]> temp; j --){
Longarr [J] = longarr [J-1];
}
Longarr [J] = temp;
}

}
// Three items are being retrieved to prevent random data
Private long basegetpivot (INT left, int right ){
Int center = (left + right)/2;
If (longarr [left] <= longarr [center]) {
Swap (left, center );
}
If (longarr [left] <= longarr [right]) {
Swap (left, right );
}
If (longarr [center]> longarr [right]) {
Swap (center, right );
}

Return longarr [right];
}
// Quick sorting
Private int basepartition (INT left, int right, long povit ){
Int leftindex = left;
Int rightindex = right-1;
While (true ){
While (longarr [++ leftindex] <povit );
While (-- rightindex)> 0 & longarr [rightindex]> povit );
If (leftindex> = rightindex) break;
Else {
Swap (leftindex, rightindex );
}
} Swap (leftindex, right-1 );
Return leftindex;
}
// Exchange
Private void swap (INT indexfrom, int indexto ){
Long temp = longarr [indexfrom];
Longarr [indexfrom] = longarr [indexto];
Longarr [indexto] = temp;
}
// Fast sorting of Object Types
Public void objquicksort (){
Objrecquicksort (0, size-1 );
Displayobjarr ();
}
// Fast sorting of Object Types
Private void objrecquicksort (INT left, int right ){
If (right-left <10 ){
Objinsertsort (left, right );
}
Else {
T compareobj = objgetpovit (left, right );
// T compareobj = objarr [right];
Int compareindex = objpartion (left, right, compareobj );
Objrecquicksort (left, compareindex );
Objrecquicksort (compareindex + 1, right );
}
}
// Fast sorting of Objects
Private int objpartion (INT left, int right, T povit ){
Int leftindex = left;
Int rightindex = right-1;
While (true ){
While (objarr [++ leftindex]. getstr (). compareto (povit. getstr () <0 );
While (objarr [-- rightindex]. getstr (). compareto (povit. getstr ()> 0 );
If (leftindex> = rightindex) break;
Else {
Swapobj (leftindex, rightindex );
}
} Swapobj (leftindex, right-1 );
Return leftindex;
}
// Sort object insertion
Private void objinsertsort (INT left, int right ){
Int I = 0;
For (Int J = left + 1; j <size; j ++ ){
T temp = objarr [J];
For (I = J; I> left & objarr [I-1]. getstr (). compareto (temp. getstr ()> 0; I --){
Objarr [I] = objarr [I-1];
}
Objarr [I] = temp;
}
}
// The values of the three parameters are not random for the array size.
Private t objgetpovit (INT left, int right ){
Int center = (right + Left)/2;
T t_left = objarr [left];
T t_center = objarr [center];
T t_right = objarr [right];
If (t_left.getstr (). compareto (t_center.getstr ()> 0 ){
Swapobj (left, center );
}
If (t_center.getstr (). compareto (t_right.getstr () <0 ){
Swapobj (center, right );
}
If (t_left.getstr (). compareto (t_right.getstr ()> 0 ){
Swapobj (left, right );
}
Return objarr [right];
}
// Object Exchange
Private void swapobj (int from, int ){
T temp = objarr [from];
Objarr [from] = objarr [to];
Objarr [to] = temp;
}

// Basic type-hill sort increment-3
Public void baseshellsort (){
Int H = 1;
Int In, out;
While (H <size/3 ){
H = H * 3 + 1;
}

While (h> 0 ){
For (out = H; out <size; out ++ ){
Long temp = longarr [out];
For (in = out; in> h-1 & longarr [in-H]> = temp; In-= h ){
Longarr [in] = longarr [in-H];
}
Longarr [in] = temp;
}
H = (h-1)/3;
}
Displaylongarr ();
}
// Incremental sorting of Xiamen and type Hill-3
Public void objshellsort (){
Int H = 1;
Int In, out;
While (H <size/3 ){
H = H * 3 + 1;
}

While (h> 0 ){
For (out = H; out <size; out ++ ){
T temp = objarr [out];
For (in = out; in> h-1 & objarr [in-H]. getstr (). compareto (temp. getstr ()> 0; In-= h ){
Objarr [in] = objarr [in-H];
}
Objarr [in] = temp;
}
H = (h-1)/3;
}
Displayobjarr ();
}

// Sort the Basic Types
Public void baseunionsort (){
Long [] workspace = new long [size];
Basemergesort (workspace, 0, workspace. Length-1 );
Displaylongarr ();
}

// Merge algorithm
Private void basemergesort (long [] arr, int lower, int upper ){
If (lower = upper) return;
Int mid = (upper + lower)/2;
Basemergesort (ARR, lower, mid );
Basemergesort (ARR, Mid + 1, upper );
Basemerge (ARR, lower, Mid + 1, upper );
}
// Merge Sorting Algorithm
Private void basemerge (long [] arr, int lower, int mid, int upper ){
Int I = 0, middle = mid-1;
Int low = lower;
Int size = (upper-lower) + 1;
While (lower <= Middle & Mid <= upper ){
If (longarr [lower] <longarr [Mid]) {
Arr [I ++] = longarr [lower ++];
}
Else {
Arr [I ++] = longarr [Mid ++];
}
}
While (lower <= middle ){
Arr [I ++] = longarr [lower ++];
}
While (mid <= upper ){
Arr [I ++] = longarr [Mid ++];
}
For (Int J = 0; j <size; j ++ ){
Longarr [Low + J] = arr [J];
}

}

// Object type merging and sorting: when there are a large number of duplicates, the results are inaccurate.
Public void objunionsort (){
Virtualobj [] T = new virtualobj [size];
Objmergesort (T []) T, 0, size-1 );
Displayobjarr ();
}
// Merge algorithm
Private void objmergesort (T [] arr, int lower, int upper ){
If (lower = upper) return;
Int mid = (upper + lower)/2;
Objmergesort (ARR, lower, mid );
Objmergesort (ARR, Mid + 1, upper );
Objmerge (ARR, lower, Mid + 1, upper );
}
// Merge Sorting Algorithm
Public void objmerge (T [] arr, int lower, int mid, int upper ){
Int low = lower;
Int I = 0;
Int size = (upper-lower)/2;
Int middle = mid-1;
While (lower <= Middle & Mid <= upper ){
If (objarr [lower]. getstr (). compareto (objarr [Mid]. getstr () <0 ){
Arr [I ++] = objarr [lower ++];
} Else {
Arr [I ++] = objarr [Mid ++];
}
}
While (lower <= middle ){
Arr [I ++] = objarr [lower ++];
}
While (mid <= upper ){
Arr [I ++] = objarr [Mid ++];
}
For (Int J = 0; j <size; j ++ ){
Objarr [Low + J] = arr [J];
}
}

// Basic type Bubble Sorting
Public void basebubblesort (){
For (INT I = size-1; I> 1; I --){
For (Int J = 0; j <I; j ++ ){
If (longarr [J]> longarr [J + 1]) {
Long temp;
Temp = longarr [J];
Longarr [J] = longarr [J + 1];
Longarr [J + 1] = temp;
}
}
}
Displaylongarr ();
}
// Bubble Sorting of Object Types
Public void objbubblesort (){
T temp;
For (INT I = size-1; I> 1; I --){
For (Int J = 0; j <I; j ++ ){
If (objarr [J]. getstr (). compareto (objarr [J + 1]. getstr ()> 0 ){
Temp = objarr [J];
Objarr [J] = objarr [J + 1];
Objarr [J + 1] = temp;
}
}
}
Displayobjarr ();
}

// Sort inserts of the basic type
Public void baseinsertsort (){
Int J;
For (INT I = 1; I <size; I ++ ){
Long temp = longarr [I];
For (j = I; j> 0 & longarr [J-1]> temp; j --){
Longarr [J] = longarr [J-1];
}
Longarr [J] = temp;
}
Displaylongarr ();
}
// Sort object types by insertion
Public void objinsertsort (){
Int J;
For (INT I = 1; I <size; I ++ ){
T temp = objarr [I];
For (j = I; j> 0 & objarr [J-1]. getstr (). compareto (temp. getstr ()> 0; j --){
Objarr [J] = objarr [J-1];
}
Objarr [J] = temp;
}
Displayobjarr ();
}
// Sort the basic type
Public void baseselectsort (){
For (INT I = 0; I <size; I ++ ){
For (Int J = I; j <size; j ++ ){
If (longarr [I]> longarr [J]) {
Long temp;
Temp = longarr [I];
Longarr [I] = longarr [J];
Longarr [J] = temp;
}
}
}
Displaylongarr ();
}
// Sort Object Types
Public void objselectsort (){
T temp;
For (INT I = 0; I <size; I ++ ){
For (Int J = I; j <size; j ++ ){
If (objarr [I]. getstr (). compareto (objarr [J]. getstr ()> 0 ){
Temp = objarr [I];
Objarr [I] = objarr [J];
Objarr [J] = temp;
}
}
}
Displayobjarr ();
}
Public void displaylongarr (){

For (INT I = 0; I <size; I ++ ){
System. Out. Print (longarr [I] + "");
} System. Out. println ();
}
Public void displayobjarr (){
For (INT I = 0; I <size; I ++ ){
System. Out. Print (objarr [I]. getstr () + "");
} System. Out. println ();
}

}
// Virtual objects can be used as sorting conversion classes
Class virtualobj {
Public virtualobj (){
Super ();
}
Public virtualobj (string STR, int age, float idcard ){
Super ();
This. Str = STR;
This. Age = age;
This. idcard = idcard;
}
Public String getstr (){
Return STR;
}
Public void setname (string Str ){
This. Str = STR;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Public float getidcard (){
Return idcard;
}
Public void setidcard (float idcard ){
This. idcard = idcard;
}

Private string STR;
Private int age;
Private float idcard;

}

 

Conclusion: After testing the array in the 1000 and 10000 cases, we can see that the bubble is bubble. If we choose to insert these three 0 (N square) algorithms, the bubble is the slowest, And the insert is slightly because of the selection.

Merge and Hill did a good job, but the quick sorting thought it was not as good as the first two, but the simple algorithm before the order. Of course, this data has a lot of duplicate data, but the efficiency in this environment is roughly the same. We recommend that you prioritize Hill and Merge Sorting when there are many duplicates of data.

 

No repeated array elements

If you replace the init method with the following code:

Static synchronized void Init (){
Persion = new virtualobj [size];
Arr = new long [size];
Int [] ran_arr = randomint ();
For (INT I = 0; I <size; I ++ ){
Persion [I] = new virtualobj ("Tom" + ran_arr [I], ran_arr [I], ran_arr [I]);
}
For (INT I = 0; I <size; I ++ ){
Arr [I] = ran_arr [I];
}
Proxy. serviceproxy (). initlongarr (ARR );
Proxy. serviceproxy (). initobjarr (persion );
}
Private Static int [] randomint (){
Random ran = new random ();
Boolean [] bool = new Boolean [size];
Int Rs [] = new int [size];
Int temp, j = 0;
For (INT I = 0; I <size; I ++ ){
Do {
Temp = ran. nextint (size );
} While (bool [temp]);
Bool [temp] = true;
RS [J ++] = temp;
}
Return Rs;
}

 

However, the data is completely unique ..

The conclusion is the same as above. The efficiency of hill and merge increases by 10 times as data increases, and the fast sorting is still slightly different. It should be two to three times that of simple sorting. [The execution environment is the same as above]

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.