1public class BubbleSortExample {
2 static void bubbleSort(int[] arr) {
3 int n = arr.length;
4 int temp = 0;
5 for(int i=0; i < n; i++){
6 for(int j=1; j < (n-i); j++){
7 if(arr[j-1] > arr[j]){
8 //swap elements
9 temp = arr[j-1];
10 arr[j-1] = arr[j];
11 arr[j] = temp;
12 }
13
14 }
15 }
16
17}
1public static void bubbleSort(int arr[])
2{
3 for (int i = 0; i < arr.length; i++) //number of passes
4 {
5 //keeps track of positions per pass
6 for (int j = 0; j < (arr.length - 1 - i); j++) //Think you can add a -i to remove uneeded comparisons
7 {
8 //if left value is great than right value
9 if (arr[j] > arr[j + 1])
10 {
11 //swap values
12 int temp = arr[j];
13 arr[j] = arr[j + 1];
14 arr[j + 1] = temp;
15 }
16 }
17 }
18}
1public class DemoBubbleSort
2{
3 void bubbleSort(int[] arrNum)
4 {
5 int num = arrNum.length;
6 for(int a = 0; a < num - 1; a++)
7 {
8 for(int b = 0; b < num - a - 1; b++)
9 {
10 if(arrNum[b] > arrNum[b + 1])
11 {
12 int temp = arrNum[b];
13 arrNum[b] = arrNum[b + 1];
14 arrNum[b + 1] = temp;
15 }
16 }
17 }
18 }
19 void printingArray(int[] arrNum)
20 {
21 int number = arrNum.length;
22 for(int a = 0; a < number; ++a)
23 {
24 System.out.print(arrNum[a] + " ");
25 System.out.println();
26 }
27 }
28 public static void main(String[] args)
29 {
30 DemoBubbleSort bs = new DemoBubbleSort();
31 int[] arrSort = {65, 35, 25, 15, 23, 14, 95};
32 bs.bubbleSort(arrSort);
33 System.out.println("After sorting array: ");
34 bs.printingArray(arrSort);
35 }
36}
1func Sort(arr []int) []int {
2 for i := 0; i < len(arr)-1; i++ {
3 for j := 0; j < len(arr)-i-1; j++ {
4 if arr[j] > arr[j+1] {
5 temp := arr[j]
6 arr[j] = arr[j+1]
7 arr[j+1] = temp
8 }
9 }
10 }
11 return arr
12}
1public static void BubbleSortShortSC(int[] array)
2 {
3 for(int i = 0; i < array.length - 1; i++)
4 {
5 boolean sorted = true;
6 for (int j = 0; j < array.length - i - 1; j++)
7 {
8 if(array[j] < array[j+1])
9 {
10 int temp = array[j];
11 array[j] = array[j+1];
12 array[j+1] = temp;
13 sorted = false;
14 }
15 }
16 if (sorted)
17 break;
18 }
19 }
1for (int i = 0; i < n-1; i++)
2 for (int j = 0; j < n-i-1; j++)
3 if (arr[j] > arr[j+1])
4 {
5 // swap arr[j+1] and arr[i]
6 int temp = arr[j];
7 arr[j] = arr[j+1];
8 arr[j+1] = temp;
9 }