For loop 如何使用扫描仪对循环使用2进行选择排序?阅读详情

For loop 如何使用扫描仪对循环使用2进行选择排序?阅读详情,for-loop,java.util.scanner,bubble-sort,selection-sort,For Loop,Java.util.scanner,Bubble Sort,Selection Sort,这就是我做泡泡排序的方式 import java.util.*; class bubblesort { public static void calc() { int i , j,temp; int a[]=new int[10]; Scanner sc=new Scanner(System.in);

这就是我做泡泡排序的方式

 import java.util.*;
        class bubblesort
        {
            public static void calc()
            {
                int i , j,temp;
                int a[]=new int[10];
                Scanner sc=new Scanner(System.in);
                for(i=0;i<10;i++)
                {
                    System.out.println("Enter Numbers");
                    a[i]=sc.nextInt();
                }
                for(i=0;i<9;i++)
                {
                    for(j=0;j<9-i;j++)
                    {
                        if(a[j]>a[j+1])
                        {
                            temp=a[j];
                            a[j]=a[j+1];
                            a[j+1]=temp;
                        }
                    }
                }
                for(j=0;j<10;j++)
                {
                    System.out.println("Sorted array " +a[j]);
                }
            }
        }

我也发现了很多做选择排序的方法,但我当时还不能理解。有人能用类似的语法、代码和scanner在冒泡排序和选择排序中发布更改吗。

这是我发现与上面给出的语法非常相似的语法

import java.util.*;
class selctionsort
{
    public static void calc()
    {
        int i , j;
        int m;
        int n ;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter how many Numbers to be sorted");
        n=sc.nextInt();
        int a[]=new int [n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter The Numbers");
            a[i]=sc.nextInt();
        }
        for(i=0;i<n;i++)
        {
            for (j=i;j<n;j++)
            {
                if(a[i]>a[j])
                {
                    m=a[i];
                    a[i]=a[j];
                    a[j]=m;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            System.out.println("Sorted array is " +a[i]);
        }
    }
}

m用作存储原始值的临时变量