zoukankan      html  css  js  c++  java
  • 冒泡排序

    冒泡排序的基本思想是:每次比较两个相邻的元素,如果它们的顺序错误就把它们交换 过来。

    冒泡排序的核心部分是双重嵌套循环。所以冒泡排序的时间复杂度是 O(N^2)。这是一个非常高的时间复杂度。这也是冒泡排序法的一个缺点。



    题目:

    现在分别有 5 个人的名字和分数:huhu 5 分、haha 3 分、xixi 5 分、hengheng 2 分和 gaoshou 8 分。请按照分数从高到低,输出他们的名字。即应该输出 gaoshou、huhu、xixi、haha、hengheng。
    
    package _4_9_test;
    
    import java.util.Scanner;
    
    import javax.lang.model.element.NestingKind;
    import javax.xml.ws.handler.MessageContext.Scope;
    
    import _12_26_test.thirteen;
    
    public class EightTwo {
    
    	public static class Student {
    		String name;
    		int score;
    	}
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("输入学生人数:");
    		int n = scanner.nextInt();
    
    		Student stu[] = new Student[n];
    		Student temp = new Student();
    
    		for (int i = 0; i < n; i++) {
    			// 这里的每个数组对象都要实例化
    			stu[i] = new Student();
    			stu[i].name = scanner.next();
    			stu[i].score = scanner.nextInt();
    		}
    
    		// 冒泡排序法
    		boolean flag = false;
    		while (!flag) {
    			flag = true;
    			for (int i = 0; i < stu.length - 1; i++) {
    				if (stu[i].score > stu[i + 1].score) {
    					temp = stu[i + 1];
    					stu[i + 1] = stu[i];
    					stu[i] = temp;
    					flag = false;
    				}
    			}
    		}
    
    		for (int i = stu.length - 1; i >= 0; i--) {
    			System.out.print(stu[i].name + " ");
    		}
    
    	}
    
    }
    
    
  • 相关阅读:
    Ubuntu 装JDK
    U盘文件夹被病毒隐藏,且不能取消解决办法
    wireshark: there are no interfaces on which a capture can be done
    [转]Ubuntu 常用快捷键10个
    恢复被win7覆盖的Ubuntu Grub
    U盘安装Win7 64位
    荣耀3X畅玩版狙击红米note!
    Secret and Whisper
    360 chrome不能登录Google账户
    周鸿祎仍想做手机
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/12711319.html
Copyright © 2011-2022 走看看