Java 常见笔试程序题——不定期更新

1.冒泡排序

package com.xue.boke;

import java.util.Arrays;
import java.util.Scanner;
/**
 * 2018/12/8
 * @author xqp
 *按从大到小将输入的值冒泡排序:
 */
public class Sort {
	public static void sort(){
		Scanner input = new Scanner(System.in);
		int sort[] = new int[10];
		int temp;
		System.out.println("请输入10以逗号结尾的数据例如1,2,3...:");
		String s1 = input.nextLine();
		String[] s2 = s1.split(",");
		for(int i = 0;i< sort.length;i++){
			sort[i] = Integer.parseInt(s2[i]);
		}
		for(int i = 0;i < sort.length - 1;i++){
			for(int j = 0;j<sort.length-i-1;j++){
				if(sort[j]<sort[j+1]){
					temp = sort[j];
					sort[j] = sort[j+1];
					sort[j+1] = temp;
					
				}
			}
		}
		System.out.println("排序后的顺序为:");
		System.out.println(Arrays.toString(sort));
		for(int i = 0;i<sort.length;i++){
			System.out.print(sort[i]+" ");
		}
	}
	public static void main(String[] args) {
		sort();
	}
}

结果:

猜你喜欢

转载自blog.csdn.net/weixin_43545004/article/details/84890115
今日推荐