[Algorithm] Bubble Sort Algorithm

Today, I'm going to implement BubbleSort Algorithm by using Java.  Once you understand the principle of the algorithm and try, It will be familiar to implement it. Let's go cracking!

Principle of BubbleSort Algorithm
<Picture1> Process of BubbleSort Algorithm
As you can see the cycle process above, BubbleSort Algorithm comares just only two values in each pair of value. During the 1st Cycle, When criterion value(5) is bigger than comparison value(4), then change the index each other like ascending order(4 --> 5). If criterion value is smaller than comparison value, you don't have to change their index and so does in case of same with comparison value. Keep moving the index of the criterion value one by one until the 1st cycle finishes. Then, turn to the 2nd Cycle and do the same process just like it did in the 1st Cycle. The array has 5 values. Thus, you'll need 5 times or less than 5 times cycle to do.

Flow Chart of BubbleSort Algorithm
<Picture2> Flow Chart of BubbleSort Algorithm>
Let me show how to flow chart of BubbleSort Algorithm. Provided that you conduct 5 times of BubbleSort Cycle(Because the array size is 5). First, declare array of Integer data type and Integer type variable 'temp'. Then, I'll use 2 for loop clause. First for loop is to rotate the Cycle(from i=0 to i=length of array-1, total 5 times) and second one is for comparing process in each Cycle(from j=0 to j=length of array-2, total 4 times). If you have question about why length of array should be minus 1, that's because the array's index in programming language is start from '0', not '1'. So, I just conducted minus 1 from the length of array. Next, I also declared 'temp' variable to use it when changing between two values should be conducted. Following is a way of how to switch two values. First, Configure criterion value(j) is bigger than comparison value(j+1). If it is, then substitute 'temp' variable for the criterion value(j). Then it is clear that the memory area of criterion value become no value state temporarily. Next, put the comparison value(j+1) into the criterion value(j), which has no value. Consequently, comparison values will become empty with no value. Then, replace comparison value with 'temp' variable's value.

Implementation in Java
Now, it's time that we should code the lines for BubbleSort Algorithm.
class BubbleSortTest
{
public static void main(String[] args)
{
//Field
int [] arr = {5,4,3,2,7};
int temp = 0;
//For loop1(Cycle)
for(int i=0;i<arr.length;i++){
//For loop2(Comparing values)
for(int j=0;j<arr.length-1;j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}//end of loop2
System.out.print("Cycle"+(i+1)+" result : [");
//For loop3
for(int k=0;k<arr.length;k++){
if(k == (arr.length-1)){
System.out.println(arr[k]+"]");
}
else{
System.out.print(arr[k]+", ");
}
}//end of loop3
}//end of loop1
System.out.print("Final result : [");
//For loop4
for(int k=0;k<arr.length;k++){
if(k == (arr.length-1)){
System.out.println(arr[k]+"]");
}
else{
System.out.print(arr[k]+", ");
}
}//end of loop4
}//end of main
}//end of class
fff
Result
<Picture3> Result of BubbleSort Algorithm

Comments

Popular posts from this blog

$$ Regular Expression이 안먹는 경우

exception in thread main java.lang.unsupportedclassversionerror unsupported major.minor version 52.0

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver