Common Operations on arrays and common operations on Arrays
Traversal
Method 1:
Public static void printArray (int [] arr) {for (int x = 0; x <arr. length; x ++) {// arr. length to obtain the length of the value System. out. println (arr [x]);}
Method 2:
public static void printArray(int[] arr) { System.out.print("["); for(int x=0; x<arr.length; x++) { if(x == arr.length-1) { System.out.println(arr[x] + "]"); } else { System.out.println(arr[x] + ","); } } }
Maximum Value
Maximum Value
public static int getMax(int[] arr) { int max = arr[0]; for (int x=1; x<arr.length; x++) { if (arr[x] > max); max = arr[x]; } return max; }
Minimum value
public static int getMin(int[] arr) { int min = arr[0]; for (int x=1; x<arr.length; x++) { if (arr[x] < min); min = arr[x]; } return min; }
Reverse Order
Method 1:
public static int reverse (int[] arr) { for (int x=0; x<arr.length/2; x++) { int temp = arr[x]; arr[x] = arr[arr.length-1-x]; arr[arr.length-1-x] = temp; } }
Method 2:
public static int reverse(int[] arr) { for (int start=0,end=arr.length-1; start<=end; start++,end--) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; } }
Query table
public static String getString(String[] strArray,int index) { return strArray[index]; }
Basic Search (unordered array)
Method 1:
public static int getIndex(int[], int value) { for (int x=0; x<arr.length; x++) if(arr[x] == value) { return x; } return -1; }
Method 2:
public static getIndex(int[] arr, int value) { int index = -1; for(int x=0; x<arr.length; x++) if(arr[x] = value) { index = x; break; } return index; }
Binary Search
Prerequisites: Elements in the array must be ordered.
public static int halfSeach_2(int[] arr,int key){ int min,max,mid; min = 0; max = arr.length-1; mid = (max+min)>>1; //(max+min)/2; while(arr[mid]!=key){ if(key>arr[mid]){ min = mid + 1; } else if(key<arr[mid]) max = mid - 1; if(max<min) return -1; mid = (max+min)>>1; } return mid; }
Sort
Bubble Sorting
The adjacent elements are compared to each other, and the values are placed later. After the first completion, the maximum value appears at the maximum index. Similarly, other elements can be arranged.
public static void bubbleSort(int[] arr) { for(int x=0; x<arr.length-1; x++) { for(int y=0; y<arr.length-1-x; y++) { if(arr[y] > arr[y+1]) { int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; } } } }
Select sort
Compare the zero index element with the element after Index 1. After the first completion, the minimum value is 0. Similarly, other elements can be arranged.
public static void selectSort(int[] arr) { for(int x=0; x<arr.length-1; x++) { for(int y=x+1; y<arr.length; y++) { if(arr[x] > arr[y]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } }
Quick sorting
The idea of fast sorting is the idea of divide governance. The main principle is to divide the array into A [p .. q-1] And A [q + 1 .. r], and then adjust the element to make A [p .. q-1] less than or equal to q, also less than or equal to A [q + 1 .. r]. Then, the sorting is completed at the end of the recursion process.
For more algorithm tutorials, see:
Http://ahalei.blog.51cto.com/