Import Java. util. arrays;/*** @ author 255166 * @ Version Feb 27,201 2 10:39:00 am */public class sortalgorithm {/*** for array I, j-location content exchange * @ Param inputarray * @ Param I * @ Param J * @ return */public static int [] exchangevalue (INT [] inputarray, int I, Int J) {int temp = inputarray [I]; inputarray [I] = inputarray [J]; inputarray [J] = temp; return inputarray ;} /* bubble sort * the basic concept of bubblesort is to compare the adjacent two numbers in sequence and put the decimal points in front, Put large numbers behind them. That is, in the first step: first compare the number of 1st and the number of 2nd, * Before the decimal point, after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. * At this point, the first round is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). * before placing the decimal number, after a large number is placed, it is always compared to the second to the last (the highest position on the last), and the second row ends, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). * In this case, repeat the above process until the sorting is completed. * @ Param inputarray * @ return */public static int [] bubblesort (INT [] inputarray) {for (INT I = 0; I <inputarray. length; I ++) {for (Int J = 0; j <inputarray. length-I-1; j ++) {If (inputarray [J]> inputarray [J + 1]) {exchangevalue (inputarray, J, J + 1 );}}} return inputarray;}/*** @ Param ARGs */public static void main (string [] ARGs) {int [] sortint = {8, 5, 12, 1, 22, 2, 99,6, 3, 7,100, 4, 44, 9, 88}; // bubblesort (sortint); // quicksort (sortint, 0, sortint. length-1); // selectionsort (sortint); // insertsort (sortint); shellsort (sortint); system. out. println ("Sort ASC:" + arrays. tostring (sortint);}/*** basic idea of hill sorting **: * first, take an integer D1 less than N as the first increment, divides all records of a file into D1 groups. All records whose distance is a multiple of DL are placed in the same group. * Sort the inserted persons in each group first, and then repeat the group and sorting in the second incremental D2 <d1, until the incremental dt = 1 (dt <DT-L <... <D2 <d1), * indicates that all records are placed in the same group for direct insertion and sorting. * @ Param inputarray */public static void shellsort (INT [] inputarray) {// groups records for (INT increment = inputarray. length/2; increment> 0; increment/= 2) {// sort data in the same group by insertion (INT I = increment; I <inputarray. length; I ++) {int temp = inputarray [I]; Int J = 0; For (j = I; j> = increment; j-= increment) {If (temp <inputarray [J-increment]) {inputarray [J] = inputarray [J-increment];} else {break ;}} inputarray [J] = temp ;}}/*** insert sort * divides the series of n elements Ordered and unordered parts, such as * {, {A2, A3, A4 ,..., An }}* {A1 (1), A2 (1)}, {A3 (1), A4 (1 )..., An (1 )}}*... * {A1 (n-1), A2 (n-1 ),...}, {An (n-1)} * each processing is to compare the first element of the unordered series with the elements of the ordered series one by one from the back to find the insert position, insert this element into the proper position of the ordered sequence. * @ Param inputarray * @ return */public static int [] insertsort (INT [] inputarray) {for (INT I = 1; I <inputarray. length; I ++) {// I from the beginning, because the first number is already sorted, // compare the first element of the unordered series with the elements of the ordered series one by one from the back to the front (Int J = I; j> 0; J --) {If (inputarray [J] <inputarray [J-1]) {int temp = inputarray [J]; inputarray [J] = inputarray [J-1]; inputarray [J-1] = temp ;}} return inputarray;}/*** select to sort the files of N records. The sorting can go through n-1 straight lines. Select and sort to get the ordered result: * ① initial state: the disordered area is R [1. N], and the ordered area is empty. * ② 1st-byte sorting * in the unordered area R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count. *...... * ③ When I sort * I sort, the current ordered area and disordered area are R [1 .. I-1] and R (1 ≤ I ≤ N-1) respectively ). This sort selects the record R [k] with the smallest keyword from the current unordered area, swaps it with the 1st records R in the unordered area, * enables R [1 .. i] and R change to a new ordered area with one more record count and a new unordered area with one fewer record count. * In this way, the direct sorting of files with N records can be directly selected and sorted through n-1 to obtain the ordered results. * @ Param inputarray * @ return */public static int [] selectionsort (INT [] inputarray) {for (INT I = 0; I <inputarray. length; I ++) {int initialindex = I; int initialvalue = inputarray [I]; for (Int J = I + 1; j <inputarray. length; j ++) {If (inputarray [J] <initialvalue) {initialindex = J; initialvalue = inputarray [J] ;}} int temp = inputarray [I]; inputarray [I] = initialvalue; inputarray [initialindex] = temp;} return inputarray;}/*** Quick Sort ** The fast sorting algorithm is: * 1) set two variables I, j, when the sorting starts: I = 0, j = N-1; * 2) take the first array element as the key data and assign it to the key, that is, key = A [0]; * 3) search forward from J, that is, start from the back forward search (j = J-1), find the first value less than the key a [J], a [J] And a [I] exchange; * 4) search backward from I, that is, search backward from the beginning (I = I + 1), find the first a [I] that is greater than the key, A [I] exchange with a [J]; * 5) Repeat steps 3rd, 4, and 5 until I = J; (Step 4 is not found in the program when J = J-1, I = I + 1 until it is found. When I is found and switched, the position of the J pointer remains unchanged. In addition, when I = J, this process must be the end of the last loop completed by I + or J .) * For example, the values of array a to be sorted are: (initial key data: Key = 49) Note that the key will never change and will always be compared with the key, no matter what position, the final goal is to put X in the middle and a small one in front and a large one in the back. * A [0] a [1] A [2] a [3] a [4] a [5] a [6]: * 49 38 65 97 76 13 27 * after the first exchange: 27 38 65 97 76 13 49 * after the second exchange: 27 38 49 97 76 13 65 * (according to the fourth step of the algorithm, find the value> X, 65> 49, and exchange the two, at this time: I = 3) * After the third exchange: 27 38 13 97 76 49 65: 27 38 13 49 76 97 65 * (according to the fourth step of the algorithm, find a value greater than X from the beginning, 97> 49, exchange the two, at this time: I = 4, j = 6) * When we execute step 3, we find that I = J and end the fast sorting. the result after a fast sorting is 27 38 13 49 76 97 65, that is, all numbers greater than 49 After 49, all numbers smaller than 49 are in front of 49. * @ Param inputarray * @ Param left * @ Param right * @ return */public static int [] quicksort (INT [] inputarray, int left, int right) {int middlevalue = inputarray [left]; int I = left; Int J = right; // first sort while (I <j) {If (left <right) {While (I <J & inputarray [J]> middlevalue) {J --;} if (I <j) {int temp = inputarray [I]; inputarray [I] = inputarray [J]; inputarray [J] = temp; }}if (left <right) {While (I <J & inputarray [I] <middlevalue) {I ++;} if (I <j) {int temp = inputarray [I]; inputarray [I] = inputarray [J]; inputarray [J] = temp ;}}// if (left <j) {quicksort (inputarray, left, I-1) Before recursion );} // If (Right> I) {quicksort (inputarray, I + 1, right);} return inputarray ;}}