数组中的常见算法

查找

1、二分查找

 1 package package1_Array;
 2 /**
 3  * 二分查找
 4  * 前提:有序数组
 5  * 时间复杂度:O(log(n))
 6  */
 7 public class array_exe8 {
 8     public static void main(String[] args) {
 9         int[] arr = new int[]{-99,-54,-2,0,2,33,43,256,999};////arr必须有序
10         System.out.println(BinarySearch(arr,-2));
11 
12     }
13     public static int BinarySearch(int[] arr,int num){//在有序数组中查找num的索引
14         boolean Flag = false;
15         int low = 0,high = arr.length-1;
16         while(low <= high){
17             int mid = (high + low)/2;
18             if(arr[mid] < num){
19                 low = mid+1;
20             }else if(arr[mid] > num){
21                 high = mid-1;
22             }else{
23                 return mid;
24             }
25         }
26         return -1;//找不到指定的元素则返回-1
27     }
28 }
View Code

排序

基本概念

稳定与不稳定;内部排序(待排序的序列完全放在内存中进行排序)和外部排序(排序过程需访问外存);

排序算法的选择

  1. 若n较小(如n≤50),可采用 直接插入或 直接选择排序。
  2. 当记录规模较小时,直接插入排序较好;否则因为直接选择移动的记录数少于直接插入,应选直接选择排序为宜。
  3. 若文件初始状态基本有序(指正序),则应选用 直接插 入、 冒泡或随机的 快速排序为宜;
  4. 若n较大,则应采用时间复杂度为O(nlgn)的排序方法: 快速排序、 堆排序或归并排序。

1、快速排序

 1 package package1_Array.array_Sort;
 2 import java.util.Arrays;
 3 
 4 //快速排序
 5 //思想:分而治之
 6 //假设我们现在需要对数组做增序排序
 7 public class QuickSort {
 8     public static void main(String[] args) {
 9         int arr[]=new int[]{3,1,-9,12,54,78,0,100,-10};
10         QS(arr,0,arr.length-1);
11         System.out.println(Arrays.toString(arr));//[-10, -9, 0, 1, 3, 12, 54, 78, 100]
12     }
13 
14     public static void QS(int[] arr,int s,int t){
15         if(s<t){//类似树的前序遍历
16             int i=Partition(arr,s,t);
17             QS(arr,s,i-1);
18             QS(arr,i+1,t);
19         }
20     }
21 
22     public  static int Partition(int arr[],int low,int high){
23         int temp = arr[low];//每次选择下标为low的元素作为划分的基准
24         while(low < high){
25             while(low < high && arr[high] > temp){//先移动high,从右往左找比temp小的数组元素的下标
26                 high--;
27             }
28             if(low < high){
29                 arr[low] = arr[high];//如果没有越界,则做填充,换low移动
30                 low++;
31             }
32             while(low < high && arr[low] < temp){//移动low,从左往右找比temp大的数组元素的下标
33                 low++;
34             }
35             if(low < high){//如果没有越界,则做填充,换high移动
36                 arr[high] = arr[low];
37                 high--;
38             }
39         }
40         arr[low] = temp;//把temp填充到此次划分的位置
41         return low;//返回划分的位置下标
42     }
43 }
View Code

2、堆排序(不稳定)

 java中内置了堆排序,使用优先队列实现的。

平均时间复杂度:O(n*log(n))

 1 package daily_code;
 2 
 3 import org.junit.Test;
 4 import java.util.PriorityQueue;
 5 //import java.util.Set;
 6 
 7 public class heapTest {
 8     //测试java中定义的堆结构-默认是建立小顶堆
 9     @Test
10     public void testHeap1(){
11         int[] arr = new int[]{4,5,1,6,2,7,3,8};
12         PriorityQueue<Integer> heap = new PriorityQueue();
13         for(int item: arr) {
14             heap.add(item);
15         }
16 
17         while(! heap.isEmpty()){
18             System.out.print(heap.poll());
19         }//12345678
20     }
21 
22     //测试java中定义的堆结构-使用PriorityQueue实现大顶堆
23     @Test
24     public void testHeap2(){
25         //方式1
26 //        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator(){
27 //            public int compare(Object o1,Object o2){
28 //                return (int)o2 - (int)o1;
29 //            }
30 //        });
31 
32         PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1,o2) -> (o2-o1));//方式2
33 
34         int[] arr = new int[]{4,5,1,6,2,7,3,8};
35         for(int item: arr) {
36             maxHeap.add(item);
37         }
38 
39         while(! maxHeap.isEmpty()){
40             System.out.print(maxHeap.poll());//87654321
41         }
42     }
43 }
View Code

 数组工具类的常见用法

 1 package package1_Array;
 2 
 3 import java.util.Arrays;
 4 
 5 public class array_tools {
 6     public static void main(String[] args) {
 7         //1.boolean equals(int[] a,int[] b) 判断两个数组是否相等
 8         int[] arr1 = new int[]{1,2,3,4};
 9         int[] arr2 = new int[]{1,4,2,3};
10         int[] arr3 = new int[]{1,2,3,4};
11         int[] arr4 = arr1;
12         System.out.println(Arrays.equals(arr1,arr2));//false
13         System.out.println(Arrays.equals(arr1,arr3));//true
14         System.out.println(Arrays.equals(arr1,arr4));//true
15 
16         //2.String toString(int[] a) 输出数组信息。
17         System.out.println(Arrays.toString(arr1));//[1, 2, 3, 4]
18 
19         //3.void fill(int[] a,int val) 将指定值填充到数组之中
20         Arrays.fill(arr1,1);
21         System.out.println(Arrays.toString(arr1));//[1, 1, 1, 1]
22 
23         //4.void sort(int[] a) 对数组进行排序
24         Arrays.sort(arr2);
25         System.out.println(Arrays.toString(arr2));//[1, 2, 3, 4]
26 
27         //5.int binarySearch(int[] a,int key) 对排序后的数组进行二分法检索指定的值。
28         int[] arr5 = new int[]{5,900,1,0,77,30,64,700};
29         Arrays.sort(arr5);
30         System.out.println(Arrays.toString(arr5));//[0, 1, 5, 30, 64, 77, 700, 900]
31         int index = Arrays.binarySearch(arr5,64);
32         System.out.println(index);//4
33     }
34 }
View Code



原文地址:https://www.cnblogs.com/xuechengmeigui/p/14610370.html