Solve the problem as follows:
1. Seek the 1+2!+3!+...+20! and.
2. Gets the maximum and minimum values in the array.
3. Select sort, bubble sort for array.
4. Find the location of a number in the array (binary lookup).
binary Find: for the lookup of the sequential table, the array is stored in sequential elements, by the following method:
Define the variable Low,mid,high to the lowest, middle, and highest-bit elements in the array, comparing the elements to find and the elements that the mid points to, and if larger than mid, continue looking between mid and high, low=mid+1;
Otherwise, continue searching between low and mid, high=mid-1. Until low is greater than high stop,
The element to find is returned as a label, and NULL is returned if not found. It is actually a comparison that can exclude half of the elements and then find them in the other half, until they find or fail.
Simple selection Sorting is the simplest and most intuitive algorithm, the basic idea for each trip from the data element to be sorted by selecting the smallest (or largest) element as the first element, until all the elements are finished, the simple selection of sorting is an unstable sort.
Bubble SortThe algorithm works as follows: (from back to forward)
- Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
- Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
After the introduction of the main idea followed by the code, because Java is still in the initial study, I hope you have a lot of advice!!!
Class Home{public static void Main (string[] args) {System.out.println ("Hello! This is the 1+2!+3!+...+20! summation result "); JC (20); System.out.println (); System.out.println (); SYSTEM.OUT.PRINTLN ("Hello! This is the result of getting the maximum and minimum values in the array "); int[] arr={2,1,3,5,9,2,7};int[] Arr1={3,2,5,8,4,15,14,1,25,6,3};maxmin (arr); System.out.println (); System.out.println (); SYSTEM.OUT.PRINTLN ("Hello! This is the result of selecting sorting "); XZPX (arr); System.out.println (); System.out.println (); SYSTEM.OUT.PRINTLN ("Hello! This is the result of the bubbling Sort "); MpPX (ARR1); System.out.println (); System.out.println (); SYSTEM.OUT.PRINTLN ("Hello! This is the result of a compromise choice of 1 "); zzpx (arr,30); System.out.println (); System.out.println (); SYSTEM.OUT.PRINTLN ("Hello! This is the result of a compromise choice of 2 "); zzpx (arr1,14);} public static void JC (int num) {int sum=0;for (int i=1;i<=num; i++) {int n=1;for (int j=1;j<=i;j++) {n=n*j;} Sum=sum+n;} System.out.println ("When n of the sum of the factorial is" +num+ "and" + "and" +sum ");} public static void Maxmin (int[] arr) {int max=arr[0]; int min=arr[0]; for (int i=0;i<arr.length; i++) {if (Max<arr[i]) {max=arr[i]; } if (Min>arr[i]) {min=arr[i]; } }System.out.print ("When array is"); for (int j=0;j<arr.length;j++) {System.out.print (arr[j]+ ","); } System.out.println ("Its maximum value is" +max+ "minimum value is" +min);} public static void Sz (int[] arr) {for (int j=0;j<arr.length;j++) {System.out.print (arr[j]+ ","); }}public static void Xzpx (int[] arr) {System.out.print ("unchanged array Before"), Sz (arr), for (int i=0;i<arr.length; i++) {for (int J =i+1;j<arr.length; j + +) {if (arr[i]>arr[j]) {int a=arr[j];arr[j]=arr[i];arr[i]=a;}}} System.out.print ("Array after change"); Sz (arr);} public static void MpPX (int[] arr) {sz (arr), for (int i=0;i<arr.length; i++) {//int b=i;for (int j=1;j<arr.length-i; j + +) {if (arr[j-1]>arr[j]) {int a=arr[j-1];arr[j-1]=arr[j];arr[j]=a;} b=b+1;}} System.out.print ("Array after change"); Sz (arr); } public static void Zzpx (int[] arr,int N) {System.out.print ("sequential array"); sz (arr); System.out.print ("Array length is" +arr.length); int min=0; int max=arr.length-1; int mid= (MIN+MAX)/2; Boolean a=false; while (Min<max) {if (N>arr[mid]) {min=mid+1; mid= (Min+max)/2;} else IF (N<arr[mid]) {max=mid-1; mid= (Min+max)/2;} else if (N==arr[mid]) {System.out.println ("When n equals" +n+ "whose index number is "+mid); a=true;break;}} if (a==false) {System.out.println ("When n equals" +n+ ", there is no number in that order array");}}}
The results of the implementation are as follows:
Java exercises (selection, bubble sort, binary lookup)