这篇文章主要介绍了Java简单数组排序,实例分析了基于冒泡法实现数组排序的相关技巧,简单实用,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Java简单数组排序(冒泡法)。分享给大家供大家参考,具体如下:
- import java.util.Scanner;
- public class testArray {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int Max=0;
- int[] score = new int[5]; //自定义数组长度
- System.out.println("please input five numbers:");
- for(int i=0; i< score.length; i++){
- score[i] = input.nextInt();
- }
- for(int j=0; j<score.length-1; j++){
- swap(score); //调用数组排序方法
- }
- System.out.println("########## the result: ###########");
- for(int i=0; i<score.length; i++){
- System.out.print(score[i]+"/t");
- }
- }
- public static void swap(int[] arr){ //冒泡法排序
- for(int i=0; i<arr.length-1; i++){
- if(arr[i]>arr[i+1]){
- int temp = arr[i];
- arr[i] = arr[i+1];
- arr[i+1] = temp;
- }
- }
- }
- }
希望本文所述对大家Java程序设计有所帮助。
新闻热点
疑难解答
图片精选