# Common method 2 for operating Arrays-sorting and searching

Source: Internet
Author: User
`/*** Common methods for operating arrays */public class arraydemo {public static void main (string [] ARGs) {int [] arr = new int [] {1, 3, 10, 2, 5, 7, 8}; // system before sorting. out. println ("-------------------- before sorting ------------------"); printarray (ARR); // select sorting // selectsort (ARR); // Bubble Sorting bubblesort (ARR); system. out. println ("-------------------- after sorting --------------------"); printarray (ARR); // common search method system. out. println (arrayindexof (ARR, 10); // second Method 1 system. out. println (searchelindex (ARR, 5); // Binary Search Method 2system. out. println (searchelindex2 (ARR, 5);}/*** select sorting (compare the first element with each element in the array in each round) * @ Param ARR The array to be sorted */public static void selectsort (INT [] ARR) {for (INT I = 0; I <arr. length-1; I ++) {for (Int J = I + 1; j <arr. length; j ++) {If (ARR [I]> arr [J]) {/** method of changing the position of an element 1: use the Temporary Variable * // * int temp = arr [I]; arr [I] = arr [J]; arr [J] = temp; * // ** method 2, the two Number to perform addition and subtraction. * For example: int [] arr = {2, 1}; * arr [0] = arr [0] + arr [1]; =, 3 = 2 + 1; * arr [1] = arr [0]-Arr [1]; =, 2 = 3-1; * arr [0] = arr [0]-Arr [J]; = "1 = 3-2; * // * arr [I] = arr [I] + arr [J]; arr [J] = arr [I]-Arr [J]; arr [I] = arr [I]-Arr [J]; */swap (ARR, I, j );}}}} /*** bubble sort (compare two adjacent numbers) * @ Param arr array to be sorted */public static void bubblesort (INT [] ARR) {for (INT I = 0; I <arr. length-1; I ++) {// control the number of comparisons for (Int J = 0; j <ar R. length-I-1; j ++) {//-I: reduces the number of elements compared to-1: To avoid throwing arrayindexoutofboundsexceptionif (ARR [J]> arr [J + 1]) {/* int temp = arr [J]; arr [J] = arr [J + 1]; arr [J + 1] = temp; */swap (ARR, J, J + 1 );}}}} /*** swap two elements in the array * @ Param arr array * @ Param index1 element subscript 1 * @ Param index2 element subscript 2 */public static void swap (INT [] arr, int index1, int index2) {int temp = arr [index1]; arr [index1] = arr [index2]; arr [index2] = te MP;}/*** returns the subscript of an element in the array by using the * @ Param arr array * @ Param key * @ return returns the subscript of the returned element in the array, -1 */public static int searchelindex (INT [] arr, int key) {int min, Max, mid; min = 0; max = arr. length-1; Mid = (min + max)/2; while (ARR [Mid]! = Key) {If (Key> arr [Mid]) {min = Mid + 1;} else if (Key <arr [Mid]) {max = mid-1 ;} if (min> MAX) Return-1; Mid = (min + max)/2;} return mid ;} /*** returns the subscripts of an element in the array by using the * @ Param arr array * @ Param key the element to be searched * @ return returns the subscripts of the returned element in the array, -1 */public static int searchelindex2 (INT [] arr, int key) {int min, Max, mid; min = 0; max = arr. length-1; while (Min <= max) {mid = (min + max)> 1; if (Key> arr [Mid]) {min = Mid + 1 ;} else if (Key <arr [Mid]) {max = mid-1 ;}else {return mid ;}} return-1 ;} /*** find the position of an element in the array, this function can find the subscript * @ Param arr searches for the array * @ Param key searches for the element * @ Param key searches for unsorted array first element * @ return if found, this element is returned in the array subscript in, -1 */public static int arrayindexof (INT [] arr, int key) {for (INT I = 0; I <arr. length; I ++) {If (ARR [I] = Key) return I;} return-1 ;} /*** print array * @ Param ARR The array to be printed */public static void printarray (INT [] ARR) {system. out. print ("["); For (INT I = 0; I <arr. length; I ++) {if (I <arr. length-1) {system. out. print (ARR [I] + ",");} else {system. out. print (ARR [I]) ;}} system. out. println ("]") ;}}`

Related Keywords:

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.