一、数组排序
1.1 冒泡排序
import java.util.Arrays;
public class BubbleSort2 {
public static void main(String[] args) {
int[] arr={12,23,45,87,1,54,8,11,12};
BubbleSortMain.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
class BubbleSortMain{
public static void sort(int[] arr){
for (int j = 0; j < arr.length-1; j++) {
for (int i = 0; i < arr.length-1-j; i++) {
if (arr[i]>arr[i+1]){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
}
}
1.2 插入排序
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] arr={12,45,8,788,456,132};
InsertSortMain.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
class InsertSortMain{
public static void sort(int[] arr) {
///控制次数
for (int i = 1; i < arr.length; i++) {
//当前元素小于我的前一个元素得交换
int j=i;
while (j>0&&arr[j]<arr[j-1]){
int temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
j--;
}
}
}
}
1.3 快速排序
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr={12,45,78,11,54,62,48};
QuickSortMain.quicksort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
class QuickSortMain{
public static void quicksort(int[] arr,int start,int end){
if (start<end){
int index=getIndex(arr,start,end);
quicksort(arr,start,index-1);
quicksort(arr,index+1,end);
}
}
private static int getIndex(int[] arr, int start, int end) {
int i=start;
int j=end;
int x=arr[i];
while (i<j){
//从后往前找数,大的不变,小的进行转换
while (i<j&&arr[j]>=x){
j--;
}
if (i<j){
arr[i]=arr[j];
i++;
}
//从前往后找,小的不变,大的进行转换
while (i<j&&arr[i]<x){
i++;
}
if (i<j){
arr[j]=arr[i];
j--;
}
}
arr[i]=x;
return i;
}
}
1.4 选择排序
import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] arr={123,434,25,253,767,345,23};
SelectionSortMian.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
class SelectionSortMian{
public static void sort(int[] arr) {
//次数
for (int index = 0;index < arr.length-1; index++) {
for (int i =1+index; i < arr.length; i++) {
if (arr[i]<arr[index]){
int temp=arr[i];
arr[i]=arr[index];
arr[index]=temp;
}
}
}
}
}
1.5 希尔排序
import java.util.Arrays;
public class ShellSort {
public static void main(String[] args) {
int[] arr = {15, 5, 8, 4, 68, 84, 75, 51, 33, 45, 89};
int[] sort = ShellSortMain.sort(arr);
System.out.println(Arrays.toString(sort));
}
}
class ShellSortMain {
public static int[] sort(int[] arr) {
int gap = arr.length / 2;
while (gap > 0) {
for (int i = gap; i < arr.length; i++) {
int num = i - gap;
int temp = arr[i];
while (num >= 0 && arr[num] > temp) {
arr[num + gap] = arr[num];
num -= gap;
}
arr[num + gap] = temp;
}
gap /= 2;
}
return arr;
}
}
二、Arrays类
1.针对数组进行的操作的工具类
import java.util.Arrays;
public class Test01 {
public static void main(String[] args) {
//Arrays工具类,提供排序,查找等功能
// public static String toString () 遍历
int[] arr={20,30,40,50,60,70};
String s = Arrays.toString(arr);
System.out.println(s);//[20, 30, 40, 50, 60, 70]
// public static void sort () 排序
int[] arr2={24,51,23,13,25,14,26,14};
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));//[13, 14, 14, 23, 24, 25, 26, 51]
//public static int binarySearch ( int[] a, int key)
//二分查找 前提:元素是有序的
int i = Arrays.binarySearch(arr, 50);
System.out.println(i);//3
//对比两个数组中的元素是否一致
boolean equals = Arrays.equals(arr, arr2);
System.out.println(equals);//false
//static int[] copyOf ( int[] original, int newLength)
//复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
int[] ints = Arrays.copyOf(arr, 4);
System.out.println(Arrays.toString(ints));//[20, 30, 40, 50]
//static int[] copyOfRange ( int[] original, int from, int to)
//复制旧数组中的指定范围间的几个元素到新数组中,含头不含尾
int[] ints1 = Arrays.copyOfRange(arr, 2, 5);
System.out.println(Arrays.toString(ints1));//[40, 50, 60]
}
}
三、二分查找
1.二分查找的前提,数组是有序的。
public class Test02 {
public static void main(String[] args) {
int[] arr={10,20,30,40,50,60,70,80,90};
int index = getIndex(arr, 40);
System.out.println(index);
}
private static int getIndex(int[] arr, int value) {
int maxIndex=arr.length-1;
int minIndex=0;
int centerIndex=(maxIndex+minIndex)/2;
while (minIndex<=maxIndex){
if (value==arr[centerIndex]){
return centerIndex;
}else if (value<=arr[centerIndex]){
maxIndex=centerIndex-1;
}else if (value>=arr[centerIndex]){
minIndex=centerIndex+1;
}
//重新计算中间索引的值
centerIndex=(maxIndex+minIndex)/2 ;
}
return -1;
}
}