Bubble Sort

Ketika interview untuk lowongan pekerjaan programmer biasanya sering ditanya tentang Algoritma Bubble Sort. Berikut ini code simplenya menurut Saya:

package org.paingan.example;

import java.util.Arrays;

public class BubbleSort {

	public static void main(String[] args) {
		int number[] = { 24, 21, 6, 8, 33, 33, 54, 11, 7, 9 };

		for (int i = 0; i < number.length; i++) {
			for (int j = 1; j < number.length; j++) {
				if (number[j-1] > number[j]) {
					int tmp = number[j-1];
					number[j-1] = number[j];
					number[j] = tmp;
				}
			}
		}

		System.out.println("result: " + Arrays.toString(number));
	}
}

Leave a Reply