4.数组、排序、查找、二进制运算

4.1 数组

1.数组是可以存放多个同一类型的数据

2.用法:

数组定义:

程序员用法:

数据类型数组名[]=new数据类型[大小];

如:int  arr[]=new int[5];

或者是int[]  arr=new int[5];

或者是int  []arr=new int[5];

 

没事儿找事儿法:

Int arr[]; arr=new int[5];

或者是int[] arr; arr=new int [5];

或者是 int  []arr;arr=new int [5];

 

数组引用:数组名[下标]

  古板用法:int a[]={1,2,3,4,5,6,7,8,9,0};

 

3.知道数组的大小:arr.length(成员属性)

4.引用在栈里面,对象在堆里面

package com.list;
import java.io.*;
//该程序是用于演示对象数组的用法。
public class ListPractice
{
	public static void main(String []args) throws Exception
	{
		
		Dog dogs[]=new Dog[4];
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		for(int i=0;i<4;i++)
		{
			dogs[i]=new Dog();
			System.out.println("Input the name:");
			//read the name of dogs
			String name=br.readLine();
			dogs[i].setName(name);
			System.out.println("Inpute the age:");
			String age=br.readLine();
			float age_temp=Float.parseFloat(age);
			dogs[i].setAge(age_temp);
		}
		//Compute the total age
		float allAge=0;
		for(int i=0;i<4;i++)
		{
			allAge+=dogs[i].getAge();
		}
		//Compute the average age
		float avgAge=allAge/dogs.length;
		System.out.println("The total age is:"+allAge+"\nThe average age is:"+avgAge);
	}
}

class Dog
{
	String name;
	float age;
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public float getAge()
	{
		return age;
	}
	public void setAge(float age)
	{
		this.age = age;
	}

}


 

4.对象、字符串的很多不能用”==”,用的话是表示地址相等,比较字符串的内容是否相等是用equals方法

5.     数组可存放同一类型数据

        简单数据类型(int,float)数组,可以直接赋值

        对象数组在定义后,赋值时候需要再次为每个对象分配空间【即是:new对象】

        数组大小必须事先指定,如:int x;int a[]=new int[x];是可以的

        数组名可以理解为指向数组首地址的引用

        数组的下标是从0开始标号的

 

4.2排序

1.排序的介绍:排序是将一群数据依据一定的顺序进行排列的过程

        分类:

内部排序:将要排序的所有数据都加在到内存里面进行排序,包括交换式排序法、选择式排序法和插入式排序法

外部排序:数据量过大,无法全部加载到内存,包括合并排序法和直接合并排序法

 

2.交换式排序法属于内部排序法,是运用数据值比较后,依判断规则对数据的位置进行交换,已达到排序的目的,它包括:冒泡排序法(Bubble sort)和快速排序法(Quick sort)

 

3.冒泡排序法:

4.注意:新建一个类的对象而且调用了类的方法时候,虚拟机会在主栈的外面给调用的这个方法分配一个相应的栈,在外面的这个栈里面对数组或者对象的操作就是对主栈的相应的数组或者对象的操作,但是不是针对所有的类型,如果是int等简单类型就不行。

 

5.选择式排序法:选择排序法(快于冒泡排序法):

        选择排序法(select sorting)也是一种简单的排序方法,第一次从R[0]~R[n-1]中寻选取最小值,与R[0]交换,第二次从R[1]~R[n-1]中选取最小值,与R[1]交换,第三次从R[2]~R[n-1]中选取最小值与R[2]交换……总共(n-1)次。

执行如下程序需要静等大概1分钟左右,视电脑配置而定。

4.3  排序查找

