Java Data Structure-classic sorting highlights

Source: Internet
Author: User
Tags comparable

Comparison of several sorting types !!

1. Bubble Sorting

Package COM. jzm. kindsofsort; public class bubblesort {/*** @ Param ARGs * Bubble Sorting */public static void bubblesort (comparable A []) {for (INT I = 1; I <. length; I ++) {for (Int J = 0; j <. length-1; j ++) {if (a [J]. compareto (A [J + 1])> 0) {comparable T = A [J]; A [J] = A [J + 1]; A [J + 1] = T ;}}} public static void main (string [] ARGs) {INTEGER [] pdata = {, 10, 6, 11,165}; bubblesort (pdata); system. out. println ("Final Result:"); For (INT I = 0; I <pdata. length; I ++) {system. out. print (pdata [I] + "");}}}

2. insertsort

Package COM. jzm. kindsofsort; public class insertionsort {// simple insertion sorting public static void insertsort (comparable [] data) {for (INT I = 1; I <data. length; I ++) {comparable TMP = data [I]; If (TMP. compareto (data [I-1]) <0) {Int J; For (j = I-1; j> = 0 & TMP. compareto (data [J]) <0; j --) {data [J + 1] = data [J];} data [J + 1] = TMP ;}}} public void display (comparable A []) {for (INT I = 0; I <. length; I ++) {system. out. print (A [I] + "") ;}} public static void main (string [] ARGs) {comparable A [] = {7,100, 1}; insertionsort = new insertionsort (); insertionsort. insertsort (a); insertionsort. display ();}}

3. quicksort

Package COM. jzm. kindsofsort; public class quicksort {// a public static void quicksort (integer [] A, int left, int right) {int I = left, j = right; int middle, strtemp; middle = A [(left + right)/2]; do {While (A [I] <middle) & (I <right) I ++; while (A [J]> middle) & (j> left) j --; if (I <= J) {strtemp = A [I]; A [I] = A [J]; A [J] = strtemp; I ++; j -- ;}} while (I <= J);/* system. out. println ("Left:" + Left + "Right:" + right); For (int t = 0; t <. length; t ++) system. out. print (A [T] + ""); system. out. println (""); */If (left <j) {quicksort (A, left, J);} If (Right> I) quicksort (A, I, right);} public static void main (string [] argv) {INTEGER [] pdata = {165,}; quicksort (pdata, 0, pdata. length-1); system. out. println ("Final Result:"); For (INT I = 0; I <pdata. length; I ++) {system. out. print (pdata [I] + "");}}}

4. quicksort2

Package COM. jzm. kindsofsort; public class quicksort2 {// General fast sorting, slow Private Static void swap (comparable A [], int I, Int J) {comparable T = A [I]; A [I] = A [J]; A [J] = T;} Private Static int partion (comparable [] A, int left, int right) {int I = left; int J = right + 1; Comparable x = A [left]; while (true) {While (A [++ I]. compareto (x) <0 & I <right); While (A [-- J]. compareto (x)> 0); if (I> = J) break; swap (A, I, j);} A [left] = A [J]; A [J] = x; return J;} public static comparable [] qsort (comparable [] A, int left, int right) {If (left <right) {int q = partion (A, left, right); // segment/* system. out. println ("Left:" + Left + "Right:" + right); For (int t = 0; t <. length; t ++) system. out. print (A [T] + ""); system. out. println (""); */qsort (A, left, q-1); qsort (A, q + 1, right);} return ;} public static void main (string [] ARGs) {comparable [] A = {11,165, 20,}; comparable [] T = qsort (A, 0, a. length-1); system. out. println ("Final Result:"); For (INT I = 0; I <t. length; I ++) {system. out. print (A [I] + "");}}}

5. shellsort

Package COM. jzm. kindsofsort; public class shellsort {// Hill sort public static void sort (INT [] A, int DK) {int I, j, temp; for (I = dk; I <. length; I ++) {if (a [I] <A [I-dk]) {temp = A [I]; A [I] = A [I-dk]; for (j = I; j> 0 & temp <A [J-1]; j = J-DK) {A [J] = A [J-1];} A [J] = temp ;}} public static void main (string ARGs []) {int [] A = {12,100, 12, 1,132}; int W = 1; while (W <=. length/5) {If (w % 2 = 0) {// improve efficiency O (N ^ 1.5) W = W + 1; system. out. println ("W");} Sort (A, W); W = W * 5 + 1;} For (INT I = 0; I <. length; I ++) system. out. print (A [I] + "");}}

6. simpleselectsort

Package COM. jzm. kindsofsort; public class simpleselectsort {// simple sort public static void sort (comparable [] data) {for (INT I = 0; I <data. length; I ++) {// record the current position int position = I; // locate the smallest number and use position to point to the smallest position for (Int J = I + 1; j <data. length; j ++) {If (data [position]. compareto (data [J])> 0) {position = J ;} // end if} // the minimum number of exchanges data [position] and the position comparable temp = data [I]; data [I] = data [position]; data [position] = temp;} // end for} // end sort public static void main (string [] ARGs) {// later than jdk1.5, the basic data types can be automatically packed. The comparable interface comparable [] C = {23,145,} has been implemented }; sort (c); For (comparable data: C) system. out. print (Data + "");}}

7. Sorting speed test

 

Package COM. jzm. kindsofsort; import Java. util. random; public class testspeed {/*** test the sorting speed. For large numbers, fast sorting speed is the fastest */Private Static final int maxnum = 50000; private Static Boolean issuccess (comparable A []) {for (INT I = 0; I <. length-2; I ++) {if (a [I]. compareto (A [I + 1])> 0) {system. out. println ("array A is not sorted in descending order"); Return false ;}} system. out. println ("detected"); Return true;} public static void main (string [] ARGs) {systemdate sysdate = new systemdate (); string strstarttime = sysdate. getthisdate (); debugruntime = new debugruntime (); random = new random (); integer a [] = new integer [maxnum]; for (INT I = 0; I <maxnum; I ++) {A [I] = random. nextint (maxnum);} // assign the initial value to the [] array // insertionsort. insertsort (a); // 24275 ms for simple insertion sorting. // simpleselectsort. sort (a); // simple sorting 34945 Ms. quicksort2.qsort (A, 0,. length-1); // 40 -- 52 Ms // quicksort. quicksort (A, 0,. length-1); // 30-50 ms // bubblesort. bubblesort (a); // 68079 -- 70356 Ms issuccess (a); string strstoptime = sysdate. getthisdate (); system. out. println ("program started at:" + strstarttime); system. out. println ("program ended at:" + strstoptime); debugruntime. executiontime (); // the running time in the background }}

8. Added test class

Package COM. jzm. kindsofsort; import Java. util. date; import Java. text. simpledateformat; import Java. text. parseexception; public class runtimec1c {public static void main (string [] Str) {systemdate sysdate = new systemdate (); string strstarttime = sysdate. getthisdate (); debugruntime = new debugruntime (); For (INT I = 0; I <5000; I ++) {for (Int J = 0; j <1000; j ++) {system. out. println ("I =" + I + ", j =" + J); // time-consuming I/O operations} string strstoptime = sysdate. getthisdate (); system. out. println ("program started at:" + strstarttime); system. out. println ("program ended at:" + strstoptime); debugruntime. executiontime (); // run time in the background} class debugruntime {private long starttime = 0; private long endtime = 0; Public debugruntime () {system. out. println ("begin .............................. "); this. setstarttime (system. currenttimemillis ();} public void executiontime () {This. setendtime (system. currenttimemillis (); system. out. println ("execution time:" + (this. getendtime ()-This. getstarttime () + "ms. ");} public void executiontime (string message) {This. setendtime (system. currenttimemillis (); system. out. println (Message + "execution time:" + (this. getendtime ()-This. getstarttime () + "ms. ");} private long getendtime () {return endtime;} private long getstarttime () {return starttime;} private void setendtime (long endtime) {This. endtime = endtime;} private void setstarttime (long starttime) {This. starttime = starttime ;}// a date operation class systemdate {public static systemdate = NULL; public date; Public String formattype = "yyyy/mm/DD-hh: mm: SS "; // Default Time Format: Public simpledateformat fmat; Public systemdate () {} static systemdate getinstance () {If (systemdate = NULL) systemdate = new systemdate (); return systemdate;} // returns the string of the current time. The default time format is formattype Public String getthisdate () {return getthisdate (formattype);} // returns the string of the input time, use the default time format formattype Public String getthisdate (date) {return getthisdate (formattype, date);} // return the string of the current time. The time format is the input parameter (string formattype) public String getthisdate (string formattype) {date = new date (); Return getthisdate (formattype, date) ;}// returns the string of the input time, the time format is input parameter (string formattype) Public String getthisdate (string formattype, date) {fmat = new simpledateformat (formattype); Return fmat. format (date) ;}// return time date, which is converted using the input time string. The input time format is the default time format formattype date getasstringdate (string strdate) throws parseexception {return getasstringdate (strdate, formattype);} // return time date, which is converted using the input time string. The input time format is string formattype date getasstringdate (string strdate, string formattype) throws parseexception {fmat = new simpledateformat (formattype); Return fmat. parse (strdate );}}

 

9. Conclusion: The sorting speed is about 5 million, which takes about 6 s.

If something is wrong, please make an axe.

 

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.