Java data structures and algorithms-reader 01 (simple sorting: Bubble, selection, and insertion)

Source: Internet
Author: User

Chapter 3: Comparison andCode

There are four types of simple sorting:

1. Bubble Sorting (this must be very familiar to you and usually involves a lot)

2. Select sort

3. Insert sorting

 

1. Bubble Sorting

In general, if there are n data items in the array, the first sort has a N-1 comparison, the second has a N-2, and so on, the summation formula of the sequence is:

(N-1) + (N-2) + (N-3) +... + 1 = N * (N-1)/2

In this case,AlgorithmAfter about n²/2 comparisons (ignore minus 1), the number of exchange and comparison operations is proportional to n², so the speed of this sort algorithm is very slow.

2. Select sort

The Select sort and bubble sort performed the same number of comparisons: N * (N-1)/2. However, there are only N interactions.

3. Insert sorting

In most cases, the insertion sorting algorithm is the best in a simple algorithm. Although insertion sorting still requires O (n²) time, in general, it is twice faster than the Bubble sorting, and a little faster than the selected sorting.

 

 

The Code is as follows:

 1   Public   Class Bubblesorttest {  2   3       /**  4   *  @ Param  ARGs  5        */  6       Public   Static   Void  Main (string [] ARGs ){  7           // Todo auto-generated method stub  8           Int Max = 10 ;  9 Bubblearray barray = New  Bubblearray (max );  10 Barray. insert (12 );  11 Barray. insert (23 );  12 Barray. insert (232 );  13 Barray. insert (1 );  14 Barray. insert (5 );  15 Barray. insert (33 );  16 Barray. insert (51 );  17 Barray. insert (41 );  18 Barray. insert (22 );  19 Barray. insert (98 ); 20   Barray. Display ();  21   //  Barray. bubblesort ();  22   //  Barray. selectsort ();  23   Barray. insertsort ();  24   Barray. Display ();  25           26   }  27  28   }  29   30   Class  Bubblearray {  31       Private   Int  [] Array;  32       Private   Int  Nelems;  33       34       Public Bubblearray (Int  Max ){  35 Array = New   Int  [Max];  36 Nelems = 0 ;  37   }  38       39       Public   Void Insert ( Int  Value ){ 40 Array [nelems] = Value;  41 Nelems ++ ;  42   }  43       44       Public   Void  Display (){  45           For ( Int I = 0; I <array. length; I ++ ){ 46 System. Out. Print (array [I] + "" );  47   }  48 System. Out. println ("" );  49   }  50       51       Public   Void  Bubblesort (){  52           Int  First; 53           Int  Last;  54           For (Last = array. Length-1; last> 1; last -- ){  55               For (First = 0; first <last; first ++ ){  56                   If (Array [first]> array [first + 1 ]) {  57 Swap (first, first + 1 ); 58   }  59   }  60   }  61   }  62       63       Public   Void  Selectsort (){  64           Int  In, out, min;  65          For (Out = 0; out <array. Length-1; out ++ ){  66 Min = Out;  67   System. Out. println (min );  68               For (IN = out + 1; in <array. length; In ++ ){  69                   If (Array [in] < Array [Min]) {  70 Min =In;  71   }  72   }  73   Swap (Out, min );  74   }  75   }  76       77       Public   Void  Insertsort (){  78          Int  In, out;  79           For (Out = 1; out <array. length; out ++ ){  80               Int Temp = Array [out];  81 In = Out;  82               While (In> 0 & array [in-1]> = Temp ){  83 Array [in] = array [in-1];  84 -- In;  85   }  86 Array [in] = Temp;  87   }  88   }  89   90       Private   Void Swap ( Int One,Int  Two ){  91           //  Todo auto-generated method stub  92           Int Temp = Array [one];  93 Array [one] = Array [two];  94 Array [two] = Temp;  95   }  96 }

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.