Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

bubble sort for java ArrayList

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

Implement the Bubble sort algorithm on the following ArrayList

import java.util.ArrayList;

public class Sort{
    private static ArrayList<String> list = new ArrayList<String>();

    public static ArrayList<String> sortByName(String [] input) {
        String temp;
        for (int i=0; i< input.length; i++){
            for(int j= i; j< input.length-1; j++){
                char first = input[i].charAt(0);
                char sec = input[j +1].charAt(0);
                 if (first < sec)  {
                     temp = input[j +1];
                     input[j +1] = input[i];        
                     input[i] = temp;
                 }
             }
            list.add(input[i]);
         }

        return list;
    }

    public static void main(String[] args) {
        String string[] =  {"Ezen", "Allen" , "Wilker", "Kruden", "Crocket"};
        bubbleSortByName(string);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to create an action listener in java 
Java :: how to get data from radio group in android 
Java :: get the image from camera click in android 
Java :: spannable string in android java for color 
Java :: java big integer to int 
Java :: javafx change textfield background color 
Java :: how to check the current user in firebase android 
Java :: java producer consumer 
Java :: how to instance a calendar in java 
Java :: insert element into arraylist java 
Java :: how to find numbers of digits in java 
Java :: java subscript numbers 
Java :: charat(0).touppercase() java 
Java :: java replace a character at end of string 
Java :: java string array to one string 
Java :: jframe color 
Java :: default argument in java 
Java :: java stream get all max values 
Java :: check if list includes value java 
Java :: sum of array elements java 
Java :: how to use sql file in java 
Java :: int [] to Integer[] 
Java :: java replace element in list 
Java :: java checking the amount of duplicates in array 
Java :: boucle for avec un tableau java 
Java :: importing all java libraries 
Java :: method reference in java 
Java :: check if char is a space java 
Java :: if driver.find_element_by_xpath selnium java is displayed 
Java :: add element in the last in double linked list in java 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =