Java sorting summary-Java sorting algorithm-Java Algorithm Summary

Source: Internet
Author: User
Tags rounds

Package COM. softeem. jbs. lesson4; </P> <p> Import Java. util. random; </P> <p>/** </P> <p> * Sorting Test </P> <p> * Sorting Algorithm Classification as follows: </P> <p> * 1. insert sorting (insert sorting, semi-insert sorting, and Hill sorting); </P> <p> * 2. exchange sorting (Bubble sorting and quick sorting); </P> <p> * 3. select sorting (directly select sorting and heap sorting). </P> <p> * 4. merge and sort; </P> <p> * 5. base sorting. </P> <p> * about sorting method selection: </P> <p> * (1) if n is small (for example, n ≤ 50), you can directly insert or directly select the sorting method. </P> <p> * When the record size is small, it is better to directly insert and sort the records. Otherwise, it is better to directly select and sort the records because the number of records to be moved is smaller than the number of directly inserted records. </P> <p> * (2) if the initial state of the file is basically ordered (in the forward direction), direct insertion, bubble, or random quick sorting should be selected; </P> <p> * (3) if n is large, the time complexity is O (nlgn. </P> <p> */</P> <p> public class sorttest {</P> <p>/** </ p> <p> * method of initializing the test array </P> <p> * @ return an initialized array </P> <p> */</P> <p> Public int [] createarray () {</P> <p> random = new random (); </P> <p> int [] array = new int [10]; </P> <p> for (INT I = 0; I <10; I ++) {</P> <p> array [I] = random. nextint (100)-random. nextint (100); // generate two random numbers to subtract from each other to ensure a negative number </P> <p >}</P> <p> system. out. println ("========= original sequence ==== ====== "); </P> <p> printarray (array); </P> <p> return array; </P> <p >}</P> <p>/** </P> <p> * print the elements in the array to the console </P> <p> * @ Param source </P> <p> */</P> <p> Public void printarray (INT [] data) {</P> <p> for (int I: Data) {</P> <p> system. out. print (I + ""); </P> <p >}</P> <p> system. out. println (); </P> <p >}</P> <p>/** </P> <p> * switch the location of the specified two elements in the array </P> <p> * @ Param data </P> <p> * @ Param x </P> <p> * @ Param Y </P> <p> */</P> <p> privat E void swap (INT [] data, int X, int y) {</P> <p> int temp = data [x]; </P> <p> data [x] = data [y]; </P> <p> data [y] = temp; </P> <p >}</P> <p>/** </P> <p> * Bubble Sorting-a type of exchange sorting </P> <p>> * method: the two adjacent elements are compared. If necessary, they are exchanged. The maximum elements are placed at the end of each loop (such as sorting from small to large ), the next cycle is to perform similar operations on other numbers. </P> <p> * performance: number of comparisons O (N ^ 2), N ^ 2/2, number of exchanges O (N ^ 2 ), N ^ 2/4 </P> <p> * @ Param data: array to be sorted </P> <p> * @ Param sorttype </P> <p> * @ return </P> <p> */</P> <p> Public void bubblesort (INT [] data, string sorttype) {</P> <p> If (sorttype. equals ("ASC") {// Positive Sorting, small to large </P> <p> // number of rounds for comparison </P> <p> for (INT I = 1; I <data. length; I ++) {</P> <p> // compare two adjacent numbers, bubble after a large number </P> <p> for (Int J = 0; j <data. length-I; j ++) {</P> <p> If (data [J]> data [J + 1]) {</P> <p> // exchange two adjacent numbers </P> <p> swap (data, j, J + 1 ); </P> <p >}</P> <p >}else if (sorttype. equals ("DESC") {// reverse sorting, number of rounds from large to small </P> <p> // comparison </P> <p> for (INT I = 1; I <data. length; I ++) {</P> <p> // compare two adjacent numbers, bubble after a large number </P> <p> for (Int J = 0; j <data. length-I; j ++) {</P> <p> If (data [J] <data [J + 1]) {</P> <p> // Number of adjacent switches </P> <p> swap (data, J, J + 1 ); </P> <p >}</P> <p>} </P> <p >}</P> <p >}else {</P> <p> system. Out. println ("the sorting type you entered is incorrect! "); </P> <p >}</P> <p> printarray (data ); // output the array value after the Bubble Sorting </P> <p >}</P> <p>/** </P> <p> * directly select the sort method ---- select </P> <p> * method of sorting: each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged. </P> <p> * performance: number of comparisons O (N ^ 2), N ^ 2/2 </P> <p> * Number of exchanges O (N ), n </P> <p> * The number of exchanges is much less than that of Bubble sorting. Because the CPU time required for switching is much longer than the Cup time required, the sort is faster than Bubble sorting. </P> <p> * However, when n is large, the CPU time required is dominant. Therefore, the performance and Bubble sorting are not much different, but it must be faster without a doubt. </P> <p> * @ Param data: array to be sorted </P> <p> * @ Param sorttype </P> <p> * @ return </P> <p> */</P> <p> Public void selectsort (INT [] data, string sorttype) {</P> <p> If (sorttype. equals ("ASC") {// Positive Sorting, from small to large </P> <p> int index; </P> <p> for (INT I = 1; I <data. length; I ++) {</P> <p> Index = 0; </P> <p> for (Int J = 1; j <= data. length-I; j ++) {</P> <p> If (data [J]> data [Index]) {</P> <p> Index = J; </P> <p>} </P> <p >}</P> <p> // exchange data at the location. length-I and index (maximum) </P> <p> swap (data, data. length-I, index); </P> <p >}</P> <p >}else if (sorttype. equals ("DESC") {// reverse sorting, from large to small </P> <p> int index; </P> <p> for (INT I = 1; I <data. length; I ++) {</P> <p> Index = 0; </P> <p> for (Int J = 1; j <= data. length-I; j ++) {</P> <p> If (data [J] <data [Index]) {</P> <p> Index = J; </P> <p >}</P> <p> // exchange data at the location. length-I and in Dex (maximum) two numbers </P> <p> swap (data, data. length-I, index); </P> <p >}</P> <p >}else {</P> <p> system. out. println ("the sorting type you entered is incorrect! "); </P> <p >}</P> <p> printarray (data ); // select the sorted array value for the output </P> <p >}</P> <p>/** </P> <p> * Insert sort </P>/ p> <p> * method: insert a record to an ordered table (possibly an empty table) with a new number of records increasing by 1. </P> <p> * performance: number of comparisons O (N ^ 2), N ^ 2/4 </P> <p> * Number of replications O (N ), N ^ 2/4 </P> <p> * The number of comparisons is the average of the first two, but the CPU time required for replication is less than the exchange, so the performance is more than doubled than that of Bubble sorting, it is faster than sorting. </P> <p> * @ Param data: array to be sorted </P> <p> * @ Param sorttype </P> <p> */</P> <p> Public void insertsort (INT [] data, string sorttype) {</P> <p> If (sorttype. equals ("ASC") {// Positive Sorting, small to large </P> <p> // number of rounds for comparison </P> <p> for (INT I = 1; I <data. length; I ++) {</P> <p> // ensure that the first I + 1 count is sorted in order </P> <p> for (Int J = 0; j <I; j ++) {</P> <p> If (data [J]> data [I]) {</P> <p> // Number of swap locations J and I </P> <p> swap (data, I, j ); </P> <p >}</P> <p>} </P> <p >}</P> <p >}else if (sorttype. equals ("DESC") {// reverse sorting, number of rounds from large to small </P> <p> // comparison </P> <p> for (INT I = 1; I <data. length; I ++) {</P> <p> // ensure that the first I + 1 count is sorted in order </P> <p> for (Int J = 0; j <I; j ++) {</P> <p> If (data [J] <data [I]) {</P> <p> // Number of swap locations J and I </P> <p> swap (data, I, j ); </P> <p >}</P> <p >}else {</P> <p> system. out. println ("the sorting type you entered is incorrect! "); </P> <p >}</P> <p> printarray (data ); // output the inserted sorted array value </P> <p >}</P> <p>/** </P> <p> * reverse array method </P> <p> * @ Param data source array </P> <p> */</P> <p> Public void reverse (INT [] data) {</P> <p> int length = data. length; </P> <p> int temp = 0; // Temporary Variable </P> <p> for (INT I = 0; I <length/2; I ++) {</P> <p> temp = data [I]; </P> <p> data [I] = data [length-1-I]; </P> <p> data [length-1-I] = temp; </P> <p >}</P> <p> printarray (data ); // Output the value to the converted array </P> <p >}</P> <p>/** </P> <p> * fast sorting </P> <p> * use divide and conquer for quick sorting) policy to divide a sequence (list) into two sub-sequences (sub-lists ). </P> <p> * Step: </P> <p> * 1. extract an element from the sequence, which is called a benchmark. </P> <p> * 2. re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After this split, the benchmark is its final position. This is called a partition operation. </P> <p> * 3. recursively (recursive) sorts the subseries smaller than the reference value element and the subseries greater than the reference value element. </P> <p> * at the bottom of the delivery, the number of rows is zero or one, that is, they are always sorted. Although it is always handed back, this algorithm always ends, because in each iteration, it will at least place an element at its final position. </P> <p> * @ Param data array to be sorted </P> <p> * @ Param low </P> <p> * @ Param high </P> <p> * @ see sorttest # qsort (INT [], int, INT) </P> <p> * @ see sorttest # qsort_desc (INT [], Int, INT) </P> <p> */</P> <p> Public void quicksort (INT [] data, string sorttype) {</P> <p> If (sorttype. equals ("ASC") {// Positive Sorting, from small to large </P> <p> qsort_asc (data, 0, Data. length-1); </P> <p>} else if (sorttype. equals ("DESC") {// reverse sorting, from large to small </P> <p> qsort_d ESC (data, 0, Data. length-1); </P> <p >}else {</P> <p> system. out. println ("the sorting type you entered is incorrect! "); </P> <p >}</P> <p>/** </P> <p> * specific implementation of fast sorting, forward </P> <p> * @ Param data </P> <p> * @ Param low </P> <p> * @ Param high </P> <p> */</P> <p> private void qsort_asc (INT data [], int low, int high) {</P> <p> int I, j, X; </P> <p> If (low <pigh) {// This condition is used to end recursion </P> <p> I = low; </P> <p> J = high; </P> <p> X = data [I]; </P> <p> while (I <j) {</P> <p> while (I <J & Data [J]> X) {</P> <p> j --; // find the first number less than X from the right to the left </P> <p >}</ P> <p> if (I <j) {</P> <p> data [I] = data [J]; </P> <p> I ++; </P> <p >}</P> <p> while (I <J & Data [I] <X) {</P> <p> I ++; // find the first number greater than X from left to right </P> <p >}</P> <p> if (I <j) {</P> <p> data [J] = data [I]; </P> <p> j --; </P> <p >}</P> <p> data [I] = X; </P> <p> qsort_asc (data, low, I-1); </P> <p> qsort_asc (data, I + 1, high ); </P> <p >}</P> <p>/** </P> <p> * specific implementation of fast sorting, reverse sorting </P> <p> * @ Param data </P> <p> * @ Param L Ow </P> <p> * @ Param high </P> <p> */</P> <p> private void qsort_desc (INT data [], int low, int high) {</P> <p> int I, j, X; </P> <p> If (low <pigh) {// This condition is used to end recursion </P> <p> I = low; </P> <p> J = high; </P> <p> X = data [I]; </P> <p> while (I <j) {</P> <p> while (I <J & Data [J] <X) {</P> <p> j --; // find the first number less than X from the right to the left </P> <p >}</P> <p> if (I <j) {</P> <p> data [I] = data [J]; </P> <p> I ++; </P> <p >}</P> <p> while (I <J & dat A [I]> X) {</P> <p> I ++; // find the first number greater than X from left to right </P> <p >}</P> <p> if (I <j) {</P> <p> data [J] = data [I]; </P> <p> j --; </P> <p >}</P> <p> data [I] = X; </P> <p> qsort_desc (data, low, I-1); </P> <p> qsort_desc (data, I + 1, high ); </P> <p >}</P> <p>/** </P> <p> * Binary Search for a specific integer in an integer position in the array (recursion) </P> <p> * query a linear table must be an ordered list </P> <p> * @ paramdataset </P> <p> * @ paramdata </P> <p> * @ parambeginindex </P> <p> * @ paramendindex </P> <p> * @ Re Turnindex </P> <p> */</P> <p> Public int binarysearch (INT [] dataset, int data, int beginindex, </P> <p> int endindex) {</P> <p> int midindex = (beginindex + endindex) >>> 1; // equivalent to mid = (low + high)/2, however, the efficiency is higher. </P> <p> If (Data <dataset [beginindex] | DATA> dataset [endindex] </P> <p> | beginindex> endindex) </P> <p> return-1; </P> <p> If (Data <dataset [midindex]) {</P> <p> return binarysearch (dataset, data, Beginindex, midindex-1); </P> <p >}else if (data> dataset [midindex]) {</P> <p> return binarysearch (dataset, data, midindex + 1, endindex); </P> <p >}else {</P> <p> return midindex; </P> <p >}</P> <p>/** </P> <p> * Binary Search for a specific integer in an integer position in the array (non-recursive) </P> <p> * query a linear table must be an ordered list </P> <p> * @ paramdataset </P> <p> * @ paramdata </P> <p> * @ returnindex </P> <p> */</P> <p> Public int binarysearch (INT [] dataset, int data) {</P> <p> Int beginindex = 0; </P> <p> int endindex = dataset. length-1; </P> <p> int midindex =-1; </P> <p> If (Data <dataset [beginindex] | DATA> dataset [endindex] </P> <p> | beginindex> endindex) </P> <p> return-1; </P> <p> while (beginindex <= endindex) {</P> <p> midindex = (beginindex + endindex) >>> 1; // equivalent to midindex = (beginindex + endindex)/2, however, the efficiency is higher </P> <p> If (Data <dataset [midindex]) {</P> <p> endindex = Midindex-1; </P> <p >}else if (data> dataset [midindex]) {</P> <p> beginindex = midindex + 1; </P> <p >}else {</P> <p> return midindex; </P> <p >}</P> <p> return-1; </P> <p >}</P> <p> Public static void main (string [] ARGs) {</P> <p> sorttest = new sorttest (); </P> <p> int [] array = sorttest. createarray (); </P> <p> system. out. println ("=========== after the Bubble Sorting (positive order) ============= "); </P> <p> sorttest. bubble Sort (array, "ASC"); </P> <p> system. out. println ("=========== after the Bubble Sorting (reverse) ============= "); </P> <p> sorttest. bubblesort (array, "DESC"); </P> <p> array = sorttest. createarray (); </P> <p> system. out. println ("============= after the inverted array ==========="); </P> <p> sorttest. reverse (array); </P> <p> array = sorttest. createarray (); </P> <p> system. out. println ("============= select the sorted (forward) ============= "); </P> <p> sorttest. selectsort (array, "ASC"); </P> <p> SY Stem. out. println ("==============select after sorting (reverse) ============= "); </P> <p> sorttest. selectsort (array, "DESC"); </P> <p> array = sorttest. createarray (); </P> <p> system. out. println ("=========== Insert the sorted (forward) ============= "); </P> <p> sorttest. insertsort (array, "ASC"); </P> <p> system. out. println ("=========== after inserting the sort (reverse) ============= "); </P> <p> sorttest. insertsort (array, "DESC"); </P> <p> array = sorttest. createarray (); </P> <p> system. out. print Ln ("=========== after fast sorting (positive order) ============ "); </P> <p> sorttest. quicksort (array, "ASC"); </P> <p> sorttest. printarray (array); </P> <p> system. out. println ("=========== after fast sorting (reverse) ============= "); </P> <p> sorttest. quicksort (array, "DESC"); </P> <p> sorttest. printarray (array); </P> <p> system. out. println ("=========== array Binary Search ==========="); </P> <p> system. out. println ("The number you are looking for is in the" + sorttest. binarysearch (array, 74) </P> <p> + "seat. (Subscript calculated from 0) "); </P> <p >}</P> <p>

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.