简体   繁体   English

这是哪个类别/类型的排序算法?

[英]which category/type of sorting algorithm is this?

This might be a dumb question.这可能是一个愚蠢的问题。
However, I am a little confused about categorizing this sort但是,我对这种分类有点困惑

here I am comparing the first element with all elements and then swapping with greater.在这里,我将第一个元素与所有元素进行比较,然后与更大的元素进行交换。
then picking the second element compared with all next and then swapping with greater然后选择第二个元素与所有下一个进行比较,然后与更大的交换

class Sort{ public static void main(String[] arg) { System.out.println("Sort"); int[] unsorted = {2,5,2,5,3,3,6,2,7,3,1,84,3}; for(int i=0;i<unsorted.length;i++) { for(int j=i;j<unsorted.length;j++) { if(unsorted[i] > unsorted[j]) { swap(unsorted,i,j); } } } System.out.println(Arrays.toString(unsorted)); } public static void swap(int[] arr,int i,int j) { int tmp = arr[i]; arr[i]=arr[j]; arr[j]=tmp; } }

That's selection sort.这就是选择排序。 You can tell because after iteration i of the outer loop, all elements of the array up to index i are in their final positions.你可以看出,因为在外部循环的迭代i之后,数组中直到索引i的所有元素都在它们的最终位置。 The only nonstandard bit is that the active element is swapped each time a smaller element is found;唯一的非标准位是每次找到较小的元素时都会交换活动元素; this is not the case in the “classic” selection sort but is common in actual implementations (particularly in embedded systems).这在“经典”选择排序中不是这种情况,但在实际实现中很常见(特别是在嵌入式系统中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM