The principle of insertion sorting is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. For detailed steps, see the code and comments.
/** <Br/> * Insert sorting <br/> * <ul> <br/> * <li> starting from the first element, this element can be considered to have been sorted </LI> <br/> * <li> to retrieve the next element, </LI> <br/> * <li> if this element (sorted) is greater than the new element, move the element to the next position </LI> <br/> * <li> repeat Step 3, </LI> <br/> * <li> Insert the new element into the position </LI> <br/> * <li> repeat Step 2 </LI> <br/> * </ul> <br/> * @ Param numbers <br/> */<br /> Public static void insertsort (INT [] numbers) {<br/> int size = numbers. length, temp, J; <br/> for (INT I = 1; I <size; I ++) {<br/> temp = numbers [I]; <br/> for (j = I; j> 0 & temp <numbers [J-1]; j --) <br/> numbers [J] = numbers [J-1]; <br/> numbers [J] = temp; <br/>}< br/>}
Merge Sorting is an effective Sorting Algorithm Based on the merge operation. Merge refers to the operation of merging two sorted sequences into one. The reference code is as follows:
/** <Br/> * merge and sort <br/> * <ul> <br/> * <li> application space, this space is used to store the merged sequence. </LI> <br/> * <li> two pointers are set, the initial position is the start position of the two sorted sequences </LI> <br/> * <li> compare the elements pointed to by the two pointers, select a relatively small element and put it into the merging space, move the pointer to the next position </LI> <br/> * <li> repeat Step 3 until a pointer reaches the end of the sequence </LI> <br/> * <li> copy all the remaining elements of another sequence to the end of the merging sequence </LI> <br/> * </ul> <br/> * algorithm reference: <a href = "http://www.cnitblog.com/intrl/" mce_href = "http://www.cnitblog.com/intrl/"> JAVA tribe </a> <br/> * @ Param numbers <br/> */<br /> Public static void mergesort (INT [] numbers, int left, int right) {<br/> int T = 1; // number of elements in each group <br/> int size = right-left + 1; <br/> while (T <size) {<br/> int S = T; // number of elements in each group in this loop <br/> T = 2 * s; <br/> int I = left; <br/> while (I + (t-1) <size) {<br/> Merge (numbers, I, I + (s-1), I + (t-1); <br/> I + = T; <br/>}< br/> if (I + (s-1) <br/> Merge (numbers, I, I + (s-1 ), right ); <br/>}< br/>/** <br/> * merge algorithm implementation <br/> * @ Param data <br /> * @ Param P <br/> * @ Param q <br/> * @ Param r <br/> */<br/> Private Static void merge (INT [] data, int P, int Q, int R) {<br/> int [] B = new int [data. length]; <br/> int S = P; <br/> int T = q + 1; <br/> int K = P; <br/> while (S <= Q & T <= r) {<br/> If (data [s] <= data [T]) {<br/> B [k] = data [s]; <br/> S ++; <br/>} else {<br/> B [k] = data [T]; <br/> T ++; <br/>}< br/> K ++; <br/>}< br/> If (S = q + 1) <br/> B [k ++] = data [t ++]; <br/> else <br/> B [k ++] = data [s ++]; <br/> for (INT I = P; I <= R; I ++) <br/> data [I] = B [I]; <br/>}
Sort all the preceding sorting algorithms into the numbersort class and code
/** <Br/> * bubblesort. class <br/> */<br/> package test. sort; <br/> Import Java. util. random; <br/>/** <br/> * sorting class implemented by Java <br/> * @ author cyq <br/> * /<br/> public class numbersort {<br/>/** <br/> * private constructor, disable instantiation <br/> */<br/> private numbersort () {<br/> super (); <br/>}< br/>/** <br/> * bubble sort <br/> * <ul> <br/> * <li> compares adjacent elements. If the first is bigger than the second, exchange the two of them. </LI> <br/> * <li> performs the same operation on each adjacent element, from the first pair to the last one. At this point, the final element should be the largest number. </LI> <br/> * <li> repeat the preceding steps for all elements except the last one. </LI> <br/> * <li> continue to repeat the above steps for fewer and fewer elements each time until no comparison is required. </LI> <br/> * </ul> <br/> * @ Param numbers <br/> * integer array to be sorted <br/> */<br/> Public static void bubblesort (INT [] numbers) {<br/> int temp; // record the temporary median value <br/> int size = numbers. length; // array size <br/> for (INT I = 0; I <size-1; I ++) {<br/> for (Int J = I + 1; j <size; j ++) {<br/> If (numbers [I] <numbers [J]) {// switch the positions of two numbers <br/> temp = numbers [I]; <br/> numbers [I] = numbers [J]; <br/> numbers [J] = te MP; <br/>}< br/>/** <br/> * fast sorting <br/> <br /> * <ul> <br/> * <li> pick an element from the sequence, called "benchmark" </LI> <br/> * <li> resorts the sequence. All elements smaller than the benchmark value are placed before the benchmark, all elements are placed behind the benchmark (the same number can reach any side ). After this split, <br/> * the benchmark is its last position. This is called a partition operation. </LI> <br/> * <li> recursively sorts subsequences smaller than the reference value element and subsequences greater than the reference value element. </LI> <br/> * </ul> <br/> * @ Param numbers <br/> * @ Param start <br/> *@ param end <br/> */<br/> Public static void quicksort (INT [] numbers, int start, int end) {<br/> If (start <End) {<br/> int base = numbers [start]; // selected benchmark value (the first value serves as the benchmark value) <br/> int temp; // records the temporary median value <br/> int I = start, j = end; <br/> do {<br/> while (numbers [I] <base) & (I <End) <br/> I ++; <br/> while (numbers [J]> Base) & (J> Start) <br/> j --; <br/> if (I <= J) {<br/> temp = numbers [I]; <br/> numbers [I] = numbers [J]; <br/> numbers [J] = temp; <br/> I ++; <br/> j --; <br/>}< br/>}while (I <= J); <br/> If (start <j) <br/> quicksort (numbers, start, j); <br/> If (end> I) <br/> quicksort (numbers, I, end ); <br/>}< br/>/** <br/> * select sorting <br/> * <ul> <br/> * <li> find the smallest element in the unordered sequence, store it in the starting position of the sorting sequence </LI> <br/> * <li> and then from the remaining unordered Elements Continue to find the smallest element and put it at the end of the sorting sequence. </LI> <br/> * <li> and so on until all elements are sorted. </LI> <br/> * </ul> <br/> * @ Param numbers <br/> */<br/> Public static void selectsort (INT [] numbers) {<br/> int size = numbers. length, temp; <br/> for (INT I = 0; I <size; I ++) {<br/> int K = I; <br/> for (Int J = size-1; j> I; j --) {<br/> If (numbers [J] <numbers [k]) <br/> K = J; <br/>}< br/> temp = numbers [I]; <br/> numbers [I] = numbers [k]; <br/> numbers [k] = temp; <br/>}< br/>/** <br/> * sort insertion <br/> * <ul> <br/> * <li> starting from the first element, this element can be considered to have been sorted </LI> <br/> * <li> to retrieve the next element, </LI> <br/> * <li> if this element (sorted) is greater than the new element, move the element to the next position </LI> <br/> * <li> repeat Step 3, </LI> <br/> * <li> Insert the new element into the position </LI> <br/> * <li> repeat Step 2 </LI> <br/> * </ul> <br/> * @ Param numbers <br/> */<br /> Public static void insertsort (INT [] numbers) {<br/> int size = numbers. length, temp, J; <br/> for (INT I = 1; I <size; I ++) {<br/> temp = numbers [I]; <br/> for (j = I; j> 0 & temp <numbers [J-1]; j --) <br/> numbers [J] = numbers [J-1]; <br/> numbers [J] = temp; <br/>}< br/>/** <br/> * merge and sort <br/> * <ul> <br/> * <li> apply for a space, this space is used to store the merged sequence. </LI> <br/> * <li> two pointers are set, the initial position is the start position of the two sorted sequences </LI> <br/> * <li> compare the elements pointed to by the two pointers, select a relatively small element and put it into the merging space, move the pointer to the next position </LI> <br/> * <li> repeat Step 3 until a pointer reaches the end of the sequence </LI> <br/> * <li> copy all the remaining elements of another sequence to the end of the merging sequence </LI> <br/> * </ul> <br/> * algorithm reference: <a href = "http://www.cnitblog.com/intrl/" mce_href = "http://www.cnitblog.com/intrl/"> JAVA tribe </a> <br/> * @ Param numbers <br/> */<br /> Public static void mergesort (INT [] numbers, int left, int right) {<br/> int T = 1; // number of elements in each group <br/> int size = right-left + 1; <br/> while (T <size) {<br/> int S = T; // number of elements in each group in this loop <br/> T = 2 * s; <br/> int I = left; <br/> while (I + (t-1) <size) {<br/> Merge (numbers, I, I + (s-1), I + (t-1); <br/> I + = T; <br/>}< br/> if (I + (s-1) <br/> Merge (numbers, I, I + (s-1 ), right ); <br/>}< br/>/** <br/> * merge algorithm implementation <br/> * @ Param data <br /> * @ Param P <br/> * @ Param q <br/> * @ Param r <br/> */<br/> Private Static void merge (INT [] data, int P, int Q, int R) {<br/> int [] B = new int [data. length]; <br/> int S = P; <br/> int T = q + 1; <br/> int K = P; <br/> while (S <= Q & T <= r) {<br/> If (data [s] <= data [T]) {<br/> B [k] = data [s]; <br/> S ++; <br/>} else {<br/> B [k] = data [T]; <br/> T ++; <br/>}< br/> K ++; <br/>}< br/> If (S = q + 1) <br/> B [k ++] = data [t ++]; <br/> else <br/> B [k ++] = data [s ++]; <br/> for (INT I = P; I <= R; I ++) <br/> data [I] = B [I]; <br/>}</P> <p >}< br/>
Digital sorting algorithms are usually used as the basic content of the algorithm entry-level course. They are often used less frequently in practical applications (especially in general commercial software), but are implemented by sorting algorithms, you can gain an in-depth understanding of the characteristics of computer languages and use these features as the basis for learning various programming languages.