简体   繁体   中英

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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