笔记内容: 数组及数组内存的划分
笔记来源:毕向东JavaSE基础视频05,(我下载的视频上面是这样的,没有第几天)
正文:我的全写在代码里
- package cxw.study.one;
- import org.junit.Test;
- public class Array {
- /*
- * 内存的划分:
- * 寄存器
- * 本地方法区
- * 方法区
- * 栈内存
- * 存储的都是局部变量
- * 而且变量所属的作用域一旦结束,该变量就自动释放
- * 堆内存
- * 存储是数组和对象(其实数组就是对象) 凡是new建立在堆中
- * 特点:
- * 1.每一个实体都有首地址值
- * 2.堆内存中的每一个变量都有默认初始化值,根据类型的不同而不同,
- * 整数是0,小数是0.0或者0.0f boolean false char '\u0000'
- * 3.垃圾回收机制
- */
-
- /**
- * 折半查找
- * 二分查找法
- */
- @Test
- public void halfSearch(){
- int[] array = {11, 13, 15, 19, 28, 33, 45, 78, 106};
- int index = 0;
- int key = 11;
- int max, min, mid;
- min = 0;
- max = array.length;
- mid = (min + max) / 2;
-
- while(array[mid] != key){
- if(key > array[mid]){
- min = mid + 1;
- } else if(key < array[mid]){
- max = mid - 1;
- }
- if(max < min){
- index = -1;
- System.out.println( key + "的下标为:" + index);
- break;
- }
- mid = (max + min) / 2;
- }
- if(index != -1){
- index = mid;
- System.out.println( key + "的下标为:" + index);
- }
- }
- /*
- 冒泡排序 :基本思想是每次进行相邻的两个数对比a(i)>a(i+1),然后将两个数交换
- int[] array = { 34, 19, 11, 109, 3, 56, 1, 99, 123, 112, 564 };
- 排序的步骤:
- 第1步[19, 11, 34, 3, 56, 1, 99, 109, 112, 123, 564]
- 第2步[11, 19, 3, 34, 1, 56, 99, 109, 112, 123, 564]
- 第3步[11, 3, 19, 1, 34, 56, 99, 109, 112, 123, 564]
- 第4步[3, 11, 1, 19, 34, 56, 99, 109, 112, 123, 564]
- 第5步[3, 1, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 第6步[1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 第7步[1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 第8步[1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 第9步[1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 第10步[1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 所以最终的结果为
- [1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- */
- @Test
- public void bubbleSort(){
- int[] array = { 34, 19, 11, 109, 3, 56, 1, 99, 123, 112, 564 };
- System.out.println("原数组:");
- printArray(array);
- for(int i = 0; i < array.length - 1; i++){
- for(int j = 0; j < array.length - 1 - i; j++){
- if(array[j] > array[j+1]){
- int temp = array[j];
- array[j] = array [j+1];
- array[j+1] = temp;
- }
- }
- printArray(array);
- }
- // printArray(array);
- }
- /*
- 选择排序:基本思想是每次循环将此次循环的最值放到后面
- int[] array = { 34, 19, 11, 109, 3, 56, 1, 99, 123, 112, 564 };
- 步骤如下:
-
- 第1步 [1, 34, 19, 109, 11, 56, 3, 99, 123, 112, 564]
- 第2步 [1, 3, 34, 109, 19, 56, 11, 99, 123, 112, 564]
- 第3步 [1, 3, 11, 109, 34, 56, 19, 99, 123, 112, 564]
- 第4步 [1, 3, 11, 19, 109, 56, 34, 99, 123, 112, 564]
- 第5步 [1, 3, 11, 19, 34, 109, 56, 99, 123, 112, 564]
- 第6步 [1, 3, 11, 19, 34, 56, 109, 99, 123, 112, 564]
- 第7步 [1, 3, 11, 19, 34, 56, 99, 109, 123, 112, 564]
- 第8步 [1, 3, 11, 19, 34, 56, 99, 109, 123, 112, 564]
- 第9步 [1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 第10步[1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- 所以最终的结果为
- [1, 3, 11, 19, 34, 56, 99, 109, 112, 123, 564]
- */
- @Test
- public void selectSort() {
- int[] array = { 34, 19, 11, 109, 3, 56, 1, 99, 123, 112, 564 };
- System.out.println("原数组:");
- printArray(array);
- for (int i = 0; i < array.length - 1; i++) {
- for (int j = i + 1; j < array.length; j++) {
- if (array[i] > array[j]) {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- printArray(array);
- }
- //printArray(array);
- }
- /**
- * 遍历数组
- *
- * @param array
- */
- public void printArray(int[] array) {
- System.out.print("[");
- for (int i = 0; i < array.length; i++) {
- if (i != array.length - 1) {
- System.out.print(array[i] + ", ");
- } else {
- System.out.println(array[i] + "]");
- }
- }
- }
- /**
- * 折半查找
- * 二分查找法
- */
- @Test
- public void halfSearch_2(){
- int[] array = {13, 15, 19, 28, 33, 45, 78, 106};
- int index = -1;
- int key = 33; //要查找的数
- int max, min, mid;
- min = 0;
- max = array.length;
- while(min <= max){
- mid = (max + min) / 2;
- if(array[mid] == key){
- index = mid;
- System.out.println( key + "的下标为:" + index);
- break;
- }
- if(key > array[mid]){
- min = mid + 1;
- } else if(key < array[mid]){
- max = mid - 1;
- }
- }
- if(index == -1){
- System.out.println( key + "的下标为:" + index);
- }
- }
- }
复制代码 |