Three typical classic algorithms bubble sort, insert sort, select sort

Source: Internet
Author: User

A little easier, a thorough understanding of the three algorithms, of course, is only part of the improved version, the specific classification and so on, but next week something, first of these several typical figure to understand, the usual, see the code said the problem

/** * Created by Fanyafeng on 2015/5/8/0008.        */public class Algtest {public static void main (string[] args) {System.out.println ("sorting algorithm");        System.out.println ("-------------------------------------------------------------------");    Domain ();        } private static void domain () {bubble ();        Insert ();    Select (); } private static void Insert () {int[] Unbu = {5, 3, 9, 8, 7, 4, 9, 5, 5, 2, 84, 51, 8, 4, 18, 515, 8, 54, 8, 15 , 84, 65, 6, 98, 5, 6, 8, 2, 6, 8, 2, 69, 8, 4, 26, 85, 21, 6, 82, 69, 85, 2, 6, 8, 1, 6, 8, 74515, 6, 85, 21, 6, 84, 2, 7        85, 542, 85, 6, 85, 12};        Long bubble_sort_starttime = System.currenttimemillis ();        Long insertionsort_starttime = System.nanotime ();        Insertionsort (Unbu);//Long Bubble_sort_endtime = System.currenttimemillis ();        Long insertionsort_endtime = System.nanotime ();        for (int Item:unbu) {System.out.print (item + ","); } SYSTEM.OUt.println ("\ n" + "program Run Time:" + (Insertionsort_endtime-insertionsort_starttime) + "nm or MS"); } private static void Select () {int[] unbu1 = {5, 3, 9, 8, 7, 4, 9, 5, 5, 2, 84, 51, 8, 4, 18, 515, 8, 54, 8, 1 5, 84, 65, 6, 98, 5, 6, 8, 2, 6, 8, 2, 69, 8, 4, 26, 85, 21, 6, 82, 69, 85, 2, 6, 8, 1, 6, 8, 74515, 6, 85, 21, 6, 84, 2,        785, 542, 6, 12};//long startTime = System.currenttimemillis ();        Long startTime = System.nanotime ();        Selection_sort (UNBU1);//Long EndTime = System.currenttimemillis ();        Long endTime = System.nanotime ();        for (int item:unbu1) {System.out.print (item + ",");    } System.out.println ("\ n" + "program Run Time:" + (Endtime-starttime) + "nm or MS"); } private static void Bubble () {int[] Unbu3 = {5, 3, 9, 8, 7, 4, 9, 5, 5, 2, 84, 51, 8, 4, 18, 515, 8, 54, 8, 1 5, 84, 65, 6, 98, 5, 6, 8, 2, 6, 8, 2, 69, 8, 4, 26, 85, 21, 6, 82, 69, 85, 2, 6, 8, 1, 6, 8, 74515, 6, 85, 21, 6, 84, 2, 785,542, 6, 12};//long bubble_sort_starttime = System.currenttimemillis ();        Long bubble_sort_starttime = System.nanotime ();        Bubble_sort (UNBU3);//Long Bubble_sort_endtime = System.currenttimemillis ();        Long bubble_sort_endtime = System.nanotime ();        for (int item:unbu3) {System.out.print (item + ",");    } System.out.println ("\ n" + "program Run Time:" + (Bubble_sort_endtime-bubble_sort_starttime) + "nm or MS"); } public static void Insertionsort (int[] unsort) {/** * insert sort, this outer loop starts directly from 1, not minus 1 * This is slightly more complicated, but         The efficiency is the highest, I did not use the statistical method, I was the test has so a dozens of times, everybody oneself also may test under * see I say is not right * outside the circulation or the old appearance, takes out the value, actually is not difficult to understand, the sort is compares the size the process, if does not have one as being compared * That this algorithm is also not set up, so that everyone write words in fact the outermost layer want to not think, inertia should be written out * each time will be the largest in the last face * can be understood to be inserted from the last surface of the array */INT        I, J, K; for (i = 1; i < unsort.length; i++) {k = unsort[i];//Remove/** * Find location * First time Time * here to sayUNSORT[J]=UNSORT[I-1],K=UNSORT[I]&LT;UNSORT[J] * so that means unsort[i]<unsort[i-1] * that is unsort[j+1] <unsort[j]=k */for (j = i-1; J >= 0 && K < unsort[j]; j--) {uns Ort[j + 1] = unsort[j];//move element backwards} unsort[j + 1] = k;//Insert}} private static void Select Ion_sort (int[] unsort) {/** * Select sort * Outside the loop is to first take out a value, that is, Unsort[i] * inside the loop is out of the outside to get that value, in fact, is the order of each The first value, and then traverse it * and compare it to the first one if the outer loop gets a large number then the order is reversed, and the opposite is the same. I believe that we can understand this very clearly, this is to make it from small to large arrangement * if from the big to the small words only use if the inside of the character Just change it. * Yes, say a little bit, is the number of arrays outside the loop traversal, is to subtract 1, or there is an invalid traversal * will affect the efficiency of the * below is not reduced by one, because he is starting from the i+1, the outer layer is from 0 onwards, Lengt  H need to subtract one * below minus one is sure to miss something */for (int i = 0; i < unsort.length-1; i++) {for (int j = i + 1; J < Unsort.length;                    J + +) {if (Unsort[i] > Unsort[j]) {int temp = Unsort[i]; UnSort[i] = Unsort[j];                UNSORT[J] = temp; }}}} private static void Bubble_sort (int[] unsort) {/** * bubble sort, basic bubble sort, looks like an improved version,         I'll talk about it first. * First of all, let's just say, "bubble," that is, each time the two-digit comparison of the big one on the top is the big head * So the understanding below should be simple it is the same outer loop traversal all Unsort * Then the inner loop starts to compare the size of the value * It will compare the number of adjacent two numbers, each time put the big in front * so that the last traversal will be all the arrangement of all the good */for (int i = 0 ; i < unsort.length-1;                    i++) {for (int j = 0; J < unsort.length-i-1; j + +) {if (Unsort[j] > unsort[j + 1]) {                    int temp = Unsort[j];                    UNSORT[J] = unsort[j + 1];                Unsort[j + 1] = temp; }            }        }    }}
Interview asked the most seems to be bubbling algorithm, where bloggers put the explanation into the code, the specific can see, Bo Master not much said, estimated to hide a week
Forget it, up the output


Three typical classic algorithms bubble sort, insert sort, select sort

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.