简体   繁体   English

是否可以在哈希图中进行冒泡排序?

[英]Is it possible to bubble sort in hashmaps?

I have a hashmap of 42 customers assigned to their seats for a reservation system and I need to bubble sort them to display them alphabetically.我有一个分配给预订系统座位的 42 个客户的哈希图,我需要对它们进行冒泡排序以按字母顺序显示它们。 Is it possible to do this on HashMaps?是否可以在 HashMaps 上执行此操作?

bubble sort to sort the keys in hashMap use following code:-冒泡排序对 hashMap 中的键进行排序使用以下代码:-


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReservationSystem {

    static Map<String, Integer> map = new HashMap<>(); 

    public static void main(String[] args) {

        //put your customers name and seat number
        map.put("Jayant", 80); 
        map.put("Abhishek", 90); 
        map.put("Anushka", 80); 
        map.put("Amit", 75); 
        map.put("Danish", 40); 
        // use this method to sort based on keys
        //sortbykey();

        List<String> list=new ArrayList<String>();
        list.addAll(map.keySet());

        String temp;
        boolean sorted = false;
        while (!sorted) {
            sorted = true;
            for (int i = 0; i < list.size()-1; i++) {
                if (list.get(i).compareTo(list.get(i + 1)) > 0) {
                    temp = list.get(i);
                    list.set(i, list.get(i + 1));
                    list.set(i + 1, temp);
                    sorted = false;
                }
            }
        }

        System.out.println(list);


    }

}

Hope this answer your question...希望这能回答你的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM