Section III Java Array (loop through, get the maximum value of the array (max and min), select Sort, bubble sort, practice console output uppercase a)

Source: Internet
Author: User

Gets the maximum value (max and min) of the array

Ideas: 1, to obtain the maximum value needs to be compared , each comparison will have a larger value, because the value is not determined, a variable is required for the pro-storage .

2. Let each element in the array be compared to the value in this variable, and if it is greater than the value in the variable, use that variable to record a larger value.

3, when all the elements are relatively complete, then the variable is stored in the array of the maximum value

Example://Maximum Value

int arr[] = {1,2,3,4,5,};

int max = arr[0];

for (int a=0;a<arr.length;a++) {

{if (Arr[a]) >arr[max]{

Mac=arr[a];}

}

System.out.println ("The array arr maximum is:" +max), the same as the minimum value.

Select sort

Select sort: At the beginning the default angle is 0 The number is the smallest number, followed by the number of comparisons, found small record subscript, the most
After the angle marked as 0 of the number and the last corner of the number of exchange, the second time from the angle of 1 of the number began to traverse;

int arr[] = {1,2,3,4,5};ForInti =0; i < arr.length-1;i++) {ForIntA = i+ 1; a<arr.length;a++) { if (Arr[i] > Arr[a]) { int temp = Arr[a]; Arr[a] = Arr[i]; Arr[i] = Temp ; }}}system.out. Print ("{"); For (int b = 0;b < arr.length;b++) { if (b! = arr.length-1) {System.out.  Print (arr[b]+",");} else{System.out.  Print (arr[b]+"}"); } } 
Bubble sort

Bubble sort: By 22 comparison, the resulting maximum or minimum value of the comparison is placed in the last face of the array, and then loop in turn
The first time to take the maximum or minimum value, enter the second cycle, the second cycle is also 22 comparison, because the
At the time of the loop, the value we get is compared with the previous value, so the second loop
It is not necessary to compare the value of the previous comparison so that our second for loop
The face will have a. length-i-1;

Int[] arr = {3,2,4,7,6,5,9,8,10,20,1};
for (int a = 0;a<arr.length-1;a++) {
for (int b = 0;b<arr.length-1-i;b++) {
if (Arr[a]>arr[b+1]) {
int temp = Arr.[a];
Arr.[a] = arr.[b];
ARR.[B] = temp;
}
}

练习:

Console with * output uppercase A;

for (int a = 1;a<=5;a++) {
if (a==1) {//When first row, we only output 5 spaces and one * end
1. Circular output spaces;
for (int i = 5;i>a;i--) {//Outputs 5 spaces for a loop, reducing this to 1;
Systen.out.print (""); The loop output space is not bhakta;
} Systen.out.println ("*"); The first line ends with a newline so use println;
} else{
for (int i = 5;i>a;i--) {//Outputs 5 spaces for a loop, reducing this to 1;
Systen.out.print (""); The loop output space is not bhakta;
Systen.out.println ("*"); Then the output space, plus *;
if (a==5/2+5%2) {//is taken to the middle value;
for (int b =1;b<=2* (A-1) -1;b++) {
Systen.out.print (""); The loop output space is not wrapped;
} Systen.out.println ("*"); After the space cycle ends, the output "*" is bhakta;
} else {//Space law is * (A-1)-1;
for (int b =1;b<=2* (A-1) -1;b++) {
Systen.out.print (""); The loop output space is not wrapped;
} Systen.out.println ("*"); After the space cycle is finished, output *, then Bhakta
}
}

 

Section III Java Array (loop through, get the maximum value of the array (max and min), select Sort, bubble sort, practice console output uppercase a)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.