1.插入式排序法分为:

        插入排序法(Insertion Sort

        谢尔排序法(shell sort

        二叉树排序法(Binary-tree sort

2.插入式排序法插入排序法(Insertion Sorting

基本思想:把n个待排序的元素看成是一个有序表和一个无序表,开始时候有序表只包含一个元素,无序表中包含有n-1个元素,排序过程中每次从无序表中取出一个元素,把她的排序吗一次和有序表元素的排序码进行比较,将它插入到有序表的适当位置,使之成为新的有序表。

3.插入法的实现:

4.交换式排序法——快速排序法

快速排序(Quick sort)是对冒泡排序法的改进,1962年提出的,使用递归思想。

5.希尔排序法

6.合并排序法:外部排序法最常用的方法,如果数据量太大无法一次完全加载,可使用外部辅助内存来处理数据,主要应用在文件排序。

7.查找:顺序查找和二分查找(使用二分查找前提是先排好序)

 

package com.sorting;
import java.util.*;
/*
 * Demonstrate all the sorting methods.
 */
public class Sorting
{
	public static void main(String[] args)
	{
		int len=90000;
		int arr1[]=new int[len];
		int arr2[]=new int[len];
		int arr3[]=new int[len];
		for(int i=0;i<len;i++)
		{
			//Generate a number between 1-10000.
			//Math.random() will generate a number x.    (0<=x<1)
			int t=(int) (Math.random()*100);
			arr1[i]=t;
			arr2[i]=t;
			arr3[i]=t;
		}
		
//Bubble sorting
		Bubble bubble=new Bubble();

		//Print the time of system before sorting.
		Calendar cal=Calendar.getInstance();
		System.out.println("(Bubble)Before:"+cal.getTime());
		bubble.sort(arr1);
		//Get instance time again.
		cal=Calendar.getInstance();
		System.out.println("(Bubble)After:"+cal.getTime());

//Selection sorting		
		Selection selection=new Selection();
		
		cal=Calendar.getInstance();
		System.out.println("(Selection)Before:"+cal.getTime());		
		selection.sort(arr2);
		cal=Calendar.getInstance();
		System.out.println("(Selection)Before:"+cal.getTime());

//Insert sorting
		InsertSort insertsort=new InsertSort();
		
		cal=Calendar.getInstance();
		System.out.println("(Insert)Before:"+cal.getTime());	
		insertsort.sort(arr3);
		cal=Calendar.getInstance();
		System.out.println("(Insert)Before:"+cal.getTime());
		
		
//This is the test Demo
		int a=12;
		System.out.println();
		bubble.test(a); 
		System.out.println(a);
		//The value of a is 12 rather than 13.
		
//print all the numbers.
		/*for(int i=0;i<arr3.length;i++)
		{
			System.out.print(arr3[i]+" ");
		}*/
	}
}

//All the sorting methods are increasing!
class Bubble
{
	//This is a test function to explain when the value will change
	//while calling a method.
	public void test(int a)
	{
		a++;
	}
	public void sort(int arr[])
	{
		int temp=0;
   		for(int i=0;i<arr.length-1;i++)
		{
			//When i=0,it seems that all the other numbers compare with a[0].
			for(int j=i+1;j<arr.length;j++)
			{
				if(arr[i]>arr[j])
				{
					temp=arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				}
			}
		}
	}
}

class Selection
{
	public void sort(int arr[])
	{
		int min=arr[0];
		int sub=0;
		int temp=0;
		for(int i=0;i<arr.length-1;i++)
		{
			min=arr[i];
			sub=i;
			for(int j=i;j<arr.length;j++)
			{
				if(min>arr[j])
				{
					min=arr[j];
					sub=j;
					//sub is used for labeling the max number.
				}
			}
			
			temp=arr[i];
			arr[i]=arr[sub];
			arr[sub]=temp;
		}
	}
}

class InsertSort
{
	public void sort(int arr[])
	{
		for(int i=1;i<arr.length;i++)
		{
			int insertVal=arr[i];
			//insertVal is going to compare with the number before it
			int index=i-1;
			while(index>=0&&insertVal<arr[index])
			{
				//move arr[index] backward
				arr[index+1]=arr[index];
				index--;
			}
			//insert insertVal in the proper position
			arr[index+1]=insertVal;
		}
	}
}


 

 

4.4 多维数组

1.多维数组,定义:

    语法:类型数组名[][]=new类型[大小][大小] int a[][]=new int [2][3]

2.

 

public class Array1
{
    public static void main(String[] args)
    {
       int a[][]=new int[4][6];
       a[1][2]=1;
       a[2][1]=2;
       a[2][3]=3;
       for (int i=0;i<4;i++)
       {
       //列
           for (int j=0;j<3;j++)
              System.out.print(a[i][j]+" ");
              System.out.println();         
       }
      }
}

4.5 二进制位运算移位计算

1.0的反码和补码都是0

2.我们只会针对有符号的数才讲反码补码

3.java没有无符号数,换言之,java中的数都是有符号的

4.在计算机运算的时候,都是以补码的方式运算的

5.按位与& | 异或^ 取反~

6.~-2=-3(00000010->11111101->11111100->10000011)

7.2&3=00000010  2|3=00000011  ~-5=1000 0101->11111010->11111011(补码)->000001004

8.3个移位运算符:

>>算术右移:低位溢出,符号位不变,并用符号位补由于移动而空出的高位

<<算术左移:符号位不变,地位补0

>>>逻辑右移:

9.记住:对负数操作就要先找补码!!!正数也是,只不过不边罢了。得到的结果如果是正数就结束,如果是负数就再折腾一下,转变成原码才行。

10.-1移动任意位都是-1

11.1<<2(相当于是*4)=4-1<<2(相当于是*4)=-4

12. 而对于有符号数,其左移操作还是逻辑左移,但右移操作是采用逻辑右移还是算术右移就取决于机器了!(算术右移和逻辑右移的区别是:算术右移不右移符号位,即最高位,右移后前补0还是1取决于符号位的值;逻辑右移执行时将移动符号位,右移后前补0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值