📜  在以下 ArrayList 上实现冒泡排序算法 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:03.606000             🧑  作者: Mango

代码示例1
import java.util.ArrayList;

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

    public static ArrayList 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);
    }
}