34.json 2.3 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
   "question_id": 7455237,
   "question_title": "数组排序",
   "question_content": "<p>编写一个JavaApplication程序,将随机生成的无序数组使用冒泡排序,将这个混乱的数组变成一个从小到大排列的有序的数组并输出。</p>",
   "difficulty": "简单",
   "answer_id": 53438050,
   "answer_content": "<p>其实就是个冒泡排序呗</p>\n\n<pre>\n<code class=\"language-java\">class demo_sort {\n        public static void main(String[] args) {\n            //冒泡排序算法\n            int[] numbers&#61;new int[]{1,5,8,2,3,9,4};\n            //需进行length-1次冒泡\n            for(int i&#61;0;i&lt;numbers.length-1;i&#43;&#43;)\n            {\n                for(int j&#61;0;j&lt;numbers.length-1-i;j&#43;&#43;)\n                {\n                    if(numbers[j]&gt;numbers[j&#43;1])\n                    {\n                        int temp&#61;numbers[j];\n                        numbers[j]&#61;numbers[j&#43;1];\n                        numbers[j&#43;1]&#61;temp;\n                    }\n                }\n            }\n            System.out.println(&#34;从小到大排序后的结果是:&#34;);\n            for(int i&#61;0;i&lt;numbers.length;i&#43;&#43;)\n                System.out.print(numbers[i]&#43;&#34; &#34;);\n        }\n    }</code></pre>\n\n<p> </p>\n",
   "tag_name": "java",
   "java": "class demo_sort {\n\tpublic static void main(String[] args) {\n\t\tint[] numbers = new int[] { 1, 5, 8, 2, 3, 9, 4 };\n\t\tfor (int i = 0; i < numbers.length - 1; i++) {\n\t\t\tfor (int j = 0; j < numbers.length - 1 - i; j++) {\n\t\t\t\tif (numbers[j] > numbers[j + 1]) {\n\t\t\t\t\tint temp = numbers[j];\n\t\t\t\t\tnumbers[j] = numbers[j + 1];\n\t\t\t\t\tnumbers[j + 1] = temp;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tSystem.out.println(\"从小到大排序后的结果是:\");\n\t\tfor (int i = 0; i < numbers.length; i++)\n\t\t\tSystem.out.print(numbers[i] + \" \");\n\t}\n}",
   "topic_link": "https://bbs.csdn.net/topics/600470289",
   "status": 1,
   "keywords": "图算法,算法高阶,最小生成树,最小生成树的形成",
   "license": "csdn.net",
   "notebook": {
      "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/34.ipynb?type=file"
   },
   "notebook_enable": 1
}