Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解数组中元素排序的代码_Java_Sorting - Fatal编程技术网

Java 理解数组中元素排序的代码

Java 理解数组中元素排序的代码,java,sorting,Java,Sorting,请解释第15-19行的代码如何帮助实现排序 public static void main( String[] args ) throws java.io.IOException { FastScanner scanner = new FastScanner(8000000); Writer writer = new Writer(System.out); int T = scanner.nextInt(); int[] arr = new int[1

请解释第15-19行的代码如何帮助实现排序

public static void main( String[] args ) throws java.io.IOException
{
    FastScanner scanner = new FastScanner(8000000);
    Writer writer = new Writer(System.out);      
    int T = scanner.nextInt();
    int[] arr = new int[1000000];     
    for(int index=0; index<T ;index++)
    {
        arr[scanner.nextInt()]++;
    }

   for( int index = 0; index<1000000 ;index++ )
    {
        for(int j=0;j<arr[index];j++)
        writer.writeln(index);
    }
    writer.flush();
}
publicstaticvoidmain(字符串[]args)抛出java.io.IOException
{
FastScanner=新的FastScanner(8000000);
Writer Writer=新的Writer(System.out);
int T=scanner.nextInt();
int[]arr=新int[1000000];

对于(int index=0;index实际上,数组没有排序,但是打印会按排序的顺序写入数组的元素。举个例子,我们在数组中输入6个元素(2、3、3、5、8和9),看看每个迭代中会发生什么:

T = 6

arr[0] = 0
arr[1] = 0
arr[2] = 1
arr[3] = 2
arr[4] = 0
arr[5] = 1
arr[6] = 0
arr[7] = 0
arr[8] = 1
arr[9] = 1

index=0
j=0; j<0; j++

index=1
j=0; j<0; j++

index=2
j=0; j<1; j++
"2"

index=3
j=0; j<2; j++
"3" "3"

index=4
j=0; j<0; j++

index=5
j=0; j<1; j++
"5"

index=6
j=0; j<0; j++

index=7
j=0; j<0; j++

index=8
j=0; j<1; j++
"8"

index=9
j=0; j<1; j++
"9"

Output: 2 3 3 5 8 9
T=6
arr[0]=0
arr[1]=0
arr[2]=1
arr[3]=2
arr[4]=0
arr[5]=1
arr[6]=0
arr[7]=0
arr[8]=1
arr[9]=1
索引=0

j=0;j这种排序方式称为

“分类”正在这里进行

   10     for(int index=0; index<T ;index++)
   11    {
   12         arr[scanner.nextInt()]++;
   13     }

示例:
输入1:
2,3,5,0,4,0,3,5

数组看起来像1:
{2,0,1,2,1,2}

打印索引发生的时间:
0,0,1,2,3,3,4,5,5



1:假设所有输入值都在0到5的范围内。因此数组的大小是6。

第15-19行根本没有排序!它们只是打印排序后的数组!你能给这个代码一些上下文吗?如果我不得不猜测,我会说FastScanner正在幕后进行排序
   15     for( int index = 0; index<1000000 ;index++ )
   16     {
   17         for(int j=0;j<arr[index];j++)
   18             writer.writeln(index);
   19     }