# 关于数组的算法

1.将一个正方形数组顺时针旋转90°。

```package algorithm;
/**
*
* @author hasee
*
*/
public class RotageOrderPrint {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr= new int[][] {
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}
};
rotag(arr);
}

public static void rotag(int[][] arr) {
int a = 0;
int b = 0;
int c = arr.length - 1;
int d = arr[0].length - 1;
while(a<c) {
rotaEdge(arr,a++,b++,c--,d--);
}
rotaPrint(arr);

}

private static void rotaPrint(int[][] arr) {
// TODO Auto-generated method stub
for(int i = 0;i < arr.length;i++) {
for(int j = 0;j < arr[0].length;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}

public static void rotaEdge(int[][] arr, int a, int b, int c, int d) {
// TODO Auto-generated method stub
int times = c - a;
int tmp = 0;
for(int i = 0;i < times;i++) {
tmp = arr[a][b+i];
arr[a][b+i] = arr[c-i][b];
arr[c-i][b] = arr[c][d-i];
arr[c][d-i] = arr[a+i][d];
arr[a+i][d] = tmp;
}

}

}```

2.循环打印一个数组 从最外圈开始，直到最里圈。

```package algorithm;
/**
* 循环打印一个数组
* @author hasee
*
*/
public class sprialOrderPrint {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = new int[][] {{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
{17,18,19,20}

};
sqrialOrder(arr);
}

public static void sqrialOrder(int[][] arr) {
int LB = 0;
int LE = 0;
int RB = arr.length-1;
int RE = arr[0].length-1;
while(LB<=RB && LE<=RE) {
sqlrialPrint(arr,LB++,LE++,RB--,RE--);
}
}

private static void sqlrialPrint(int[][] arr, int lb, int le, int rb, int re) {
// TODO Auto-generated method stub
if(lb == rb) {
for(int i = le;i <= re;i++) {
System.out.print(arr[lb][i]+" ");
}
}else if(le == re) {
for(int i = lb;i<=rb;i++) {
System.out.print(arr[i][le]+" ");
}
}else {
int a =lb;
int b =le;
while(b < re) {
System.out.print(arr[lb][b++]+" ");
}
while(a < rb)
System.out.print(arr[a++][re]+" ");
while(b > le)
System.out.print(arr[rb][b--]+" ");
while(a > lb)
System.out.print(arr[a--][le]+" ");
}
}

}```

3.给定一个数组和一个数num，把小于num的书放在数组左边，大于num的书放在数组右边

```package algorithm;
/**
* 给定一个数组，和一个数num，把小于num的书放在数组左边，大于num的书放在数组右边
*
*/
public class test4 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {5,1,4,3,6,3,8,4,6,5};
convert(a,5);
for (int i : a) {
System.out.println(i);
}
}

public static void convert(int[]a ,int num) {
int l = -1;
int r = a.length;
int cur = 0;
while(cur<r) {
if(a[cur]<num) {
swap(a,++l,cur++);
}else if(a[cur]>num) {
swap(a,--r,cur);
}else {
cur++;
}
}

}

private static void swap(int[] a, int l, int cur) {
// TODO Auto-generated method stub
int temp = a[l];
a[l] = a[cur];
a[cur] = temp;
}

}```

4.使用归并算法求小和问题

```package algorithm;

/**
* 使用归并算法求小和问题
* @author hasee
*
*/
public class smallSum {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,2,3,4,5,6,1};
System.out.println(merge(a));
}
public static int merge(int[] a) {
if(a==null || a.length<2)
return 0;
return mergeSort(a,0,a.length-1);
}
private static int mergeSort(int[] a, int l, int r) {
// TODO Auto-generated method stub
if (l == r)
return 0;
int mid = l + (r - l) / 2;
return mergeSort(a, l, mid) + mergeSort(a, mid + 1, r) + merge(a, l, mid, r);
}
private static int merge(int[] a, int l, int mid, int r) {
// TODO Auto-generated method stub
int count = 0;
int p1 = l;
int p2 = mid + 1;
int i = 0;
int[] help = new int[r-l+1];
while (p1 <= mid && p2 <= r)
{
count += a[p1] < a[p2] ? (r - p2 + 1)*a[p1] : 0;
help[i++] = a[p1] < a[p2] ? a[p1++] : a[p2++];
}

return count;
}

}```

84 篇文章17 人订阅

0 条评论

## 相关文章

### Java 解惑：Comparable 和 Comparator 的区别

Java 中为我们提供了两种比较机制：Comparable 和 Comparator，他们之间有什么区别呢？今天来了解一下。

832

892

841

24410

3489

43311

### LINQ

select:提取要查询的数据                 where:筛选满足条件的元素

1021

1513

963

### .NET面试题系列[12] - C# 3.0 LINQ的准备工作

"为了使LINQ能够正常工作，代码必须简化到它要求的程度。" - Jon Skeet

1103