본문 바로가기
2015 IT 웹 기반 개발자과정/JAVA

JAVA Bubble Sort

by 한여름밤의코딩 2015. 10. 27.
package java_week2;
public class C1 {
		
		public void BubbleSort(C2 p){
				
			int j, tmp, flag;
			
			do{
				flag = 0;
				for (j = 0; j <p.Count-1; j++) {
					if(p.Array01[j]>p.Array01[j+1]){
						tmp = p.Array01[j];
						p.Array01[j] = p.Array01[j+1];
						p.Array01[j+1] = tmp;
						flag = 1;
					}
				}
			}while(flag==1);
			
				
	}
		
		public boolean BinarySearch(C2 p, int iFind)
		{
			int first, last, check;
			first = 0;
			last = p.Count-1;
			
			
			do{
				check = (first+last)/2;
				if(p.Array01[check]==iFind) return true;
				if(p.Array01[check] > iFind){
					if(last==check) return false;
					last = check;
				}else{
					if(first==check) return false;
					first = check;
				}					
				
			}while(first!=last);
			
			return false;
			
			
		}
		

		public static void main(String[] args) {
			C1 test = new C1();
			C2 Num01 = new C2(10);
			
			Num01.ArrayInput();
			Num01.PrintArray();
			test.BubbleSort(Num01);
			Num01.PrintArray();
			
			System.out.println(test.BinarySearch(Num01, 50));
	}		
}

==============================================================================================================

package java_week2;
import java.util.Scanner;
public class C2 {

	int		Array01[];
	int		Count;
	
	public	C2(int Count)
	{
		this.Count = Count;
		Array01 = new int [Count];
	}
	public	void PrintArray()
	{
		int	i;
		for(i=0;i<Count;i++) System.out.printf("%d,",Array01[i]);
		System.out.println();
	}
	public	void ArrayInput()
	{
		int	i;
		Scanner scan = new Scanner(System.in);
		for(i=0;i<Count;i++) {
			Array01[i] = scan.nextInt();	
		}		
	}
	
	
}