# Integer array. The maximum value is exchanged with the first value of the array. The minimum value is exchanged with the last value.

Source: Internet
Author: User

RT: an integer array. The maximum value is exchanged with the first value of the array. Two days before the minimum value is exchanged with the last value, it is interesting to see a question on the Internet. It is easy to be neglected. OK, the Code is as follows: [java] package org. wxp. sort; import junit. framework. testCase;/*** an integer array that exchanges the maximum value with the first value of the array, the minimum value is exchanged with the last value of the array to find the maximum and minimum values in the loop and Their subscript in the array. data is exchanged according to the table below. * Key: if the maximum and the first values are exchanged first, if the first value in the array is the minimum value, the subscript of the minimum value has changed when the minimum value is exchanged, * In this case, the subscript of the maximum value must be paid to the subscript of the minimum value before normal exchange. And vice versa; ** @ author Champion. wong **/public class Sort_01 extends TestCase {private int [] arr = {1, 3, 5, 6, 4, 9, 7 }; // define an array public void test () {int max = 0, min = 0; // maximum value, minimum value int i1 = 0, i2 = 0; // The following table lists the maximum values in the array. The minimum value is the subscript int temp = 0 in the array; // used for data exchange max = min = arr ; // The default maximum and minimum values are the first values of the array for (int I = 1; I <arr. length; I ++) {if (max <arr [I]) {// obtain the maximum and maximum subscript in the array max = arr [I]; i1 = I ;} if (min> arr [I]) {min = arr [I]; i2 = I ;}} // swap the maximum value with the first value of the array temp = arr ; arr  = arr [i1]; arr [i1] = temp; /** if the first value of the array is the minimum value, the position has been moved during the exchange process. The current minimum value is the position of the maximum value, that is, the position of the first moving value */if (min = arr [i1]) {i2 = i1 ;} // swap the minimum value with the last value of the array temp = arr [arr. length-1]; arr [arr. length-1] = arr [i2]; arr [i2] = temp; System. out. println (max + "=" + min); System. out. println (i1 + "=" + i2); for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I] + "") ;}} the output result of the console is as follows:

Related Keywords:
Related Article

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.