# Array algorithm set

Posted by jimmygee on Mon, 29 Jun 2020 05:58:50 +0200

1. Bubble sorting: average time complexity (O(n^2))
① Compare adjacent elements. If the first is bigger than the second, exchange them.
② Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the maximum number.
③ Repeat the above steps for all elements except the last one.
④ Continue to repeat the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.

```package Demo4;
public class ButtbbleSort {
public static void main(String[] args) {
int[] arr={9,8,10,7,6,0,11};
int count=0;
//Method 1:
for (int i =arr.length-1 ; i >0; i--) {
for (int j = 0; j <i ; j++) {
count++;
if (arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]= temp;
}
}
}
//Method 2:
for(int i =0 ; i<arr.length-1 ; i++) {
for(int j=0 ; j<arr.length-1-i ; j++) {
count++;
if(arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("Number of comparisons:"+count);
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);
}
}
}
```

2. Sorting by selection: select the smallest (or largest) element from the data elements to be sorted for the first time, and store it at the beginning of the sequence, and then find the smallest (largest) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on until the number of all data elements to be sorted is zero. Selection sorting is an unstable sorting method. It is more efficient than bubble sorting.

```package Demo4;

public class SelectSort {
public static void main(String[] args) {
int [] arr={9,8,10,7,6,0,11};
int count=0;
int count1=0;
for (int i = 0; i <arr.length-1 ; i++) {
//            System.out.println(i);
int min=i;
for (int j = i+1; j <arr.length; j++) {
count++;
if (arr[j]<arr[min]){
min=j;
}
}
if (min !=i){
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
count1++;
}
}
System.out.println("Comparison times:"+count);
System.out.println("Number of exchanges:"+count1);
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);

}
}
}

```

3. Dichotomy (half search) method: ① search one by one until you find it. ② Dichotomy search, high efficiency.
Algorithm requirements:
1. Sequential storage structure must be adopted.
2. The keywords must be arranged in order.
Method 1:

```package Demo4;

public class ArraySearch {
public static void main(String[] args) {
int[] arr={9,8,10,7,6,0,11};

int index=arraySearch(arr,11);
System.out.println(index==-1 ? "The element does not exist":"The subscript of this element is:"+index);

}

public static int arraySearch(int[] arr, int ele) {
for (int i = 0; i <arr.length ; i++) {
if (ele==arr[i]){
return i;
}
}
return -1;
}
}

```

Method 2:

```package Demo4;

public class ArrayUnit {
public static void main(String[] args) {

int[] arr = {100,200,222,230,600,777,888,1000,2000,9999,};

int index = binarySearch(arr,666);
System.out.println(index == -1 ? "The element does not exist!" : "The subscript of this element is:" + index);
}

public static int binarySearch(int[] arr, int dest) {
int begin = 0;
int end = arr.length - 1;

while (begin <= end) {
int mid = (begin + end) / 2;
if (arr[mid] == dest) {
return mid;
} else if (arr[mid] < dest) {
begin = mid + 1;
} else {
end = mid - 1;
}
}
return -1;

}

}

```