JAVA Parallel sorting, bubbling, selection, insertion of sorting code and comments

Source: Internet
Author: User

Package com. stig_dim.hello;

02

03 import java. util. Random;

04

05 public class TestSorts {

06

07 /**

08 * @ param null;

09 * @ author Administrator

10 * @ category efficiency of mainstream sorting algorithms

11 * @ see <Chapter 1-3 of Introduction to algorithms>

12 */

13 @ SuppressWarnings ("deprecation ")

14 public static void main (String [] args ){

15 int [] testarr = new int [4086];

16 double t;

17 InsertData2Array (testarr );

18

19 t = System. currentTimeMillis ();

20 SortAlgorithem. BubbleSort (testarr );

21 t = System. currentTimeMillis ()-t;

22 for (int num: testarr)

23 System. out. print (num + "");

24 System. out. println ("Bubble Sorting takes time:" + t + "ms ");

25

26 t = System. currentTimeMillis ();

27 SortAlgorithem. InsertSort (testarr );

28 t = System. currentTimeMillis ()-t;

29 for (int num: testarr)

30 System. out. print (num + "");

31 System. out. println ("insertion sorting takes" + t + "ms ");

32

33 t = System. currentTimeMillis ();

34 SortAlgorithem. MergeSort (testarr );

35 t = System. currentTimeMillis ()-t;

36 for (int num: testarr)

37 System. out. print (num + "");

38 System. out. println ("federated (non-partitioned) sorting takes the following time:" + t + "ms ");

39}

40

41 private static void InsertData2Array (int [] ){

42

43 Random rd = new Random (System. currentTimeMillis ());

44 for (int I = 0; ++ I <a. length ;){

45 a [I] = rd. nextInt (30 );

46 System. out. print (a [I] + "");

47}

48 System. out. println ("Raw data ");

49}

50}


View sourceprint? 01 package com. stig_dim.hello;

02

03 /**

04 * @ author Administrator

05 * @ category all input N is 4086 simulated average

06 */

07 public class SortAlgorithem {

08 /**

09 * @ deprecated brainless bubble growth rate = Σ (O (n ^ 2 ))

10 **/

11 public static void BubbleSort (int [] ){

12 for (int I = a. length; -- I> 0 ;){

13 for (int j = 0; j <I; j ++)

14 {

15 if (a [j]> a [j + 1])

16 {

17 int temp = a [j];

18 a [j] = a [j + 1];

19 a [j + 1] = temp;

20}

21}

22}

23}

24

25 /**

26 * @ category see <Introduction to algorithms> Chapter 1

27 * @ deprecated

28 * @ deprecated insert sort growth rate = O (n ^ 2)

29 * although it is the essence of insertion sorting or bubble, the size of each bubble is not the same

30 */

31 public static void InsertSort (int [] ){

32 for (int I = 1; I <a. length; I ++ ){

33 int N = a [I]; // The right hand draws the card N

34 int j;

35 for (j = I-1; j> = 0 & a [j]> = N; j --) // locate

36 a [j + 1] = a [j]; // left-hand shuffling

37 a [j + 1] = N; // Insert the card N to the allocated card on the left.

38}

39}

40

41

42 /**

43 * @ category = parallel sorting (non-recursive non-sharding) growth rate = O (n)

44 **/

45 public static void MergeSort (int [] ){

46 // divided? Do it ~ [A []-> [A [1. p] + A [r-p + 1]

47 int N = a. length;

48 int [] L = new int [N/2]; // p // pretty tricks use RAM for time ..

49 int [] R = new int [(N-N/2)]; // q // This is the lowest average worst time compared to other series such as heap sorting

50 for (int I = 0; I <L. length; I ++) // If the input is large enough, the more complicated and fast the sorting is, the more obvious the linear performance is. Generally, the average performance is good.

51 L [I] = a [I];

52 for (int I = 0; I <R. length; I ++)

53 R [I] = a [I];

54

55 for (int I = 0, j = 0; I <L. length & j <R. length; I ++ ){

56 if (L [I] <R [j]) {

57 a [I] = L [I];

58 ++ I;

59} else

60 {

61 a [I] = R [j];

62 + + j;

63}

64}

65}

66}

Author: "beauty, your brick-and-mortar blog"
 

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.