In Java, how fast is the fast sorting of array. sort and (self-written?

Source: Internet
Author: User

I accidentally used quick sorting when I was doing Java exercises recently. I forgot to search online ...... I think about a searchable search: in Java, array. what is the speed for fast sorting of sort and (self-written?

Many bloggers have discussed this issue on the Internet. They come to the conclusion that system. array. sort is much faster than the sort algorithm they write, for example, sort! I did something in Java with a curious mind. The result is ......

Description of system. array. Sort on Microsoft msdn:

"Each element in array must implement the icomparable interface to be compared with all other elements in array. If the sorting cannot be completed successfully, the result is not defined. This method uses the quicksort algorithm. This achieves unstable sorting; that is, if the two elements are equal, their order may not be retained. On the contrary, stable sorting retains the order of equal elements. In general, the computational complexity of this method is O (n log n), where N is the length of array; in the worst case, the computational complexity is O (n ^ 2 )."

 

Java program comparison:

1 import Java. util. arrays; 2 Import Java. util. random; 3 Import Java. util. values; 4 5 public class quicksort {6 public static void quicksort (INT [] A, int left, int right) {7 int left, right, temp, middle; 8 middle = A [(left + right)/2]; 9 left = left; 10 right = right; 11 do {12 // search for the subscript 13 while (left <Right & A [left] <middle) {14 left ++ from the left; 15} 16 // search for the subscript 17 while (Right> left & A [right]> middle) {18 right --; 19} 20 from the right to find a pair, change them 21 // Note: If the equal sign is missing, the life cycle will be 22 if (left <= right) {23 temp = A [left]; 24 A [left] = A [right]; 25 A [right] = temp; 26 left ++; 27 right --; 28} 29 30} while (Right> = left); // until the left and right sides are switched to 31 32 If (left <right) {33 quicksort (A, left, right ); 34} 35 36 IF (Right> left) {37 quicksort (A, left, right); 38} 39} 40 41 public static void main (string [] ARGs) {42 // todo auto-generated method stub43 export myscnner = new partition (system. in); 44 system. out. println ("Enter the size of the sorting array:"); 45 46 int n = myscnner. nextint (); 47 int [] A = new int [N]; 48 49 random = new random (); 50 for (INT I = 0; I <. length; I ++) {51 A [I] = math. ABS (random. nextint () % 10000000; 52} 53 54 // system. array. sort execution time and execution result 55 system. out. println ("system. array. sort execution time and Result: "); 56 long T = system. nanotime (); 57 arrays. sort (A, 0,. length); 58 for (INT I = 0; I <. length; I ++) {59 system. out. print (A [I] + ""); 60} 61 system. out. println (); 62 system. out. println (system. nanotime ()-T); 63 64 // my quicksort execution time and execution result 65 system. out. println ("My quicksort execution time and execution result:"); 66 long T2 = system. nanotime (); 67 quicksort (A, 0, n-1); 68 for (INT I = 0; I <. length; I ++) {69 system. out. print (A [I] + ""); 70} 71 system. out. println (); 72 system. out. println (system. nanotime ()-t2); 73} 74 75}

 

Running result:

When the array is very small:

Increase gradually:

 

I was surprised. Isn't the answer to this question in Java: array. sort is less than (written by myself) in fast sorting.

I don't know if my conclusions are correct. I have tried multiple times and the results are the same!

Do you have any questions if you want to see this article ??? Email Contact (18700943025@163.com)

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.