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 :: set location of a label java 
Java :: java servlets session call 
Java :: take string array input in java 
Java :: ova definition 
Java :: check if string contains numbers 
Java :: update value in hashmap java 
Java :: fullscreen in java 
Java :: java file reader utf 8 
Java :: java remove first element from array 
Java :: duck number in java 
Java :: calculate pi in java 
Java :: java read file text 
Java :: complicated average java code 
Java :: java string to int 
Java :: What AJAJ stand for 
Java :: W/System.err: java.io.IOException: Cleartext HTTP traffic to not permitted 
Java :: java kommentointi 
Java :: jpa validation string only number 
Java :: target element in a sorted matrix of distinct ints 
Java :: how to create a list in java 
Java :: android volley 
Java :: java swing stripes 
Java :: programmation android avoir acces à la liste des intents de partage 
Java :: checkindex java 
Java :: java par o impar 
Java :: android java toast 
Java :: PlatformException (PlatformException(error, Neither user 10024 nor current process has android.permission.WAKE_LOCK., null, java.lang.SecurityException: Neither user 10024 nor current process has android.permission.WAKE_LOCK. 
Java :: topological sort java 
Java :: change text color in joptionpane 
Java :: java create list of strings 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =