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 use input in java 
Java :: java lowercase 
Java :: java replaceall regex 
Java :: java array to string 
Java :: javafx resizable window 
Java :: how to check whether a character is alphabet or not in java 
Java :: jframe change icon 
Java :: java Modulo 10^9+7 (1000000007) 
Java :: how to get int from string java 
Java :: Problem adding rectangle in javaFX 
Java :: if number is negative java 
Java :: Do not concatenate text displayed with setText. Use resource string with placeholders. 
Java :: contains hashmap java 
Java :: how to generate random id in java 
Java :: Java how to make a number with digits 
Java :: how to initialize a set in java 
Java :: find min in array java 
Java :: iterable interface in java 
Java :: image cropper implementation 
Java :: java protected 
Java :: get the max value from arrayList java and save it in int 
Java :: java button size 
Java :: java eclipse console clear 
Java :: compare two character alphabetically java 
Java :: android studio alert dialog box 
Java :: long vs int java 
Java :: java string array to one string 
Java :: scanner.hasnext() 
Java :: how to check base64 in java 
Java :: pointers in java 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =