Asymptotic Complexity | |
Time Complexity | Space Complexity |
n^2 | 1 |
If you want to see the same implementation in python please click the following link: Bubblesort implementation in python
If you would like to see how this implentation would look like in C click the following link: Bubblesort implementation in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BBSort | |
{ | |
private void bubblesort(int [] arr) | |
{ | |
int i = 1, temp; | |
boolean swapped = true; | |
while(swapped) | |
{ | |
swapped = false; | |
for(int j=0; j < arr.length - i ; j++) | |
{ | |
if(arr[j] > arr[j+1]) | |
{ | |
temp = arr[j]; | |
arr[j] = arr[j+1]; | |
arr[j+1] = temp; | |
swapped = true; | |
} | |
} | |
i++; | |
} | |
} | |
public static void main(String []args) | |
{ | |
int [] arr = { 12,34,32,5,6,2,3,4,23,34,233,64,64,3,634,43,322,3232,3}; | |
for (int i=0; i < arr.length; i++) | |
System.out.print(" " +arr[i]); | |
BBSort b = new BBSort(); | |
b.bubblesort(arr); | |
System.out.println(); | |
for (int i=0; i < arr.length; i++) | |
System.out.print(" " +arr[i]); | |
} | |
} |
No comments:
Post a Comment