数组反转
class Reversal{
public static void main(String args[]){
int[] data = new int[] {1,2,3,4,5,6,7,8,9};
data = reverse(data);
printArray(data);
}
public static int[] reverse(int[] arr){
int center = arr.length/2;
int tail = arr.length-1;
int head = 0;
for(int x =0; x<center;x++){
int temp = arr[head];
arr[head] = arr[tail];
arr[head] = temp;
head++;
tail--;
}
return arr;
}
public static void printArray(int[] temp){
for(int z= 0;z<temp.length;z++){
System.out.print(temp[z]+"、");
}
}
}
二维数组反转
class Reversal{
public static void main(String args[]){
int[][] data = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
data = reverse(data);
printArray(data);
}
public static int[][] reverse(int[][] arr){
for(int x = 0;x<arr.length;x++){
for(int y = x;y<arr[x].length;y++){
if(x!=y){
int temp = arr[x][y];
arr[x][y] = arr[y][x];
arr[y][x] = temp;
}
}
}
return arr;
}
public static void printArray(int[][] pa){
for(int z = 0;z<pa.length;z++){
for(int u = 0;u<pa[z].length;u++){
System.out.print(pa[z][u]+"、");
}
System.out.print("\n");
}
}
}
-------------------------------------------------
数组的二分法
class Dichotomy{
public static void main(String args[]){
int[] arr = new int[]{1,3,5,7,9};
int from = 0;
int to = arr.length;
int temp = 7;
System.out.print(binarySearch(arr,from,to,temp));
}
public static int binarySearch(int arr[],int from,int to,int key){
int mid = (from+to)/2;
if(mid == key){
return mid;
}else if(key<arr[mid]){
return binarySearch(arr,from,mid-1,key);
}else if(key>arr[mid]){
return binarySearch(arr,mid+1,to,key);
}else{
return -1;
}
}
}
class Reversal{
public static void main(String args[]){
int[] data = new int[] {1,2,3,4,5,6,7,8,9};
data = reverse(data);
printArray(data);
}
public static int[] reverse(int[] arr){
int center = arr.length/2;
int tail = arr.length-1;
int head = 0;
for(int x =0; x<center;x++){
int temp = arr[head];
arr[head] = arr[tail];
arr[head] = temp;
head++;
tail--;
}
return arr;
}
public static void printArray(int[] temp){
for(int z= 0;z<temp.length;z++){
System.out.print(temp[z]+"、");
}
}
}
二维数组反转
class Reversal{
public static void main(String args[]){
int[][] data = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
data = reverse(data);
printArray(data);
}
public static int[][] reverse(int[][] arr){
for(int x = 0;x<arr.length;x++){
for(int y = x;y<arr[x].length;y++){
if(x!=y){
int temp = arr[x][y];
arr[x][y] = arr[y][x];
arr[y][x] = temp;
}
}
}
return arr;
}
public static void printArray(int[][] pa){
for(int z = 0;z<pa.length;z++){
for(int u = 0;u<pa[z].length;u++){
System.out.print(pa[z][u]+"、");
}
System.out.print("\n");
}
}
}
-------------------------------------------------
数组的二分法
class Dichotomy{
public static void main(String args[]){
int[] arr = new int[]{1,3,5,7,9};
int from = 0;
int to = arr.length;
int temp = 7;
System.out.print(binarySearch(arr,from,to,temp));
}
public static int binarySearch(int arr[],int from,int to,int key){
int mid = (from+to)/2;
if(mid == key){
return mid;
}else if(key<arr[mid]){
return binarySearch(arr,from,mid-1,key);
}else if(key>arr[mid]){
return binarySearch(arr,mid+1,to,key);
}else{
return -1;
}
}
}