新人は暇だから大丈夫だ,牛人はれんがをたたいてはいけない.

1743 ワード

import java.util.Scanner;

public class SortNum {	
	public static void main(String[] args)
	{
		 int init=0,count=0;
		
		System.out.print(" :");
		init = new Scanner(System.in).nextInt();
		int[] num =new int[init];
		
		while(count<init)
		{
			System.out.print(" "+(count+1)+" :");
			num[count] = new Scanner(System.in).nextInt();
			count++;
		}
		sortNum1(num);
		sortNum2(num);
	}

	public static void sortNum1(int[] nums)
	{
		int temp=0;
		for(int i = nums.length-1;i>=1;i--)
		{
			for(int j=0;j<i;j++)
			{		
				if(nums[j]>nums[j+1])
				{
					temp = nums[j];
					nums[j]=nums[j+1];
					nums[j+1]=temp;
				}
				m1++;
			}
		}
		System.out.println(" :");
		for(int i = 0;i<=nums.length-1;i++)
		{
			System.out.print(nums[i]+"\t");
		}
		System.out.println(" :"+m1);
	}
	
	public static void sortNum2(int[] nums)
	{
		int temp=0;
		for(int i = 1;i<=nums.length;i++)
		{
			for(int j=0;j<nums.length-1;j++)
			{		
				if(nums[j]>nums[j+1])
				{
					temp = nums[j];
					nums[j]=nums[j+1];
					nums[j+1]=temp;
				}
				m2++;
			}
		}
		System.out.println(" :");
		for(int i = 0;i<=nums.length-1;i++)
		{
			System.out.print(nums[i]+"\t");
		}
		System.out.println(" :"+m2);
	}
	private static int m1 = 0;
	private static int m2 = 0;
}