거품 정렬,정렬 선택,정렬 정렬 정렬,빠 른 정렬,이분법 찾기,반전 링크------자바 기본 알고리즘

1766 단어 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

3.병합 정렬
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

4.빠 른 정렬...
5.이분법 찾기
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;
}

6.반전 링크
//         
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;
}

좋은 웹페이지 즐겨찾기