冒泡排序,选择排序,归并排序,快速排序,二分法查找,反转链表-------------------java最基础算法

1----冒泡排序

private void bubblingSort(int [] arr){
	int temp = 0;
	for(int i = 0; i < arr.length-1;i++){
		for(int j = 0; j < arr.length-1-i;i++){
			if(arr[j]>arr[j+1]){
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp; 
			}
		}
	}
}

二、选择排序

private void chooseSort(int [] arr){
	for(int i = 0; i

三、归并排序

public static void mergeSort(int[] arr1,int[] arr2){
	int a = 0;
	int b = 0;
	int n = 0;
	int[] newArr = new int[arr1.length+arr2.length];
	while( a < arr1.length && b < arr2.length){
		if(arr1[a]arr2[b]){
			newArr[n] = arr2[b];
			n++;
			b++;
		}
	}
	while(a

四、快速排序
。。。

五、二分法查找

public static int binarySearch(int[] arr,int key){
	int start = 0;
	int end =arr.length-1;
	int center;
	//如果key不在数组中,直接return -1
	if( key < arr[start] || key > arr[end] || start > end){
		return -1;
	}
	while(start <= end){
		center = (start + end)/2
		if(keyarr[center]){
			//hey在数组后半段
			start = center + 1;
		}
	}
	return -1;
}

六、反转链表

//先定义节点的实体类
public Node{
    int data;
    Node next;
    public Node(int data,Node next;){
	   this.data = data;
	   this.next = next;
    }
}
//反转链表
public Node reverseList( Node node){
	Node prev = null;
	Node now = node;
	while(now != null){
		Node next = now.next;
		now.next = prev;
		prev = now;
		now = next;
	}
	return prev;
}

你可能感兴趣的:(Java)