Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

bubble sort java

public class BubbleSortExample {  
    static void bubbleSort(int[] arr) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(arr[j-1] > arr[j]){  
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
                          
                 }  
         }  
  
}  
Comment

bubble sort in java

    public static void sort(int[] arr) {
        int n = arr.length;

        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {

        int[] xr = {1, 3, 5, 2, 4};
        sort(xr);
        System.out.println(Arrays.toString(xr));
    }
Comment

bubble sort java

package com.SortingAlgorithm;

import java.util.Arrays;

public class Bubblesort {
    public Bubblesort() {
    }

    public static void main(String[] args) {
        int[] x = new int[]{5, 1, 4, 12, 7};
        int n = x.length;
        System.out.println("Original Array list is: " + Arrays.toString(x));

        for(int i = 0; i < n - 1; ++i) {
          // swapping of element occurs here
            if (x[i] > x[i + 1]) {
                int temp = x[i];
                x[i] = x[i + 1];
                x[i + 1] = temp;
            }
        }

        System.out.println("The Sorted list is:     " + Arrays.toString(x));
    }
}
Comment

bubble sort in java

class Sort 
{
    static void bubbleSort(int arr[], int n)
    {                                       
        if (n == 1)                     //passes are done
        {
            return;
        }

        for (int i=0; i<n-1; i++)       //iteration through unsorted elements
        {
            if (arr[i] > arr[i+1])      //check if the elements are in order
            {                           //if not, swap them
                int temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
            }
        }
            
        bubbleSort(arr, n-1);           //one pass done, proceed to the next
    }

    void display(int arr[])                 //display the array
    {  
        for (int i=0; i<arr.length; ++i) 
        {
            System.out.print(arr[i]+" ");
        } 
    } 
     
    public static void main(String[] args)
    {
        Sort ob = new Sort();
        int arr[] = {6, 4, 5, 12, 2, 11, 9};    
        bubbleSort(arr, arr.length);
        ob.display(arr);
    }
}
Comment

java bubble sort

def bubble_sort(nums):
    n = len(nums)
    for i in range(n):
        swapped = False
        for j in range(1, n - i):
            if nums[j] < nums[j - 1]:
                nums[j], nums[j - 1] = nums[j - 1], nums[j]
                swapped = True
        if not swapped: break
    return nums
print(bubble_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]))
Comment

Bubble Sort Java

public void bubbleSort(java.util.ArrayList aList) {
        int n = aList.size();
        int temp = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - i); j++) {
                if (aList.get(j - 1) > aList.get(j)) {
                    //swap elements  
                    temp = aList.get(j - 1);
                    aList.set(j-1, aList.get(j));
                    aList.set(j, temp);
                }
Comment

bubble sort java


list.set(list.get(j+1), temp);

Comment

PREVIOUS NEXT
Code Example
Java :: javafx file to image 
Java :: how to deselect radio button in java 
Java :: set icon to fab programmatically in android studio 
Java :: array slice java 
Java :: for loop in multidimensional array java 
Java :: Spigot API inventory close 
Java :: force fullscreen jframe 
Java :: making matrix in java 
Java :: new hashmap java 
Java :: java show error dialog 
Java :: how to create a java jframe 
Java :: java unique id 
Java :: removeAll hard code 
Java :: JFrame text java 
Java :: java product 1 to n 
Java :: taking date as input in java 
Java :: Utils code for plugin minecraft 
Java :: bungeecord plugin add configs 
Java :: android studio constrainglayout 
Java :: java swing keyadapter 
Java :: how to install java 8 on terminal os 
Java :: open a new activity on click of a button 
Java :: show confirmation dialog java android 
Java :: java write a file line by line 
Java :: spigot sounds 
Java :: android elevation not working 
Java :: How to generate all subsets of a given set in Java? 
Java :: how to add java_home in mac 
Java :: how to set the backtound color in java 
Java :: print a to z in java 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =