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 :: GenerationTarget encountered exception accepting command : Error executing DDL 
Java :: Java Create an OutputStream 
Java :: how to get request json web token in next js 
Java :: stringbuilder insert beginning 
Java :: java netbeans textfield only numbers 
Java :: java actionperformed 
Java :: spring log info 
Java :: To enable CORS in the MVC Java config 
Java :: string.indexof java 
Java :: Java print() and println() 
Java :: retrofit 
Java :: java string character at index 
Java :: android studio copy file 
Java :: get data from database sqlite android 
Java :: javafx fxmlloader location is not set 
Java :: rock paper scissors java 
Java :: Syntax of how to create ArrayList in Java 
Java :: program to check if the given date is in in the format dd/mm/yyyy java 
Java :: Spigot coding how to send a message to player after commadn 
Java :: android view set padding programmatically 
Java :: how to get cpu model number in android programmatically 
Java :: java creating a stack 
Java :: Java Create a BufferedOutputStream 
Java :: java.lang.NoClassDefFoundError: 
Java :: separateur JMenu swing java 
Java :: android studio hide linearlayout 
Java :: org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:876) 
Java :: spring data sql not utf8 
Java :: read excel file in java and store into arraylist 
Java :: what are variables in java 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =