Collections vs Arrays regarding sort()

前端 未结 8 1999
北荒
北荒 2021-01-31 03:20

Collections vs Arrays regarding sort() What is the difference between these two regarding sort() method? I know Arrays\' sort() is using binary search for sort(), what about Co

8条回答
  •  伪装坚强ぢ
    2021-01-31 03:51

    Collections.sort() Operates on List Whereas Arrays.sort() Operates on an Array.

    Arrays.sort() uses Dual-Pivot Quicksort for Primitive Arrays and MergeSort for sorting array of Objects.

    Example of Collections.sort() :

     ArrayList arr = new ArrayList();
     arr.add(15);
     arr.add(10);
     arr.add(5); 
     arr.add(2); 
    
     Collections.sort(arr);
    

    Example of Arrays.sort() :

    int[] arr = new int[4]
     arr[0]=15;
     arr[1]=10;
     arr[2]=5; 
     arr[3]=2; 
    
     Arrays.sort(arr);
    

提交回复
热议问题