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 + " ");
    		}
    
    	}
    
    }
    
    
  • 相关阅读:
    日志收集
    解决spawn-fcgi child exited with: 1
    confluence启动关闭
    Informatica 启动、停止工作流命令
    启动obiee
    oracle修改连接空闲自动断开
    ORA-00845: MEMORY_TARGET not supported on this system
    svn执行clean up命令时报错
    手游推广
    phonegap/cordova 升级版本
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/12711319.html
Copyright © 2011-2022 走看看