Java simple array sort of bubble method

  • 2020-04-01 04:16:53
  • OfStack

This article illustrates a simple array sort in Java (bubble method). Share with you for your reference, as follows:


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]; //Customize the array length
 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); //Call the array sort method
 }
 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){ //Bubble sort
 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;
  }
 }
 }
}

I hope this article has been helpful to you in Java programming.


Related articles: