bubble sort using java

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.

사용자 삽입 이미지

	public static void bubbleSort() {

		int temp = 0;
		int[] data = new int[5];

		data[0] = 1;
		data[1] = 5;
		data[2] = 100;
		data[3] = 20;
		data[4] = 50;

		for (int i = 0; i < data.length; i++) {
			for (int j = i + 1; j < data.length; j++) {
				if (data[i] < data[j]) {
					temp = data[i];
					data[i] = data[j];
					data[j] = temp;
				}
			}
		}

		for (int x = 0; x < data.length; x++) {
			System.out.println(data[x]);
		}
	}

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.