[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
Flow Chart of BubbleSort Algorithm
Implementation in Java
Now, it's time that we should code the lines for BubbleSort Algorithm.
Result
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.
Result
<Picture3> Result of BubbleSort Algorithm
Comments
Post a Comment