Java Code:
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.crunchify;
import java.util.Arrays;
/**
* @author Crunchify.com
*/
public class CrunchifyBubbleSortAsce {
public static void main(String[] args) {
int arrayList[] = { 5,3,9,7,1,8 };
System.out.println("\nFinal result:"+Arrays.toString(CrunchifyBubbleSortAsceMethod(arrayList)));
}
public static int[] CrunchifyBubbleSortAsceMethod(int[] arr){
int temp;
for(int i=0; i < arr.length-1; i++){
for(int j=1; j < arr.length-i; j++){
if(arr[j-1] > arr[j]){
temp=arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println((i+1)+"th iteration result: "+Arrays.toString(arr));
}
return arr;
}
}
|
1
2
3
4
5
6
7
|
1th iteration result: [3, 5, 7, 1, 8, 9]
2th iteration result: [3, 5, 1, 7, 8, 9]
3th iteration result: [3, 1, 5, 7, 8, 9]
4th iteration result: [1, 3, 5, 7, 8, 9]
5th iteration result: [1, 3, 5, 7, 8, 9]
Final result:[1, 3, 5, 7, 8, 9]
|
No comments:
Post a Comment