有人可以解释这段代码中的最后一个方法吗?
Can somebody explain the last method in this code?
我是一名初级程序员,我不理解这段代码中的最后一个方法(mysort
)。它实际上是冒泡排序的一个例子。
import java.util.*;
public class Sort
{
public static void main(String[] args)
{
int[] a = {22, 55, 44, 11, 33};
disp(a);
mysort(a);
disp(a);
}
public static void disp(int[] x)
{
for(int e : x)
{
System.out.print(e + "\t");
}
System.out.println();
}
public static void mysort(int[] a)
{
int l = a.length;
for(int i = 0; i < l - 1; i++)
{
for(int j = 0; j < l - 1 - i; j++)
{
if(a[j + 1] < a[j])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
以上代码的解释:
让我们使用数字数组“5 1 4 2 8”,并使用冒泡排序从小到大对数组进行排序。在每一步中,都会比较以粗体书写的元素。需要三遍。
第一关:
( 5 1 4 2 8 ) 到 ( 1 5 4 2 8 ),在这里,算法比较前两个元素,并交换,因为 5 > 1.
( 1 5 4 2 8 ) 到 ( 1 4 5 2 8 ), 交换 5 > 4
( 1 4 5 2 8 ) 到 ( 1 4 2 5 8 ), 交换 5 > 2
( 1 4 2 5 8 ) 到 ( 1 4 2 5 8 ),现在,因为这些元素已经有序 (8 > 5),所以算法不会交换它们。
第二遍:
( 1 4 2 5 8 ) 到 ( 1 4 2 5 8 )
( 1 4 2 5 8 ) 到 ( 1 2 4 5 8 ), 交换 4 > 2
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
现在,数组已经排序了,但是算法不知道是否完成了。该算法需要一个完整的通道而不进行任何交换才能知道它已排序。
第三遍:
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
查看此 link 以获得更多解释:http://en.wikipedia.org/wiki/Bubble_sort
让我们分解每个方法。代码的第一部分要理解的是最里面的块:
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
如果我们运行数组{1,2,3,4,5}
上的上述语句,以及j=2
,我们将有:
int temp = a[2] --> 3;
a[2] = a[2+1] --> a = {1,2,4,4,5};
a[2+1] = temp --> a = {1,2,4,3,5};
我们现在看到这三行交换了 a[j]
和 a[j+1]
的元素。
现在我们来看看for循环。内部循环:for(int j = 0; j < l - 1 - i; j++)
从数组的开头循环到 l-1-i
。在每个索引处,它会询问 if(a[j+1] < a[j])
,意思是 "is the element at index j+1
smaller than the element at index j
",或者更具体地说,"Should the elements at j+1
and j
be swapped?"
外层循环只是运行使用索引变量i
遍历整个数组。将两个循环放在一起,我们看到 j
将首先遍历没有最后一个索引的整个数组,然后是没有最后两个索引的整个数组,然后没有最后三个索引,等等。例如,如果 l = 10
, j
将采用以下值:
0 1 2 3 4 5 6 7 8 //i = 0, l - 1 - i = 9
0 1 2 3 4 5 6 7 //i = 1, l - 1 - i = 8
0 1 2 3 4 5 6 //i = 2, l - 1 - i = 7
...
0
所以这两个 for 循环一起 运行 对数组的某些部分 l
次,进行交换。
以这种方式形成循环的原因是在每次 j=0...l-1-i
迭代之后,已知最后一个元素位于其排序位置,因此我们不必再次检查它。这个理由和更多在这里:http://en.wikipedia.org/wiki/Bubble_sort
mysort
为数组 for(int i = 0; i < l - 1; i++)
.
中的每个元素循环遍历给定数组 for(int j = 0; j < l - 1 - i; j++)
一次
if(a[j + 1] < a[j])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
上面只是交换了两个整数,所以较大的排在第二位。
开始:1753
第 1 轮:1753(无交换)
第 1 次:1573(交换)
第 1 轮:1537(无交换)
第 2 轮:1537(无交换)
传递 2:1357(交换)
传递 2:1357(无交换)
第 3 轮:1357(无交换)
第 3 轮:1357(无交换)
第 3 轮:1357(无交换)
完成:1357
我是一名初级程序员,我不理解这段代码中的最后一个方法(mysort
)。它实际上是冒泡排序的一个例子。
import java.util.*;
public class Sort
{
public static void main(String[] args)
{
int[] a = {22, 55, 44, 11, 33};
disp(a);
mysort(a);
disp(a);
}
public static void disp(int[] x)
{
for(int e : x)
{
System.out.print(e + "\t");
}
System.out.println();
}
public static void mysort(int[] a)
{
int l = a.length;
for(int i = 0; i < l - 1; i++)
{
for(int j = 0; j < l - 1 - i; j++)
{
if(a[j + 1] < a[j])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
以上代码的解释:
让我们使用数字数组“5 1 4 2 8”,并使用冒泡排序从小到大对数组进行排序。在每一步中,都会比较以粗体书写的元素。需要三遍。
第一关: ( 5 1 4 2 8 ) 到 ( 1 5 4 2 8 ),在这里,算法比较前两个元素,并交换,因为 5 > 1.
( 1 5 4 2 8 ) 到 ( 1 4 5 2 8 ), 交换 5 > 4
( 1 4 5 2 8 ) 到 ( 1 4 2 5 8 ), 交换 5 > 2
( 1 4 2 5 8 ) 到 ( 1 4 2 5 8 ),现在,因为这些元素已经有序 (8 > 5),所以算法不会交换它们。
第二遍: ( 1 4 2 5 8 ) 到 ( 1 4 2 5 8 )
( 1 4 2 5 8 ) 到 ( 1 2 4 5 8 ), 交换 4 > 2
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
现在,数组已经排序了,但是算法不知道是否完成了。该算法需要一个完整的通道而不进行任何交换才能知道它已排序。
第三遍: ( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
( 1 2 4 5 8 ) 到 ( 1 2 4 5 8 )
查看此 link 以获得更多解释:http://en.wikipedia.org/wiki/Bubble_sort
让我们分解每个方法。代码的第一部分要理解的是最里面的块:
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
如果我们运行数组{1,2,3,4,5}
上的上述语句,以及j=2
,我们将有:
int temp = a[2] --> 3;
a[2] = a[2+1] --> a = {1,2,4,4,5};
a[2+1] = temp --> a = {1,2,4,3,5};
我们现在看到这三行交换了 a[j]
和 a[j+1]
的元素。
现在我们来看看for循环。内部循环:for(int j = 0; j < l - 1 - i; j++)
从数组的开头循环到 l-1-i
。在每个索引处,它会询问 if(a[j+1] < a[j])
,意思是 "is the element at index j+1
smaller than the element at index j
",或者更具体地说,"Should the elements at j+1
and j
be swapped?"
外层循环只是运行使用索引变量i
遍历整个数组。将两个循环放在一起,我们看到 j
将首先遍历没有最后一个索引的整个数组,然后是没有最后两个索引的整个数组,然后没有最后三个索引,等等。例如,如果 l = 10
, j
将采用以下值:
0 1 2 3 4 5 6 7 8 //i = 0, l - 1 - i = 9
0 1 2 3 4 5 6 7 //i = 1, l - 1 - i = 8
0 1 2 3 4 5 6 //i = 2, l - 1 - i = 7
...
0
所以这两个 for 循环一起 运行 对数组的某些部分 l
次,进行交换。
以这种方式形成循环的原因是在每次 j=0...l-1-i
迭代之后,已知最后一个元素位于其排序位置,因此我们不必再次检查它。这个理由和更多在这里:http://en.wikipedia.org/wiki/Bubble_sort
mysort
为数组 for(int i = 0; i < l - 1; i++)
.
for(int j = 0; j < l - 1 - i; j++)
一次
if(a[j + 1] < a[j])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
上面只是交换了两个整数,所以较大的排在第二位。
开始:1753
第 1 轮:1753(无交换)
第 1 次:1573(交换)
第 1 轮:1537(无交换)
第 2 轮:1537(无交换)
传递 2:1357(交换)
传递 2:1357(无交换)
第 3 轮:1357(无交换)
第 3 轮:1357(无交换)
第 3 轮:1357(无交换)
完成:1357