-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble Sort.java
38 lines (36 loc) · 945 Bytes
/
Bubble Sort.java
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
32
33
34
35
36
37
38
// Created by Justine Ogaraku
public class Program
{
static void bubbleSort(int[ ] lst){
boolean swapped;
int space = lst.length;
//a do-while loop to prevent futher itration after the list is sorted.
do{
swapped=false;
//BubbleSort😊
for(int i=0;i<space-1;i++){
if(lst[i+1]<lst[i]){
int first=lst[i];
lst[i]=lst[i+1];
lst[i+1]=first;
swapped = true;
}
}
// displays list after every sort
System.out.print("==> ");
for(int i=0; i<space;i++){
System.out.print(lst[i]+" ");
}
System.out.print("\n");
}while(swapped==true);
//displays the fully bubbleSorted list.
System.out.print("\nFinally: ");
for(int each:lst){
System.out.print(each+" ");
}
}
public static void main(String[] args) {
int[] nums ={3,4,1,6,0,2,8};
bubbleSort(nums);
}
}