I. Bubble Sorting
Thought: Repeat visits to the sequence to be sorted, compare two elements at a time, exchange them if their order is wrong, the smallest one comes up, followed by the second smallest.
Time complexity: O(n^2)
Spatial complexity: O(1)
Stability: Stability
1.
- /**
- * Bubble sort
- * @param disOrderArray
- * @return
- */
- public static int[] BubbleSort(int[] disOrderArray) {
- int temp;
- //Layer 1 loop: Indicates the number of comparisons, such as length elements, and the number of comparisons is length-1.
- for(int i=0;i<disOrderArray.length-1;i++)
- {
- //Exchange the smallest number with the relative top of the bubbles, the smallest one coming up at a time, and the second smallest.
- for(int j=disOrderArray.length-1;j>i;j–)
- {
- //Here is < when it returns from small to large, and > when it returns from large to small.
- if(disOrderArray[j] < disOrderArray[j-1])
- {
- temp = disOrderArray[j];
- disOrderArray[j] = disOrderArray[j-1];
- disOrderArray[j-1] = temp;
- }
- }
- }
- return disOrderArray;
- }
/** * Bubble sort * @param disOrderArray * @return */ public static int[] BubbleSort(int[] disOrderArray) { int temp; // Layer 1 loop: Indicates the number of comparisons, such as length elements, and the number of comparisons is length-1. for(int i=0;i<disOrderArray.length-1;i++) { // Exchange the smallest number with the relative top of "bubbles". The smallest number comes up at one time, and the second smallest. for(int j=disOrderArray.length-1;j>i;j--) { // Here is < when it returns from small to large, and > when it returns from large to small. if(disOrderArray[j] < disOrderArray[j-1]) { temp = disOrderArray[j]; disOrderArray[j] = disOrderArray[j-1]; disOrderArray[j-1] = temp; } } } return disOrderArray; }
2. Quick Sorting
Idea: By one-time sorting, the waiting records are divided into two parts, one part of which has smaller keywords than the other part. Then the two parts can be sorted separately to achieve the purpose of ordering the whole sequence.
Time complexity: O(nlogn), at worst O(n^2)
Spatial complexity: O(1)
Stability: instability
- /*
- *
- * Quick sort
- *
- * Thoughts:
- * By one-time sorting, the waiting records are divided into two separate parts, in which the keywords of one part of the records are smaller than those of the other.
- * Then the two parts of records can be sorted separately to achieve the purpose of ordering the whole sequence.
- *
- * The essence is to find a base (pivot, watershed, the role is that the left side is smaller than it, the right side is larger than it. Random, named base.
- * Start at the right-most edge of the sequence to find something smaller than base
- * ,If it's small, change the position so that base moves to the right (smaller than base in comparison) position (marked as temporary high bit), so that the right side of base is larger than base.
- * Then, start at the leftmost edge of the sequence to find something larger than base
- * ,If it's big, change the position so that the base moves to the left (larger than base in comparison) position (marked as temporary low bit), so that the left side of the base is smaller than base.
- *
- * Cycle the above two steps until low = heigh, which makes it possible to really find the pivot, the watershed. Return to this location, the sequence on the left and right of the watershed, and recurse again, respectively.
- */
- public static int[] quickSort(int[] arr, int low, int heigh) {
- if(low < heigh)
- {
- int division = partition(arr, low, heigh);
- quickSort(arr, low, division - 1);
- quickSort(arr, division + 1, heigh);
- }
- return arr;
- }
- //The watershed, the base, the left are smaller than this, and the right are larger.
- private static int partition(int[] arr, int low, int heigh) {
- int base = arr[low]; //Make pivot (watershed) records with the first record of the subtable
- while (low < heigh)
- {
- //Change <= and >= in the following two while loops to get the order from large to small
- //Scanning alternately from both ends of the table to the middle, ranging from small to large
- while (low < heigh && arr[heigh] >= base)
- {
- heigh–;
- }
- //If the high bit is less than base,base assigns to the current heigh bit, base moves to (swaps) here, and the right heigh bit is larger than base.
- swap(arr, heigh, low);
- while(low < heigh && arr[low] <= base)
- {
- low++;
- }
- //If there is a base in the lower position,
- swap(arr, heigh, low);
- }
- //Now low=heigh
- return low;
- }
- //Exchange size
- private static void swap(int[] arr, int heigh, int low) {
- int temp = arr[heigh];
- arr[heigh] = arr[low];
- arr[low] = temp;
- }
/* * * Quick sort * * thought: * By one-time sorting, the waiting records are divided into two separate parts, in which the keywords of one part of the records are smaller than those of the other. * Then the two parts of records can be sorted separately to achieve the purpose of ordering the whole sequence. * * The essence is to find a base (pivot, watershed, the role is that the left side is smaller than it, the right side is larger than it. Random, named base. * Start at the right-most edge of the sequence to find something smaller than base * If it's small, change the position so that the base moves to the right (smaller than base in comparison) position (marked as the temporary high bit), so that the right side of the base is larger than the base. * Then, start at the leftmost edge of the sequence to find something larger than base * If it is large, change the position, so that the base moves to the left (larger than base in comparison) position (marked as temporary low bit), so that the left side of the base is smaller than base * * Cycle the above two steps until Low = heigh, which makes it possible to really find the pivot, the watershed. Return to this location, the sequence on the left and right of the watershed, and recurse again, respectively. */ public static int[] quickSort(int[] arr, int low, int heigh) { if(low < heigh) { int division = partition(arr, low, heigh); quickSort(arr, low, division - 1); quickSort(arr, division + 1, heigh); } return arr; } // The watershed, the base, the left are smaller than this, and the right are larger. private static int partition(int[] arr, int low, int heigh) { int base = arr[low]; // Make pivotal (watershed) records with the first record of the subtable while (low < heigh) { // Change <= and >= in the following two while loops to get the order from large to small // Scanning alternately from both ends of the table to the middle, ranging from small to large while (low < heigh && arr[heigh] >= base) { heigh--; } // If the high bit is less than base,base assigns the current heigh bit, base moves to (swaps) here, and the right side of the heigh bit is larger than base. swap(arr, heigh, low); while(low < heigh && arr[low] <= base) { low++; } // If the base is large at the lower level, swap(arr, heigh, low); } // Now low=heigh return low; } // swap size private static void swap(int[] arr, int heigh, int low) { int temp = arr[heigh]; arr[heigh] = arr[low]; arr[low] = temp; }
3. Direct selection ranking:
Thought: Each sorting will select the smallest (or largest) value, which will be placed after the sorted sequence.
Time complexity: O(n^2)
Spatial complexity: O(1)
Stability: instability
- /**
- * Direct Selection Sorting
- * Select the smallest value for each trip by direct selection sort
- * @param arr
- * @return
- */
- public static int[] selectionSort(int[] arr) {
- for(int i=0;i<arr.length;i++)
- {
- for(int j=i+1;j<arr.length;j++)
- {
- if(arr[i] > arr[j])
- {
- int temp = arr[j];
- arr[j] = arr[i];
- arr[i] = temp;
- }
- }
- }
- return arr;
- }
/** * Direct Selection Sorting * Select the smallest value for each trip by direct selection sort * @param arr * @return */ public static int[] selectionSort(int[] arr) { for(int i=0;i<arr.length;i++) { for(int j=i+1;j<arr.length;j++) { if(arr[i] > arr[j]) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } } return arr; }
4. Heap Sorting
Thought: Heap sorting uses this heap data structure A sort designed algorithm Using the characteristics of arrays, you can quickly locate the elements of a specified index
Time complexity: O(nlogn)
Spatial complexity: O(1)
Stability: instability
- /**
- * Heap sort
- * Heapsort (Heapsort) is a sort algorithm designed by using heap as a data structure. It can quickly locate the elements of a specified index by using the characteristics of arrays.
- * @param arr
- * @return
- */
- public static int[] heapSort(int[] arr) {
- int i;
- //Make arr a large top heap
- //From 0 to arr.length/2, these are nodes with children
- //It is meaningless to construct a large pile of nodes without children.
- for (i = arr.length / 2; i >= 0; i–)
- {
- heapAdjust(arr, i, arr.length - 1);
- }
- for (i = arr.length - 1; i > 0; i–)
- {
- swap(arr, 0, i);
- //Ar [0... i-1] Reconstructed into a large top heap
- heapAdjust(arr, 0, i - 1);
- }
- return arr;
- }
- private static void heapAdjust(int[] arr, int s, int m) {
- int temp, j;
- temp = arr[s]; //Point to the root node of the temporary (relative to the root node)
- for (j = 2 * s; j <= m; j *= 2)
- {
- //If the right node is larger than the left node, the current node moves to the right node
- if (j < m && arr[j] < arr[j + 1])
- {
- //Point to the right node
- j++;
- }
- //The current parent node is larger than the node to which it is pointing
- //There is no need to do anything about it.
- if (temp >= arr[j])
- {
- break;
- }
- //The current parent node is smaller than the next child node
- //Replace this child node with the parent node
- //The current location, if it is a leaf node, should be the smallest (relative to its ancestors)
- //The purpose of this method is to exchange the values of parent and child ren to construct a large root heap.
- //Execution to this point indicates that the parent node of the current node (temporary root node is smaller than the current node)
- //Move the current node up and change position
- //It doesn't matter if arr[s] is overwritten, because temp records this value (the original root node (relative parent))
- arr[s] = arr[j];
- //Now consider the current element as a temporary parent node
- //To find the child node of this element at this time, see if it is larger than the current value.
- //The last s points to the element currently traversed
- s = j;
- }
- arr[s] = temp;
- }
/** * heap sort * Heapsort (Heapsort) is a sort algorithm designed by using heap as a data structure. It can quickly locate the elements of a specified index by using the characteristics of arrays. * @param arr * @return */ public static int[] heapSort(int[] arr) { int i; // Make arr a large top heap // From 0 to arr.length/2, these are nodes with children // It's meaningless to construct a big pile of nodes without children. for (i = arr.length / 2; i >= 0; i--) { heapAdjust(arr, i, arr.length - 1); } for (i = arr.length - 1; i > 0; i--) { swap(arr, 0, i); // Reconstruct arr[0...i-1] into a large top heap heapAdjust(arr, 0, i - 1); } return arr; } private static void heapAdjust(int[] arr, int s, int m) { int temp, j; temp = arr[s]; // points to the root node of the temporary (relative to the root node) for (j = 2 * s; j <= m; j *= 2) { // If the right node is larger than the left node, the current node moves to the right node if (j < m && arr[j] < arr[j + 1]) { // Point to the right node j++; } // The current parent node is larger than the node to which it is pointing. // There is no need to do anything about it. if (temp >= arr[j]) { break; } // The current parent node is smaller than the next child node // Replace this child node with the parent node // The current location, if it is a leaf node, should be the smallest (relative to its ancestors). // The purpose of this method is to exchange the values of parent and child ren to construct a large root heap. // Execution to this point indicates the parent of the current node (temporary root node is smaller than the current node). // Move the current node up and change position // It doesn't matter if arr[s] is overwritten, because temp records this value (the original root node (relative parent)). arr[s] = arr[j]; // Now consider the current element as a temporary parent node // To find the child node of this element at this time, see if it is larger than the current value. // The last s points to the element currently traversed s = j; } arr[s] = temp; }
5. Insertion sort
Idea: Insert a record into an ordered table, and get a new ordered table with 1 record added. By default, the first element is treated as an ordered table, and then the next element is inserted in turn.
Time complexity: O(n^2)
Spatial complexity: O(1)
Stability: Stability
- /**
- * Insertion sort
- * Idea: Insert a record into an ordered table, and get a new ordered table with the number of records increasing by 1.
- * By default, consider the first element as an ordered table, inserting the desired element at a time
- * Time complexity O(n^2)
- * Spatial complexity O(1) is suitable for small number of records
- * @param arr
- * @return
- */
- public static int[] InsertSort(int[] arr) {
- //Arrangement from small to large
- for(int i=1;i<arr.length;i++)
- {
- //Elements to be inserted
- int temp = arr[i];
- int j;
- for(j=i-1;j>=0 && temp < arr[j];j–)
- {
- //When the insertion element is smaller than the existing one, move the existing one back until the element is larger than the insertion element or has reached the top of the sequence.
- arr[j+1] = arr[j];
- }
- arr[j+1] = temp;
- }
- return arr;
- }
/** * Insertion sort * Idea: Insert a record into an ordered table, and get a new ordered table with the number of records increasing by 1. * By default, consider the first element as an ordered table, inserting the desired element at a time * Time complexity O(n^2) * Spatial complexity O(1) is suitable for small number of records * @param arr * @return */ public static int[] InsertSort(int[] arr) { // Arrangement from small to large for(int i=1;i<arr.length;i++) { // Elements to be inserted int temp = arr[i]; int j; for(j=i-1;j>=0 && temp < arr[j];j--) { // When the insertion element is smaller than the existing one, move the existing one back until the element is larger than the insertion element or has reached the top of the sequence. arr[j+1] = arr[j]; } arr[j+1] = temp; } return arr; }
6. Half-folded insertion sort
Idea: Half-insert sorting is rewritten based on direct insert sorting, which can reduce the number of "moves" and "comparisons"
Time complexity: O(n^2)
Spatial complexity: O(1)
Stability: Stability
- /**
- * Binary Insertion Sort
- * Advantages: Reduce the number of "comparisons" and "moves"
- * @param arr
- * @return
- */
- public static int[] BInsertSort(int[] arr){
- for(int i=1;i<arr.length;i++)
- {
- //Elements to be inserted
- int temp = arr[i];
- int j;
- int low = 0, high = i-1;
- while(low <= high) //Find the location of orderly insertion by halving in arr[low..high]
- {
- int m = (low + high)/2;//Halve
- if(temp < arr[m])
- {
- high = m-1; //Insertion point in lower half
- }
- else
- {
- low = m+1; //Insertion point in high half area
- }
- }
- //Record shift
- for(j=i-1;j>=high+1;j–)
- {
- arr[j+1] = arr[j];
- }
- arr[j+1] = temp;
- }
- return arr;
- }
/** * Half Insert Sort * Advantages: Reduce the number of "comparisons" and "moves" * @param arr * @return */ public static int[] BInsertSort(int[] arr){ for(int i=1;i<arr.length;i++) { // Elements to be inserted int temp = arr[i]; int j; int low = 0, high = i-1; While (low <= high) // Halve in arr[low..high] to find the location of the ordered insertion { Int m = Low + high) / 2; // half if(temp < arr[m]) { high = m-1; // insertion point in the lower half } else { low = m+1; // insertion point in high half } } // Record postponement for(j=i-1;j>=high+1;j--) { arr[j+1] = arr[j]; } arr[j+1] = temp; } return arr; }
Seventh, Hill Ranking:
Thought: Hill sort is also a sort of insertion sort, which is directly improved for insertion sort. This method is also called "reduced incremental sort".
Time complexity: O(n^2)
Spatial complexity: O(1)
Stability: instability
- /**
- * Hill Sorting (Reduced Incremental Sorting)
- * Hill sort is also a sort of insertion sort, except that it has an increment, and the last increment must be 1.
- * @param arr
- * @return
- */
- public static int[] ShellInsert(int[] arr){
- int step = arr.length/2; //Increment
- //Guarantee the last increment of 1
- while(step >= 1)
- {
- for(int i=step;i<arr.length;i++)
- {
- int temp = arr[i];
- int j = 0;
- //The difference between root insertion sort is here
- for(j=i-step;j>=0 && temp<arr[j];j-=step)
- {
- arr[j+step] = arr[j];
- }
- arr[j+step] = temp;
- }
- step /= 2;
- }
- return arr;
- }
/** * Hill Sorting (Reduced Incremental Sorting) * Hill sort is also a sort of insertion sort, except that it has an increment, and the last increment must be 1. * @param arr * @return */ public static int[] ShellInsert(int[] arr){ int step = arr.length/2; // take increments // Guarantee the last increment of 1 while(step >= 1) { for(int i=step;i<arr.length;i++) { int temp = arr[i]; int j = 0; // The difference between root insertion sort is here for(j=i-step;j>=0 && temp<arr[j];j-=step) { arr[j+step] = arr[j]; } arr[j+step] = temp; } step /= 2; } return arr; }
8. Merging and Sorting
Idea: Merging and sorting is to combine two or more ordered tables into an ordered table. The algorithm is implemented by dividing and conquering.
Time complexity: O(nlogn)
Spatial complexity: O(n)
Stability: Stability
- /**
- * Merge sort
- * Merging and sorting is to combine two or more ordered tables into a new ordered table.
- * Time complexity O(nlog2n)
- * @param arr
- * @param tempArray
- * @param left
- * @param right
- * @return
- */
- public static int[] mergeSort(int[] arr, int left,int right) {
- if (left < right)
- {
- //Segmentation location
- int middle = (left + right) / 2;
- //Recursively partitioning the left sequence of arrays
- mergeSort(arr, left, middle);
- //Recursively partitioning the right sequence of arrays
- mergeSort(arr, middle+1, right);
- //Merge left and right arrays
- Merge(arr, left, middle, right);
- }
- return arr;
- }
- private static void Merge(int[] arr, int left, int middle,int right) {
- int[] tempArray = new int[arr.length];
- int leftEnd = middle;
- int rightStart = middle+1;
- //Subscription of temporary arrays
- int tempIndex = left;
- int tmp = left;
- //The case where neither interval of the first cycle ends
- while ((left <= leftEnd) && (rightStart <= right))
- {
- //The left is smaller than the right, insert the left first.
- if (arr[left] < arr[rightStart])
- {
- tempArray[tempIndex++] = arr[left++];
- }
- else
- {
- tempArray[tempIndex++] = arr[rightStart++];
- }
- }
- //Judging whether the left sequence ends
- while (left <= leftEnd)
- {
- tempArray[tempIndex++] = arr[left++];
- }
- //Judging whether the right sequence ends
- while (rightStart <= right)
- {
- tempArray[tempIndex++] = arr[rightStart++];
- }
- //Copy the contents of the temporary array back to the original array.
- //(The contents of the original left-right range are copied back to the original array)
- while (tmp <= right) {
- arr[tmp] = tempArray[tmp++];
- }
- }
/** * Merge Sort * Merging and sorting is to combine two or more ordered tables into a new ordered table. * Time complexity O(nlog2n) * @param arr * @param tempArray * @param left * @param right * @return */ public static int[] mergeSort(int[] arr, int left,int right) { if (left < right) { // Segmentation location int middle = (left + right) / 2; // Recursively partitioning the left sequence of arrays mergeSort(arr, left, middle); // Recursively partitioning the right sequence of arrays mergeSort(arr, middle+1, right); // Merge left and right arrays Merge(arr, left, middle, right); } return arr; } private static void Merge(int[] arr, int left, int middle,int right) { int[] tempArray = new int[arr.length]; int leftEnd = middle; int rightStart = middle+1; // Subscription of Temporary Array int tempIndex = left; int tmp = left; // The case where neither interval of the first cycle ends while ((left <= leftEnd) && (rightStart <= right)) { // The left side is smaller than the right side. Insert the left side first. if (arr[left] < arr[rightStart]) { tempArray[tempIndex++] = arr[left++]; } else { tempArray[tempIndex++] = arr[rightStart++]; } } // Judging whether the left sequence ends while (left <= leftEnd) { tempArray[tempIndex++] = arr[left++]; } // Judging whether the right sequence ends while (rightStart <= right) { tempArray[tempIndex++] = arr[rightStart++]; } // Copy the contents of the temporary array back to the original array // (The contents of the original left-right range are copied back to the original array) while (tmp <= right) { arr[tmp] = tempArray[tmp++]; } }
9. cardinal ordination
Thought: The cardinality is sorted first according to the low position, then collected; then sorted by the high position, then collected, and so on, until the highest position.
Note: It means that keywords are classified into Radix boxes. When keywords are numbers, the base number is 10. When keywords are letters, the base number is 26.
Time complexity: O(n+d)
Spatial complexity: O(n)
Stability: Stability
- /**
- * Radix sorting
- * @radix The cardinality means that Radix boxes are categorized according to keywords. When keywords are numbers, the cardinality is 10.
- * @d Number of ranking elements
- * @return
- */
- public static int[] RadixSort(int[] arr, int radix, int d){
- //For temporary elements
- int[] temp = new int[arr.length];
- //For counting sort
- int[] count = new int[radix];
- int divide = 1;
- for(int i=0;i<d;i++)
- {
- System.arraycopy(arr, 0, temp, 0, arr.length);
- //Reset the count array to start counting the next keyword.
- Arrays.fill(count, 0);
- //Calculate subkeywords for each data to be sorted
- for(int j=0;j<arr.length;j++)
- {
- int tempKey = (temp[j]/divide)%radix;
- count[tempKey]++;
- }
- for(int j=1;j<radix;j++)
- {
- count[j] = count[j] + count[j-1];
- }
- //Sort the specified data by subkeywords
- for(int j=arr.length-1;j>=0;j–)
- {
- int tempKey = (temp[j]/divide)%radix;
- count[tempKey]–;
- arr[count[tempKey]] = temp[j];
- }
- divide = divide * radix;
- }
- return arr;
- }
/** * cardinal sorting *@ radix cardinality means radix boxes categorized by keywords. When keywords are numbers, the cardinality is 10 *@ The number of digits of d-sorted elements * @return */ public static int[] RadixSort(int[] arr, int radix, int d){ // For temporary elements int[] temp = new int[arr.length]; // For counting sort int[] count = new int[radix]; int divide = 1; for(int i=0;i<d;i++) { System.arraycopy(arr, 0, temp, 0, arr.length); // Reset the count array to start counting the next keyword Arrays.fill(count, 0); // Calculate subkeywords for each data to be sorted for(int j=0;j<arr.length;j++) { int tempKey = (temp[j]/divide)%radix; count[tempKey]++; } for(int j=1;j<radix;j++) { count[j] = count[j] + count[j-1]; } // Sort specified data by subkeywords for(int j=arr.length-1;j>=0;j--) { int tempKey = (temp[j]/divide)%radix; count[tempKey]--; arr[count[tempKey]] = temp[j]; } divide = divide * radix; } return arr; }
- public static void main(String[] args) {
- //By default, the base is arranged from small to large
- // int[] disOrderArray = {3,1,5,7,0};
- //Bubble sort
- // disOrderArray = BubbleSort(disOrderArray);
- //Quick sort
- // disOrderArray = quickSort(disOrderArray, 0, disOrderArray.length-1);
- //Direct Selection Sorting
- // disOrderArray = selectionSort(disOrderArray);
- //Heap sort
- // disOrderArray = heapSort(disOrderArray);
- //Direct insertion sort
- // disOrderArray = InsertSort(disOrderArray);
- //Half Insert Sort (Binary Search Sort)
- // disOrderArray = BInsertSort(disOrderArray);
- //Shell Sort
- // disOrderArray = ShellInsert(disOrderArray);
- //Merge sort
- // disOrderArray = mergeSort(disOrderArray, 0, disOrderArray.length-1);
- //Radix sorting
- int[] disOrderArray = {3,2,3,2,5,333,45566,2345678,78,990,12,432,56};
- disOrderArray = RadixSort(disOrderArray, 10, 7);
- for(int i=0;i<disOrderArray.length;i++)
- {
- System.out.print(disOrderArray[i]+" ");
- }
- }
public static void main(String[] args) { // By default, the base is arranged from small to large // int[] disOrderArray = {3,1,5,7,0}; // Bubble sort // disOrderArray = BubbleSort(disOrderArray); // Quick sort // disOrderArray = quickSort(disOrderArray, 0, disOrderArray.length-1); // Direct Selection Sorting // disOrderArray = selectionSort(disOrderArray); / / heap sort // disOrderArray = heapSort(disOrderArray); // Direct insertion sort // disOrderArray = InsertSort(disOrderArray); // Half Insert Sort (Binary Search Sort) // disOrderArray = BInsertSort(disOrderArray); // Hill sort // disOrderArray = ShellInsert(disOrderArray); // Merge Sort // disOrderArray = mergeSort(disOrderArray, 0, disOrderArray.length-1); // cardinal sorting int[] disOrderArray = {3,2,3,2,5,333,45566,2345678,78,990,12,432,56}; disOrderArray = RadixSort(disOrderArray, 10, 7); for(int i=0;i<disOrderArray.length;i++) { System.out.print(disOrderArray[i]+" "); } }
The basic sorting algorithms for data structures are almost complete.
Add a binary search algorithm: similar to the half-fold search algorithm
Time complexity: O(logn)
- /**
- * Two points search
- * @param arr
- * @param searchnum Elements to be looked up
- * @return
- */
- public static int BSearch(int[] arr, int searchnum){
- int low = 0;
- int high = arr.length-1;
- while(low<=high)
- {
- int m = (low+high)/2;
- if(searchnum == arr[m])
- {
- return m;
- }
- else if(searchnum < arr[m])
- {
- high = m-1;
- }
- else
- {
- low = m+1;
- }
- }
- return -1;
- }
/** * Binary search * @param arr *@ Param search num element to be found * @return */ public static int BSearch(int[] arr, int searchnum){ int low = 0; int high = arr.length-1; while(low<=high) { int m = (low+high)/2; if(searchnum == arr[m]) { return m; } else if(searchnum < arr[m]) { high = m-1; } else { low = m+1; } } return -1; }
This paper refers to: http://blog.csdn.net/tan313/article/details/51146170