Question 1: Binary find
Binary lookup is in an ordered table, comparing the value of the data to be looked up with the median value of the lookup range, there are three cases:
1) to find the data value exactly equal to the intermediate element value, put back the index of the intermediate element value.
2) The data value to be found is smaller than the middle element value, then the first half of the entire look-up range is used as the new look-up range, and 1 is executed until an equal value is located.
3) The data value to be found is larger than the middle element value, then take the second half of the entire look-up range as the new lookup range, execute 1) until an equal value is found
4) If an equal value is not found at the end, the fallback unit 0 that does not store the data is returned.
The question to you is that the standard input is sorted in ascending order, using the binary lookup method to find a given integer value, the lookup is done by using the elements in the table to compare with the given element values, and you need to sequentially output the values of the elements that were used in the binary lookup process.
Input: Standard input, the first behavior of the input is a positive integer n, which indicates the number of elements that need to look up the table; the second behavior has n integers in ascending sequence, and two numbers are separated by a space; the third behavior requires the integer you are looking for.
Output: Standard output, the first line outputs the element values that were compared during the lookup process, separated by a space between two numbers. The output of the second row outputs the lookup results, if the lookup element is in the table, output the ordinal of the element (starting with 1), and if the lookup element is not in the table, output "NO".
Input Sample:
13
7 14 18 21 23 29 31 35 38 42 46 49 52
21st
Sample output:
31 18 23 21
4
Idea: Using recursion, it is relatively simple to achieve.
Import java.util.*;p Ublic class Main {public static void main (string[] args) {int n, m;int[] s; Scanner in = new Scanner (system.in); n = In.nextint (); s = new Int[n+1];for (int i = 1; I <= n; i++) s[i] = In.nextint (); m = In.nextint (); Find (1, N, M, s); In.close ();} public static void Find (int s, int e, int m, int[] st) {int index = (s + e)/2;if (St[index] < m) {System.out.printf (st[i Ndex] + ""); Find (Index+1, E, M, St);} else if (St[index] > m) {System.out.printf (St[index] + ""); Find (S, index-1, M, St);} else System.out.printf ("%d\n%d\n", St[index], index);}}
Question 2: Heap sorting
the idea of heap sequencing actually uses a complete binary tree stored in the array, by adjusting the complete binary tree into a large top heap to get a sorted table of the maximum value of the method, the large top heap satisfies the root node larger than the node of the subtree. Heap sorting is mainly through the root element of the big top heap and the last element of the unfinished sort to exchange, the exchange of the complete binary tree does not meet the requirements of the large top heap to meet the requirements of the adjustment by the following methods to complete:
void Heapadjust (int[] r,int s,int t), wherein the binary tree stored in the array R, only with R[s] as the root tree, the left and right sub-tree may not meet the large top heap characteristics.
The operation difficulty of adjusting the heap is the root tree node number is I, then the left subtree node number is 2*i, the right subtree node number is 2*i+1; by comparing the size of the subtree select the large subtree to adjust, always adjust to the roots node is larger than the child node, and then insert the value of the root node into the last adjusted node. To complete the heap sequencing, the initial heap can be obtained by adjusting from the bottom of the heap to the top of the heap, and then the order is completed by N-1, and the control flow is:
void Heapsort (int[] R) {
int i;
int n=r.length-1;
For (i=n/2;i>0;i--) {
Heapadjust (r,i,n);
}
For (i=n;i>1;i--) {
r[0]=r[1];r[1]=r[i];r[i]=r[0];
Heapadjust (r,1,i-1);
}
}
The problem for you is that the n integers of the standard input are sorted by the heap, and the initial heap that is built is displayed and the data is sorted.
input: Standard input, input the number of the first behavior integer n value, the second behavior n integers, each integer is a space.
output: Standard output, the first line sequentially outputs the stored value of the initial heap built in the array, separated by a space between each output data, and the second row outputs the sorted sequence, separated by a space between each output data.
Input Sample:
-
The 7 in each of the above
Sample output:
The 7 -A-.-
7 For each of the more than
Import Java.util.scanner;public class Main {public static void Main (String args[]) {Scanner in=new Scanner (system.in int n = in.nextint (); int[] array = new Int[n + 1];for (int i = 1; I <= n; i++) Array[i] = In.nextint (); adjust (array);p R an int (array); Heapsort (array);p rint (array); In.close ();} Output public static void print (int[] list) {System.out.print (list[1]), for (int i = 2; i < list.length; i++) {SYSTEM.O Ut.print ("" + list[i]); } System.out.println ();} Exchange public static void Swap (int[] heap, int a, int b) {int temp = Heap[a];heap[a] = heap[b];heap[b] = temp;} By definition, the first half of the tree node is having children, so start from here to adjust public static void adjust (int[] heap) {for (int i = HEAP.LENGTH/2; i > 0; i--) Adjust (hea P, I, heap.length-1); } public static void adjust (int[] heap,int I, int. n) {int child;while (i <= n/2) {child = i * 2;if (child + 1 <= N && Heap[child] < Heap[child + 1]) Child + = 1;//to point to children with larger values if (Heap[i] < Heap[child]) {Swap (heap, I, child); After swapping, a subtree that is rooted in child does not necessarily meetHeap definition, so start adjusting i = child from child;} else break;}} Sorts a maximum heap heap public static void Heapsort (int[] heap) {for (int i = heap.length-1; i > 0; i--) {//Put the root node with the last Element Exchange position, adjust the remaining n-1 nodes, swap (heap,1, i); adjust (heap,1, i-1); } }
Question 3: Quick sort
The core operation of fast sorting is to divide the original sort table into two parts through a data, the previous part is smaller than the number, the data is larger or equal than the data, the data is the sorted position, that is, the data is finished sorting. If you define a sort table, the partitioning method is:int partition (int[] r,int low,int High); wherein low,high means to divide the low data of data R into high data, Return to an integer after dividing to pivot record position; defines the completion partitioning method, complete the quick sort: void QuickSort (int[] R, by calling the following int S,int t) { if (s<t) { i=partition (r,s,t); QuickSort (r,s,i-1); QuickSort (r,i+1,t); } } recommends that each partition select the first element to program the pivot record. To you the problem is that the n integers of the standard input are sorted in a quick order and need to show the pivot record used for each partition and complete the sorting of the data. Input: Standard input, input the number of the first behavior integer n value, the second behavior n integers, each integer is a space. Output: Standard output, the first line of the output outputs the pivot record used during the sorting process, separated by a space between each output data, and the second row outputs the sorted sequence, separated by a space between each output data. Input Sample: 1439 80 76 41 13 29 50 78 30 11 100 7 41 86 Output Example: 39 7 11 30 13 50 41 76 100 86 78 7 11 1329 30 39 41 41 50 76 78 80 86 100
Import java.util.*;p Ublic class Main {//used to save the pivot record used by private static list<integer> ans;public static void Main (String [] args) {ans = new arraylist<integer> (); Scanner in = new Scanner (system.in), int n = in.nextint (), int[] A = new Int[n + 1];for (int i = 1; I <= n; i++) {A[i] = in . Nextint ();} QuickSort (A, 1, n);//output uses pivot record Print_ans ();//output sorted value print (a); In.close ();} public static void QuickSort (int[] R, int s, int t) {if (S < t) {int i = partition (R, S, t); Ans.add (R[i]); QuickSort (R, S, i-1); QuickSort (R, i+1, T); }}public static int partition (int[] R, int s, int t) {int i = s, j = t;int x = R[s];while (i < J) {//right-to-left for a number less than x to fill r[i] whi Le (i < J && R[j] >= x) j--;if (i < J) {R[i] = r[j];i++;} To fill R[j]while (i < J && R[i] < x) I++;if (i < J) {R[j] = r[i];j--;}} from left to right for a number greater than or equal to X R[i] = X;return i;} public static void print (int[] R) {System.out.print (r[1]), for (int i = 2; i < r.length; i++) {System.out.print ("" + r[i]) ;} System.out.println ();} public static void Print_ans () {if (!ans.isempty ()) {System.out.print (ans.get (0)); for (int i = 1; i < ans.size (); i++) {System.out.print (" "+ ans.get (i));} System.out.println ();}}}
Question 4: Hill sort
The idea of hill sorting is to first select an integer di (called step, usually less than n) that is less than the number of sorted data, a set of interval di, a direct insertion of each set of elements, and a sort of data to be inserted into the sequence. When the step is 1 o'clock, complete the sorting of the entire data. The process for sorting is:
1, according to the number of steps, for each step to group;
2, for each group to insert the sort, the main operation is as follows:
1) If there is no data in order, the data is stored in a temporary traverse r[0];
2) Move the data in front of him that is older than he is one bit backwards;
3) Insert the data of r[0] into the last move to the data position;
To you the problem is that the n integers of the standard input are sorted by hill, the step takes 5,3,1, and the number that needs to be inserted is displayed each time, and the data is sorted.
Input: Standard input, input the number of the first behavior integer n value, the second behavior n integers, each integer is a space.
Output: Standard output, output the first line in turn outputs the number that needs to be inserted during the sorting process, separated by a space between each output data, and the second row outputs the sorted sequence, separated by a space between each output data.
Input Sample:
14
39 80 76 41 13 29 50 78 30 11 100 7 41 86
Sample output:
29 50 30 11 7 41 39 13 86 7 29 11 30 41 50 80 78
7 11 13 29 30 39 41 41 50 76 78 80 86 100
Import java.util.*;p Ublic class Main {//is used to save the number of private static list<integer> ans;public static void Main ( String[] args) {ans = new arraylist<integer> (); Scanner in = new Scanner (system.in), int n = in.nextint (), int[] A = new Int[n];for (int i = 0; i < n; i++) {A[i] = In.next Int ();} Shellsort (a);//output each time the number of Print_ans () required to be inserted,//output sorted results print (a); In.close ();} public static void Shellsort (int[] data) {int j = 0; int temp = 0; for (int increment = 5; increment > 0; increment-= 2) {for (int i = increment; i < data.length; i++) {temp = da Ta[i]; Boolean flag = true;for (j = i; J >= increment; J-= increment) {if (temp < data[j-increment]) {Data[j] = Data[j -increment]; if (flag) {Ans.add (temp); flag = false;}} else break; }DATA[J] = temp;} }} public static void print (int[] R) {System.out.print (r[0]), for (int i = 1; i < r.length; i++) {System.out.print ("" + R[i]);} System.out.println ();} public static void Print_ans () {if (!ans.isempty ()) {System.out.print (Ans.get (0)); for (int i = 1; i < ans.size (); i++) {System.out.print ("" + ans.get (i));} System.out.println ();}}}
Java sorting algorithm binary Heap Hill quick Finishing