Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubble Sort Algorithm in Array #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Data-Structures/Java/Sorting/Bubblesort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Bubblesort {

// Bubble sort = pairs of adjecent elements are compared , and the elements
// swaped if they are not in order.

// Quadratic time O(n^2)
// small data set = okey-ish
// large data set = bad
public static void main(String[] args) {
int [] array= {9,6,7,3,4,2,8,5,1};

bubblesort(array);

for(int i: array)
System.out.print(i);
}
public static void bubblesort(int [] array){

for(int i=0; i<array.length-1; i++){ // this loop for compare every elements of array with others
for(int j=0; j<array.length-i-1; j++){// this loop for give order to elements
if(array[j]>array[j+1]){ // condition for checking 1st two elements with each other
int temp=array[j]; // if 1st element is greater then 2nd then it will store in temp
array[j]=array[j+1]; // then 1st element will be equal to 2nd
array[j+1]=temp; // and 2nd is equal to temp which was 1st element
}
}
}
}
}