Recently collated the previous information. Some algorithms have not been implemented, hey, and then fill it!
/*** sorting algorithms are categorized as follows: * 1. Insert sort (direct insert sort, binary insert sort, hill Sort); * 2. Swap sort (bubble bubble sort, quick sort); * 3. Select sort (Directly select sort, heap sort); * 4. Merge sort; * selection of sorting methods: * (1) If n is small (for example, n≤50), direct insertion or direct selection can be used. * when the record size is small, the direct insertion sort is better; otherwise, the direct selection of the number of records is less than the direct interpolation, should choose the direct selection of the appropriate sort. (2) If the initial state of the file is basically ordered (order), then the direct insertion, bubbling or random quick ordering should be chosen; * (3) If n is larger, then the time complexity should be used O Sort by (NLGN): Quick sort, heap sort, or merge sort. * @author HaiYang * */public class SortTest {/** * Create an array * @param len * @return */public int[] creatarray (Int len) { Int arr[] = new int[len]; random r=new random (); for (int i=0;i<len-1;i++) { arr[i]=r.nextint (+) +1; } System.out.println ("initial array ..."); printarray (arr); return arr ;} /** * Print Array * @param arr */public static void printarray (int[] arr) {for (int i=0;i<arr.length;i++) { system.out.print (arr[i]+ "\ T"); }system.out.println ("");} /** * Inverted Array * @param arr */private void reversearray (Int[] arr) {int len = arr.length;int tmp =0;for (int i=0;i<len/2;i++) {tmp = arr[i];arr[i] = arr[len -1 - i];arr[len -1 - i] = tmp; PrintArray ( arr);}} /** * the position of the specified two element in the interchange array * @param data * @param x * @param y * @return */private void swap (int[] arry ,int x,int Y) {Int tmp = arry[x];arry[x] = arry[y];arry[y] = tmp;//printarray (Arry);} A of/** * bubble sort----exchange sort * method: The adjacent two elements are compared, if necessary, to exchange, Each time a loop is completed, the largest element is ranked at the end (such as from small to large), and the next loop is similar to the other number. * performance: Number of Comparisons O (n^2), N^2/2; number of exchanges o (n^2),n^2/4 * * @param data arrays to sort * @param sortType Sort Type * @return */public void bubblesort ( int[] data ,string sorttype) {if ( "ASC". Equals (SortType)) {//positive-order for ( int i=0;i<data.length-1;i++) {for (int j=0;j<data.length-1;j++) {if (data[j] > data[j+1]) {swap (data,j,j+1);}}} Else{for ( int i=0;i<data.length-1;i++) {for (int j=0;j<data.length-1;j++) {if (data[j] < data[j+1]) {swap (data,j,j+1);}}}} /** * * Direct Selection Sorting method----selection of a sort of * method: Each trip selects the smallest (or largest) element from the data element to be sorted, the order at the end of the ordered sequence until all the data elements to be sorted are finished. * performance: Number of Comparisons O (n^2),n^2/2 * Exchange times O (n ), n * the number of exchanges is much less than the bubble sort, Because the swap requires more CPU time than the comparison requires, the selection sort is faster than the bubble sort. * However, when n is larger, the CPU time required for comparison is the main position, so the performance and bubble sorting is not too much, but certainly faster. * @param data Array to sort * @ param sorttype Sort Type */public void selectesort ( int[] data , String sorttype) {if ( "ASC". Equals (SortType)) {//positive order int index ;//find the maximum for ( int i=1;i<data.length;i++) {index = 0;for (int j=1;j<=data.length-i;j++) {if (data[j] > data[ index ]) {index = j;}} Put the last swap (data,data.length-i,index );}} else{ int index ;//find Minimum index for ( int i=1;i<data.length;i++) {index = 0;for (int j=1;j<=data.length-i; j + +) {if (data[j] < data[ index ]) {index = j;}} Put the last swap (data,data.length-i,index );}} /** * Insert Sort * Method: Insert a record into an ordered table (possibly an empty table), resulting in an ordered table with a new number of records increased by 1. * performance: Compare times O (n^2),n^2/4 * copy Count O (n) The number of,n^2/4 * comparisons is the same as in the first two, and the CPU time required for replication is less than the swap, so the performance is much more than the bubble sort, and faster than the sort of selection. * * @param data Array to sort * @param sortType Sort Type * /public void insertsort ( int[] data ,string sorttype) {if ( "ASC". Equals ( SortType) {//Positive order for ( int i=1;i<data.length;i++) { for (int j=0;j< i;j++) {if ( data[j] > data[ i ]) {swap (data,i,j );}}} Else{ for ( int i=1;i<data.length;i++) { for (int j=0;j< i;j++) {if (data[j] < data[ i ]) {swap (data,i,j );}}}} /** * http://mobile.51cto.com/news-450017.htm * Quick Sort * quick sorting using the divide-and-conquer method (divide and Conquer) strategy to divide a sequence (list) into two sub-sequences (sub-lists). * Steps for: * 1. Select an element from the sequence called "datum" (pivot), * 2. reorder the columns, and all elements are placed in front of the datum in a smaller position than the base value. All elements that are larger than the base value are placed behind the datum (the same number can be on either side). After this split, the benchmark is its last position. This is called a split (partition) operation. * 3. recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements.  The bottom case of; * recursion is that the size of the sequence is 0 or one, which is always sorted. Although it is always handed back, the algorithm always ends, because in each iteration (iteration), it will at least put an element in its final position. * @param data Array to sort * @param low * @param high * @see sorttest#qsort (int[], int, int) * @see sorttest#qsort_desc (int[], Int, int) */public void QuickSort (Int[] data, string sorttype) {if ( "ASC". Equals (SortType)) {//Positive-order QSORT_ASC (data, 0, data.length - 1); }else{ qsort_desc (data, 0, data.length - &NBSP;1);} }private&nbsP;void qsort_desc (Int[] data, int low, int high) {int i, j, x;if (Low < high) { // This condition is used to end recursion i = low;j = high;x = data[i];while (i < j) {while (i < j && data[ J]&NBSP;<&NBSP;X) {j--; // right-to-left find the first number greater than x}if (i < j) {data[i] = data[j]; i++;} while (i < j && data[i] > x) {i++; // From left to right find the first number less than x}if (i < j) {data[j] = data[i]; j--;}} Data[i] for the sub-node left is greater than it, the right is less than his data[i] = x; printarray (data); system.out.println ( i Qsort_desc (data, low, i - 1); Qsort_desc (data, i + 1, high);}} PRIVATE&NBSP;VOID&NBSP;QSORT_ASC (Int[] data, int low, int high) {int i, j, x;if (Low < high) { // This condition is used to end recursion i = low;j = high;x = data[i];while (i < j) {while (i < j && data[j ]&NBSP;>&NBSP;X) {j--; // right-to-left find the first number less than x}if (i < j) {data[i] = data[j]; i++;} while (i < j && data[i] < x) {i++; // From left to right find the first number greater than x}if (i < j) {data[j] = data[i]; j--;}} Data[i] for the sub-node left is less than it, the right is greater than his data[i] = x; printarray (data); system.out.println ( i QSORT_ASC (data, low, i - 1); QSORT_ASC (Data, i + 1, high );}} The /** * heap is defined as follows: sequence of n elements {k0,k1,..., ki,..., k (n-1)} When and only if the next relationship is satisfied, called a heap. " ki<=k2i,ki<=k2i+1; or ki>=k2i,ki>=k2i+1. (i=1,2,..., [N/2])" If a one-dimensional array corresponding to this sequence (that is, a storage structure with one-dimensional array for this sequence) is treated as a complete binary tree, then the value of each node in the full binary tree is greater than or equal to the value of any one byte, if any), called the Big Top heap. The value of each node in a fully binary tree is less than or equal to the value of any one byte (if any), called the small top heap. Thus, if the sequence {k0,k1,..., K (n-1)} is a heap, then the top element of the heap (or the root of a complete binary tree) must be the minimum (or maximum) of n elements in the sequence. If each node in the heap is given an integer value label, the root node is marked as 0, for each node labeled I, its left child node (if present) is marked as 2*i+1, and its right child node (if present) is marked as 2*i+2, for a non-root node labeled I, Its parent node is marked (i-1)/2. With this tag, we are able to store the heap in an array, where the nodes are stored in the data so that they are labeled. * @param data * @param sortType */public void Heapsort (int[] data, string sorttype) { //1 Create a heap h[0..n-1]////2 swap the top of the heap (maximum or minimum) and the end of the heap 3. reduce the size of the heap by 1, in order to adjust the new array top data to the corresponding position////4. repeat step 2 until the heap size is 1if ( "ASC". Equals (SortType)) {//positive order Build_ Max_heap (data), for (int i = data.length - 1; i>=1; i--) {swap (data, i , 0); max_heapify (data, 0,i);}} Else{ build_min_heap (data); for (int i = data.length - 1; i>=1; i--) { Swap (data, i, 0); min_heapify (data, 0,i);}} /** * creating the largest heap * @param data */private void Build_max_heap (Int[] data) {int heap_size = data.length;for (int i=heap_ size/2-1;i>=0;i--) {max_heapify (data, i,heap_size);} PrintArray (data);} /** * maintain maximum heap structure * @param data arrays * @param i * @param heap_size */private void max_heapify (int[] data, int i,int heap_size) { int left = 2*i+1;int right = 2*i + 2;int largest = 0;if (Left < heap_size && data[i]<data[left]) { Largest = left;} Else{largest = i;} if (Right < heap_size && data[right] > data[largest]) {largest = right;} if (largest == i) {return ;} Else{swap (data, largest, i); //recursive exchange heap top last Change maximum max_heapify (data, largest,heap_size);}} Private void build_min_heap (Int[] data) {int heap_size = data.length;for (int i=heap_size/2-1;i>=0;i--) {min_heapify (data, i,heap_size);}} Private void min_heapify (int[] data, int i, int heap_size) { int left = 2*i+1;int right = 2*i + 2;int min = 0;if (left < heap_size && data[i]>data[left]) {min = left;} Else{min = i;} if (Right < heap_size && data[right] < data[min]) {min = Right;} if (min == i) {return ;} Else{swap (data, min, i); min_heapify (data, min,heap_size);}} Public void mergesort (int[] data, string sorttype) {sort (data,0,data.length-1);} Public static int[] sort (Int[] nums, int low, int high) {int mid = (Low + high) / 2;if (Low < high) {// left sort (nums, low, mid);// Right Sort (nums, mid + 1, high);// merges merge around ( Nums, low, mid, high);} Return nums;} Public static void merge (int[] nums, int low, int mid, int High) {int[] temp = new int[high - low + 1];int i = low;// left pointer int j = mid + 1;// right pointer int k = 0;// Move the smaller number first to the new array while (I <= mid && j <= high) {if (nums [I] < nums[j]) {temp[k++] = nums[i++];} else {temp[k++] = nums[j++];}} Move the left remaining number into the array while (I&NBSP;<=&NBSP;MID) {temp[k++] = nums[i++];} Move the number remaining on the right side into the array while (J <= high) {temp[k++] = nums[j++];} overwrites the number in the new array with the Nums array for (INT&NBSP;K2&NBSP;=&NBSP;0;&NBSP;K2&NBSP;<&NBSP;TEMP.LENGTH;&NBsp;k2++) &NBSP;{NUMS[K2&NBSP;+&NBSP;LOW]&NBSP;=&NBSP;TEMP[K2];}} /** * * binary search algorithm * * * binary lookup algorithm is a searching algorithm that finds a particular element in an ordered array. The search process starts with the middle element of the array, * if the intermediate element is exactly the element you want to find, the process ends; If a particular element is greater than or less than the middle element, * is found in the half of the array that is greater than or less than the middle element, and is compared with the beginning from the middle element. If in a step * array is empty, the generation table cannot be found. Each comparison of this search algorithm reduces the search scope by half. Binary search reduces the search area by half, with a time complexity of 0 (LOGN) . * @param srcArray * ordered arrays * * @param des * find elements * * @return des array subscript, no return -1 */ public found static int binarysearch (int[] srcarray, int des) { int low = 0; int high = srcarray.length-1; while (Low <= high) { int middle = (Low + high)/2; if (des == srcArray[middle]) { return middle; }else if (des < Srcarray[middle]) { high = middle - 1; }else { low = middle + 1; }}return -1; } /** * binary finds the position of a specific integer in an array of integers (recursion) * @paramdataset * @paramdata * @parambeginIndex * @paramendIndex * @returnindex */public static int binarysearch (int[] Dataset,int data,int beginindex,int endindex) { int midindex = (Beginindex+endindex)/2; if (data <dataset[beginindex]| | data>dataset[endindex]| | Beginindex>endindex) { return -1; } if (dAta <dataset[midindex]) { return binarysearch (dataset,data,beginIndex , midIndex-1); }else if (Data>dataset[midindex]) { return binarysearch (Dataset,data,midindex+1,endindex); }else { return midindex; } } public static void main (String[] args) {SortTest st = new sorttest (); Int[] data = st.creatarray (10); string sorttype = "ASC"; St.mergesort (Data, sorttype);p rintarray (data); System.out.println ( st.binarysearch ( new int[]{1,4,5,6,9,15,25},1));}}
Simple algorithm for sorting and finding Java code implementation