记录一些关于数组的代码

二分法查找:
public class Find {

  public static int find(int[] array, int aim) {
    int left = 0;
    int right = array.length - 1;
    
    
    while(left<=right){
    int middle = (left + right)/2;
    int middleValue = array[middle];
      if(middleValue == aim){
        return middle;
      }else if(middleValue > aim){
        left = middle + 1;
      }else{
        right = middle - 1;
      }
    }
    return -1;
  }

  public static void main(String[] args) {
    int[] array = {100, 90, 80, 75, 22, 3, 2};
    int result1 = find(array, 22);
    if (result1 == -1) {
      System.out.println("22 不存在数组中");
    } else {
      System.out.println("22 存在数组中,索引值是 " + result1);
    }

    int result2 = find(array, 50);
    if (result2 == -1) {
      System.out.println("50 不存在数组中");
    } else {
      System.out.println("50 存在数组中,索引值是 " + result2);
    }
  }
}

自制数组(支持增删数据,数组扩容)

public class YKDArrayList {

  // 此处需要声明一个数组,作为底层存储
  int[] array = new int[20];
  int size = 0;

  public YKDArrayList() {
  }

  // 获取数组的长度
  public int size() {
    return this.size;
  }

  // 数组获取某个索引值
  public int get(int index) {
    return this.array[index];
  }

  // 添加元素在末尾 
  public void add(int element) {
    //相当于调用传入this.size
    this.add(this.size, element);
  }

  // 添加元素在中间
  public void add(int index, int element) {
    if (index < 0 || index > this.size) {
      return;
    }

    // 支持扩容
    this.judgeMemory(this.size + 1);
    // 元素依次右移
    for (int i = this.size - 1; i >= index; i--) {
      this.array[i + 1] = this.array[i];
    }
    // 插入元素
    this.array[index] = element;
    // 调整size
    this.size++;
  }

  // 删除元素
  public void remove(int index) {
    if (index < 0 || index > this.size - 1) {
      return;
    }

    // 元素依次左移
    for (int i = index; i < this.size - 1; i++) {
      this.array[i] = this.array[i + 1];
    }
    // 删除最后一个元素
    this.array[this.size - 1] = 0;
    // 调整size
    this.size--;
  }

  private void judgeMemory(int size) {
    // 如果内存不满足,则需要扩容
    if (size > this.array.length) {
      // 申明两倍空间
      int[] newArray = new int[this.array.length * 2];
      // 拷贝操作
      this.copy(this.array, newArray);
      // 新数组赋值给原数组
      this.array = newArray;
    }
  }

  // 拷贝操作
  private void copy(int[] source, int[] aim) {
    for (int i = 0; i < source.length; i++) {
      aim[i] = source[i];
    }
  }

  public static void main(String[] args) {
    YKDArrayList ykdArrayList = new YKDArrayList();
    ykdArrayList.add(1);
    ykdArrayList.add(2);
    ykdArrayList.add(3);
    ykdArrayList.add(4);

    ykdArrayList.add(0, 5);

    ykdArrayList.remove(3);
  }
}

冒泡排序:
import java.util.Arrays;

public class Sort {

  // 冒泡排序
  public static void bubbleSort(int[] array) {
    // 每次循环,都能冒泡出剩余元素中最大的元素,因此需要循环 array.length 次
    for (int i = 0; i < array.length; i++) {
      // 每次遍历,只需要遍历 0 到 array.length - i - 1中元素,因此之后的元素都已经是最大的了
      for (int j = 0; j < array.length - i - 1; j++) {
        // 交换元素
        if (array[j] < array[j + 1]) {
          int temp = array[j];
          array[j] = array[j+1];
          array[j+1] = temp;
        }
      }
    }
  }


  public static void main(String[] args) {
    int[] array = {9, 2, 4, 7, 5, 3};
    // Arrays.toString 可以方便打印数组内容
    System.out.println(Arrays.toString(array));
    bubbleSort(array);
    System.out.println(Arrays.toString(array));
  }
}

选择排序:

在这里插入图片描述

import java.util.Arrays;

public class Sort {

  // 选择排序
  public static void selectSort(int[] array) {
    // 遍历N次,每次选择数组剩余元素中最大的元素
    for (int i = 0; i < array.length; i++) {
      // 暂时把第一个元素当做最大元素,并且记录最大元素的索引
      int maxIndex = 0;
      int max = array[0];
      // 注意 j 从 索引1 开始,到 array.length - i 截止
      for (int j = 1; j < array.length - i; j++) {
        // 如果元素大于当前最大值,则替换 max,maxIndex
        if (array[j] > max) {
          max = array[j];
          maxIndex = j;
        }
      }

      // 交换最大值元素 和 数组末尾元素
      int temp = array[maxIndex];
      array[maxIndex] = array[array.length - i - 1];
      array[array.length - i - 1] = temp;
    }
  }


  public static void main(String[] args) {
    int[] array = {9, 2, 4, 7, 5, 3};
    // Arrays.toString 可以方便打印数组内容
    System.out.println(Arrays.toString(array));
    selectSort(array);
    System.out.println(Arrays.toString(array));
  }
}


import java.util.Arrays;

public class Sort {

  // 选择排序
  public static void selectSort(int[] array) {
    for (int i = 0;i<array.length-1 ;i++ ){
      int m = i;
        for(int j = i+1 ;j<array.length;j++){
          if(array[j]<array[m]){
            m = j;
          }
          if(m!=i){
          int temp = array[i];
          array[i] = array[m];
          array[m] = temp;
            
          }
        }      
    } 
  }


  public static void main(String[] args) {
    int[] array = {9, 2, 4, 7, 5, 3};
    // Arrays.toString 可以方便打印数组内容
    System.out.println(Arrays.toString(array));
    selectSort(array);
    System.out.println(Arrays.toString(array));
  }
}

插入排序:

在这里插入图片描述

import java.util.Arrays;

public class Sort {

  // 插入排序
  public static void insertSort(int[] array) {
   for (int i = array.length-2;i>=0 ;i-- ){
     int temp = array[i];
     int j = i+1;
     while(j<array.length){
       if(array[j]<temp){
         array[j-1] = array[j];
       }else{
         array[j-1] = temp;
         break;
       }
       j++;
     }
     if(j==array.length){
       array[j-1] = temp;
     }
   } 
  }


  public static void main(String[] args) {
    int[] array = {9, 2, 4, 7, 5, 3};
    // Arrays.toString 可以方便打印数组内容
    System.out.println(Arrays.toString(array));
    insertSort(array);
    System.out.println(Arrays.toString(array));
  }
}



二分插入排序:
import java.util.Arrays;

public class Sort {

  // 查找应该插入的索引位置
  public static int searchIndex(int[] array, int left, int right, int aim) {
    // 循环查找节点位置
    while (left < right) {
      int middle = (left + right) / 2;
      int value = array[middle];
      if (value < aim) {
        left = middle + 1;
      } else {
        right = middle - 1;
      }
    }
    // 如果最终元素仍然大于目标元素,则将索引位置往左边移动一个
    if(array[left] > aim){
      return left -1;
    }
    // 否则就是当前位置
    return left;
  }

  // 插入排序
  public static void insertSort(int[] array) {
    // 从倒数第二位开始,遍历到底0位,遍历 N-1 次
    for (int i = array.length - 2; i >= 0; i--) {
      // 存储当前抽离的元素
      int temp = array[i];
      int index = searchIndex(array, i + 1, array.length - 1, temp);

      // 根据插入的索引位置,进行数组的移动和插入
      int j = i + 1;
      while (j <= index) {
        array[j - 1] = array[j];
        j++;
      }
      array[j - 1] = temp;
    }
  }


  public static void main(String[] args) {
    int[] array = {9, 2, 4, 7, 5, 3};
    // Arrays.toString 可以方便打印数组内容
    System.out.println(Arrays.toString(array));
    insertSort(array);
    System.out.println(Arrays.toString(array));
  }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值