Java多字段排序之冒泡
2021-01-20 05:12
标签:dom 英文字母 rom one val key pru ada current 场景: 需求: Java的实现一:暴力排序,即进行四次冒泡排序。 代码: 验证: 执行结果: Java多字段排序之冒泡 标签:dom 英文字母 rom one val key pru ada current 原文地址:https://www.cnblogs.com/qujiayuan/p/12904682.html
奥运会时期,每场赛事后,对国家的金、银、铜牌进行排序,排序规则如下:
先以金牌排序,金牌数越多,排得越前,如果金牌数相同,则比银牌数,如果银牌数一样多,则比铜牌数,如果铜牌数还一样多,则按国家英文字母顺序升序排序。
例如:
China 51 20 21
American 50 1 1
Japan 0 0 0
上面是三个国家的奖牌数,每一行依次是国家名、金牌数、银牌数、铜牌数。
请将奖牌按上面规则进行排序。 public static String[] rankByBubble(String[] paramArray) {
String[][] scores = new String[paramArray.length][4]; // 存储每个国家的金、银、铜的分数
for (int i = 0; i Integer.valueOf(scores[j][1])) {
String[] temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
// 金牌一样时,比银牌
for (int i = 0; i Integer.valueOf(scores[j][2])) {
String[] temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
// 金牌、银牌一样时,比铜牌
for (int i = 0; i Integer.valueOf(scores[j][3])) {
String[] temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
// 金牌、银牌、铜牌一样多时,按国家字母升序排
for (int i = 0; i
@Benchmark
@Test
public void rankByBubble() {
String[] countries = {"Afghanistan 55 23 31", "Albania 68 3 79", "Algeria 66 39 62", "Andorra 15 88 70", "Angola 0 65 55", "Anguilla 44 46 47", "Antigua_and_Barbuda 12 10 119", "Argentina 26 52 10", "Armenia 18 0 45", "Ascension 69 6 17", "Australia 74 90 21", "Austria 12 9 58", "Azerbaijan 14 11 4", "Bahamas 32 31 29", "Bahrain 45 32 115", "Bangladesh 49 70 90", "Barbados 37 26 24", "Belarus 47 89 107", "Belgium 78 72 51", "Belize 58 18 98", "Benin 60 43 37", "Bermuda_Is. 26 53 69", "Bolivia 65 29 56", "Botswana 0 49 29", "Brazil 67 78 51", "Brunei 26 57 119", "Bulgaria 51 99 112", "Burkina-faso 57 33 78", "Burma 79 34 52", "Burundi 76 51 12", "Cameroon 66 15 57", "Canada 52 77 88", "Cayman_Is. 21 68 28", "Central_African_Republic 28 46 85", "Chad 65 45 81", "Chile 18 59 84", "China 28 24 39", "Colombia 57 84 38", "Congo 24 11 88", "Cook_Is. 31 24 65", "Costa_Rica 61 48 70", "Croatia 70 35 28", "Cuba 17 23 36", "Cyprus 60 12 51", "Czech_Republic 44 44 90", "Denmark 9 13 22", "Djibouti 40 92 97", "Dominica_Rep. 35 20 96", "Ecuador 10 68 94", "Egypt 2 47 12", "EI_Salvador 41 79 62", "Estonia 1 59 51", "Ethiopia 73 60 22", "Fiji 46 69 92", "Finland 60 99 84", "France 69 43 85", "French_Guiana 46 92 47", "French_Polynesia 11 42 94", "Gabon 13 14 31", "Gambia 19 63 83", "Georgia 14 6 81", "Germany 65 89 116", "Ghana 59 20 50", "Gibraltar 26 90 30", "Greece 63 47 112", "Grenada 39 36 6", "Guam 4 1 0", "Guatemala 46 24 7", "Guinea 20 55 69", "Guyana 35 19 69", "Haiti 16 95 75", "Honduras 38 9 27", "Hongkong 28 15 0", "Hungary 21 53 2", "Iceland 46 41 89", "India 13 9 34", "Indonesia 22 9 60", "Iran 9 28 32", "Iraq 72 82 83", "Ireland 16 97 23", "Israel 1 21 41", "Italy 69 38 63", "Ivory_Coast 25 61 21", "Jamaica 68 70 100", "Japan 30 57 36", "Jordan 2 49 7", "Kampuchea_(Cambodia) 15 38 87", "Kazakstan 12 27 116", "Kenya 76 51 76", "Korea 6 74 101", "Kuwait 53 25 4", "Kyrgyzstan 59 74 71", "Laos 44 11 98", "Latvia 20 12 47", "Lebanon 23 34 74", "Lesotho 46 52 36", "Liberia 52 49 89", "Libya 46 81 59", "Liechtenstein 62 66 26", "Lithuania 4 42 45", "Luxembourg 9 21 75", "Macao 31 8 48", "Madagascar 38 39 2", "Malawi 69 42 56", "Malaysia 10 97 63", "Maldives 8 34 72", "Mali 68 1 23", "Malta 28 66 119", "Mariana_Is 58 86 79", "Martinique 18 6 108", "Mauritius 38 3 92", "Mexico 36 32 11", "Moldova,_Republic_of 46 37 43", "Monaco 17 75 109", "Mongolia 79 67 4", "Montserrat_Is 12 57 32", "Morocco 59 16 94", "Mozambique 68 60 67", "Namibia 61 18 96", "Nauru 16 41 25", "Nepal 47 57 10", "Netheriands_Antilles 62 2 73", "Netherlands 58 14 54", "New_Zealand 35 76 40", "Nicaragua 26 26 62", "Niger 30 95 113", "Nigeria 0 30 43", "North_Korea 38 42 106", "Norway 15 75 16", "Oman 0 15 107", "Pakistan 47 0 117", "Panama 64 30 115", "Papua_New_Cuinea 64 79 93", "Paraguay 23 56 42", "Peru 31 90 14", "Philippines 10 1 74", "Poland 57 80 92", "Portugal 1 24 87", "Puerto_Rico 19 28 97", "Qatar 8 85 25", "Reunion 68 72 66", "Romania 26 1 40", "Russia 64 74 21", "Saint_Kitts_and_Nevis 37 37 30", "Saint_Lueia 51 31 34", "Saint_Vincent 79 19 78", "Samoa_Eastern 20 22 2", "Samoa_Western 51 3 48", "San_Marino 29 40 116", "Sao_Tome_and_Principe 41 53 102", "Saudi_Arabia 7 33 85", "Senegal 34 5 118", "Seychelles 77 10 37", "Sierra_Leone 48 80 68", "Singapore 27 86 57", "Slovakia 73 35 44", "Slovenia 42 56 78", "Solomon_Is 45 38 53", "Somali 26 29 24", "South_Africa 58 83 61", "Spain 5 53 47", "Sri_Lanka 50 6 5", "St.Lucia 26 67 71", "Syria 48 91 114", "Tajikstan 10 88 45", "Tanzania 40 26 85", "Thailand 78 72 51", "Togo 18 50 60", "Tonga 6 45 26", "Trinidad_and_Tobago 67 80 19", "Tunisia 52 95 80", "Turkey 61 22 37", "Turkmenistan 71 73 62", "Uganda 56 80 72", "Ukraine 35 21 34", "United_Arab_Emirates 72 33 98", "United_Kiongdom 60 93 77", "United_States_of_America 50 67 3", "Uruguay 62 56 58", "Uzbekistan 25 57 32", "Venezuela 26 91 67", "Vietnam 72 35 105", "Yemen 33 93 91", "Yugosl**ia 10 47 110", "Zaire 53 78 80", "Zambia 0 67 22", "Zimbabwe 77 81 93"};
long start = System.currentTimeMillis();
String[] rank = Medal.rankByBubble(countries);
System.out.println("耗时:" + (System.currentTimeMillis() - start));
for (int i = 0; i
耗时:12
Mongolia
Burma
Saint_Vincent
Belgium
Thailand
Zimbabwe
Seychelles
Kenya
Burundi
Australia
Ethiopia
Slovakia
Iraq
Vietnam
United_Arab_Emirates
Turkmenistan
Croatia
France
Malawi
Italy
Ascension
Reunion
Jamaica
Mozambique
Albania
Mali
Trinidad_and_Tobago
Brazil
Algeria
Cameroon
Germany
Chad
Bolivia
Papua_New_Cuinea
Russia
Panama
Greece
Liechtenstein
Uruguay
Netheriands_Antilles
Costa_Rica
Turkey
Namibia
Finland
United_Kiongdom
Benin
Cyprus
Kyrgyzstan
Ghana
Morocco
Mariana_Is
South_Africa
Belize
Netherlands
Colombia
Poland
Burkina-faso
Uganda
Afghanistan
Zaire
Kuwait
Tunisia
Canada
Liberia
Bulgaria
Saint_Lueia
Samoa_Western
United_States_of_America
Sri_Lanka
Bangladesh
Syria
Sierra_Leone
Belarus
Nepal
Pakistan
French_Guiana
Libya
Fiji
Lesotho
Iceland
Moldova,Republic_of
Guatemala
Solomon_Is
Bahrain
Anguilla
Czech_Republic
Laos
Slovenia
EI_Salvador
Sao_Tome_and_Principe
Djibouti
Tanzania
Grenada
North_Korea
Madagascar
Honduras
Mauritius
Saint_Kitts_and_Nevis
Barbados
Mexico
New_Zealand
Ukraine
Dominica_Rep.
Guyana
Senegal
Yemen
Bahamas
Peru
Cook_Is.
Macao
Niger
Japan
San_Marino
Malta
Central_African_Republic
China
Hongkong
Singapore
Venezuela
Gibraltar
St.Lucia
Brunei
Bermuda_Is.
Argentina
Somali
Nicaragua
Romania
Ivory_Coast
Uzbekistan
Congo
Paraguay
Lebanon
Indonesia
Cayman_Is.
Hungary
Guinea
Samoa_Eastern
Latvia
Gambia
Puerto_Rico
Chile
Togo
Martinique
Armenia
Monaco
Cuba
Ireland
Haiti
Nauru
Andorra
Norway
Kampuchea(Cambodia)
Azerbaijan
Georgia
Gabon
India
Montserrat_Is
Kazakstan
Antigua_and_Barbuda
Austria
French_Polynesia
Malaysia
Tajikstan
Ecuador
Yugosl**ia
Philippines
Iran
Luxembourg
Denmark
Qatar
Maldives
Saudi_Arabia
Korea
Tonga
Spain
Lithuania
Guam
Jordan
Egypt
Estonia
Portugal
Israel
Zambia
Angola
Botswana
Nigeria
Oman