Day_06_数组、排序

本文介绍了数据结构的概念,强调了合理选择数据结构对存储和运行效率的重要性。详细讲解了数组的静态和动态声明,以及数组的操作特点。接着讨论了传值与传引用的区别,并展示了数组复制的方法。还探讨了二维数组的使用和操作。最后,文章通过实例介绍了冒泡排序和选择排序这两种基础排序算法,并提到了API排序的便捷性。
摘要由CSDN通过智能技术生成

1.数据结构

计算机存储和组织数据的一种方式,根据存储的方式不同,操作特性也就不同

合理选择数据结构能够有效的提升存储效率和运行效率

数据操作 : 增删改查

常见数据结构 ;

数组,链表,散列表,红黑树,B+树,二叉树等...

2.数组


 数组有指定下标,并且是连续存储,通过内存地址偏移,可以快速定位到我们要操作的数据.
 所以数组在查询效率上是极高的
 
  数组 : 添加删除慢 , 查询更改 快
  
  变量 : 数据类型 变量名 = 值


  
2.1  数组声明


2.1.1静态声明 : 在已知数组中每个元素的情况下,使用静态声明 
              数据类型[] 变量名 = {值1 , 值2 , 值3....};
              数据类型 变量名[] = {值1 , 值2 , 值3....};
              int i = 1;
              int[] a = {1,2,3,4};  int一维数组
              int[][] b = {{1,2,3},{2,3,4},{4,3,2}};  int二维数组
              int[][][] c =  int三维数组
     
              数组可以多维,但是每一维的类型必须一致
  
2.1.2动态声明 : 在不知道数组中每个元素的情况下,使用动态声明,需要指定长度,然后用对应默认值占位
            数据类型[] 变量名 = new 数据类型[长度];
              int[] a = new int[18];
              boolean[] b = new boolean[10];
              int[][] c = new int[3][5];  二维数组中有3个一维数组,每个一维数组中有5个数据
                      c = {
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                              {0,0,0,0,0},
                          };
              int[][][] d = new int [3][4][5];
  
  
          整数 : 0  , 小数 : 0.0 , 字符 : \u0000 , 布尔型 : false , 引用类型 : null

2.1.3 另一种声明方式

int i = 1;
		// 传递变量
		m2(i);
		// 传递字面量
		m2(12);

		int[] arr = {};
		// 传递变量
		m1(arr);
		// 传递字面量
		m1(new int[] { 1, 3, 4 });
	}

	public static void m2(int i) {

	}

	public static void m1(int[] arr) {

	}

2.2传值和传引用

传值:传递基本类型

传引用:传递引用类型

public static void main(String[] args) {
		int i = 1;
		m1(i);
		// 1 
		System.out.println(i);
		System.out.println("------");
		int[] arr = {1,2,3};
		m2(arr);
		// 100
		System.out.println(arr[0]);
	}
	public static void m1(int i){
		i++;
		// 2
		System.out.println(i);
	}
	public static void m2(int[] arr){
		arr[0] = 100;
		// 100
		System.out.println(arr[0]);
	}

2.3 数组复制

public static void main(String[] args) {
		int[] src = {2,3,4,5,6,7,8,9};
		int[] dest = {11,12,13,14,15,16,17,18,19};
		// 第一个参数 是 源数组 , 2 是源数组起始位置(包含) , 3 目标数组 , 4 目标数组起始位置(包含) , 5 替换个数
		System.arraycopy(src, 2, dest, 3, 3);
		for (int i = 0; i < dest.length; i++) {
			System.out.println(dest[i]);
		}
		// 增强for循环 foreach
		// for(数据类型 变量 : 数组){}  类型一般是数组中元素的类型,把数组中每个元素拿出来给了这个变量
		for(int element : dest){
			System.out.println(element);
		}
	}

3. 二维数组

public static void main(String[] args) {
		// 静态声明
		int[][] arr1 = {
				{1,2,3},
				{1,4,6},
				{12,41,11,12,3,45}
		};
		// 动态声明  有3个一维数组,每个一维数组中有4个元素
		int[][] arr2 = new int[3][4];
		
		// 获取第一个元素
		int[] arr1_0 = arr1[0];
		int arr1_00 = arr1_0[0];
		System.out.println(arr1_00);
		// [第几个一维数组][第几个元素]
		System.out.println( arr1[0][0] );
		
		// 更改
		arr1[0][0] = 100;
		
		arr2[1][2] = 1;
		// 遍历
		for (int i = 0; i < arr2.length; i++) {
			// int[] arr = arr2[i];
			for (int j = 0; j < arr2[i].length; j++) {
				System.out.print(arr2[i][j] + "  ");
			}
			System.out.println();
		}
	}

4. 排序

4.1 交换变量的值

public static void main(String[] args) {
		int x = 2;
		int y = 3;

		// 1 中间变量(开发常用)
		// int temp = x;
		// x = y;
		// y = temp;

		// 2 位移运算交换(面试用)
		// ^ : 转换为二进制,每位进行异或,相同为0,不同为1
		// 2 0000 0010
		// 3 0000 0011
		// x = x ^ y; // x = 0000 0001
		// y = x ^ y; // y = 0000 0010
		// x = x ^ y; // x = 0000 0011

		// 3 加减运算
		x = x + y; // x=5
		y = x - y; // y = 2
		x = x - y; // x = 3

		System.out.println("x : " + x + " , y : " + y);
	}

4.2 冒泡排序

1.比较相邻的两个元素,如果第一个比第二个大,就交换位置

2.对每一对相邻元素做同样的工作,比较完一轮之后,最后一个一定是最大的哪个

3.在对以上元素重复该步骤,除了最后一个

4.一直到没有一对元素需要比较,终止

public class Array_02_BubbleSort {
	public static void main(String[] args) {
		int[] arr = {3,2,4,5,1};
		bubbleSort(arr);
		for (int i : arr) {
			System.out.println(i);
		}
	}
	public static void bubbleSort(int[] arr){
		for (int i = 0; i < arr.length-1; i++) {
			for (int j = 0; j < arr.length-1-i; j++) {
				if (arr[j] > arr[j+1]) {
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
	}
}

4.3 选择排序

 选择排序 :
1.每次都把最小的放到左边
   先拿出第一个 假设是最小的,然后挨个和后面所有的进行比较,如果有比他小的,就保存对应的下标2.比较完之后,就保存了最小的这个下标,然后判断是否和 第一个相同,不同就换位

public class Array_03_SelectSort {
	public static void main(String[] args) {
		int[] arr = {3,2,4,5,1};
		selectSort(arr);
		for (int i : arr) {
			System.out.println(i);
		}
	}
	public static void selectSort(int[] arr){
		for (int i = 0; i < arr.length; i++) {
			// 假设当前位  是最小的
			int min = i;
			for (int j = i+1; j < arr.length; j++) {
				// 如果有比min小的,就把该下标赋值给min
				if (arr[min] > arr[j]) {
					min = j;
				}
			}
			// 判断 是否一致,如果一致 说明i就是最小的,就继续下次循环
			// 如果不一致说明有比i小的,就换位
			if (min != i) {
				int temp = arr[i];
				arr[i] = arr[min];
				arr[min] = temp;
			}
		}
	}
}

4.4 API排序

public class Array_04_API {
	public static void main(String[] args) {
		int[] arr = { 3, 2, 4, 5, 1 };
		// 直接调用即可
		Arrays.sort(arr);
		for (int i : arr) {
			System.out.println(i);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值