Why isn't Collections.sort() optimized for ArrayList, but is for LinkedList?












8















Why does Collections.sort() creates an extra object array and performs Tim sort on the array and then finally copies the sorted array back into List object? I know this call is optimized for LinkedList, but won't we lose out on performance for ArrayList?



We could have avoided 2n number of operations in converting it into an object array and adding them back to the list. I know that these extra operations wouldn't affect the Big-O of the whole sorting operation, but I believe it could've been further optimised for ArrayList.



Am I missing something here? I'm just trying to understand why the architecture is laid out as such. Thanks.



https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Collections.java#l164










share|improve this question

























  • Please PM here if the question is a possible duplicate. I will be glad to know that there is already an answer to this question.

    – Vineeth Chitteti
    Jan 13 at 10:42
















8















Why does Collections.sort() creates an extra object array and performs Tim sort on the array and then finally copies the sorted array back into List object? I know this call is optimized for LinkedList, but won't we lose out on performance for ArrayList?



We could have avoided 2n number of operations in converting it into an object array and adding them back to the list. I know that these extra operations wouldn't affect the Big-O of the whole sorting operation, but I believe it could've been further optimised for ArrayList.



Am I missing something here? I'm just trying to understand why the architecture is laid out as such. Thanks.



https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Collections.java#l164










share|improve this question

























  • Please PM here if the question is a possible duplicate. I will be glad to know that there is already an answer to this question.

    – Vineeth Chitteti
    Jan 13 at 10:42














8












8








8


1






Why does Collections.sort() creates an extra object array and performs Tim sort on the array and then finally copies the sorted array back into List object? I know this call is optimized for LinkedList, but won't we lose out on performance for ArrayList?



We could have avoided 2n number of operations in converting it into an object array and adding them back to the list. I know that these extra operations wouldn't affect the Big-O of the whole sorting operation, but I believe it could've been further optimised for ArrayList.



Am I missing something here? I'm just trying to understand why the architecture is laid out as such. Thanks.



https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Collections.java#l164










share|improve this question
















Why does Collections.sort() creates an extra object array and performs Tim sort on the array and then finally copies the sorted array back into List object? I know this call is optimized for LinkedList, but won't we lose out on performance for ArrayList?



We could have avoided 2n number of operations in converting it into an object array and adding them back to the list. I know that these extra operations wouldn't affect the Big-O of the whole sorting operation, but I believe it could've been further optimised for ArrayList.



Am I missing something here? I'm just trying to understand why the architecture is laid out as such. Thanks.



https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Collections.java#l164







java algorithm sorting timsort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 13 at 10:49









Slaw

7,80831033




7,80831033










asked Jan 13 at 10:41









Vineeth ChittetiVineeth Chitteti

713522




713522













  • Please PM here if the question is a possible duplicate. I will be glad to know that there is already an answer to this question.

    – Vineeth Chitteti
    Jan 13 at 10:42



















  • Please PM here if the question is a possible duplicate. I will be glad to know that there is already an answer to this question.

    – Vineeth Chitteti
    Jan 13 at 10:42

















Please PM here if the question is a possible duplicate. I will be glad to know that there is already an answer to this question.

– Vineeth Chitteti
Jan 13 at 10:42





Please PM here if the question is a possible duplicate. I will be glad to know that there is already an answer to this question.

– Vineeth Chitteti
Jan 13 at 10:42












1 Answer
1






active

oldest

votes


















10














You are looking at an older JDK version. At least since since JDK 1.8.0_162 Collections.sort() calls List's sort(Comparator<? super E> c). And while the default implementation creates an array from the List and sorts that array, ArrayList overrides that default implementation, and sorts the backing array directly.



Collections's sort:



public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}


List's sort:



default void sort(Comparator<? super E> c) {
Object a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}


ArrayList's sort:



public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}


In other words, this has been addressed in JDK versions newer than the one you are looking at.



You can look at the source here (thanks to eckes's link).






share|improve this answer





















  • 1





    The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

    – Slaw
    Jan 13 at 10:48











  • Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

    – Joe C
    Jan 13 at 10:56








  • 1





    @Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

    – Eran
    Jan 13 at 10:57











  • Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

    – Eran
    Jan 13 at 11:00











  • @Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

    – eckes
    Jan 13 at 11:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54168052%2fwhy-isnt-collections-sort-optimized-for-arraylist-but-is-for-linkedlist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














You are looking at an older JDK version. At least since since JDK 1.8.0_162 Collections.sort() calls List's sort(Comparator<? super E> c). And while the default implementation creates an array from the List and sorts that array, ArrayList overrides that default implementation, and sorts the backing array directly.



Collections's sort:



public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}


List's sort:



default void sort(Comparator<? super E> c) {
Object a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}


ArrayList's sort:



public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}


In other words, this has been addressed in JDK versions newer than the one you are looking at.



You can look at the source here (thanks to eckes's link).






share|improve this answer





















  • 1





    The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

    – Slaw
    Jan 13 at 10:48











  • Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

    – Joe C
    Jan 13 at 10:56








  • 1





    @Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

    – Eran
    Jan 13 at 10:57











  • Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

    – Eran
    Jan 13 at 11:00











  • @Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

    – eckes
    Jan 13 at 11:09
















10














You are looking at an older JDK version. At least since since JDK 1.8.0_162 Collections.sort() calls List's sort(Comparator<? super E> c). And while the default implementation creates an array from the List and sorts that array, ArrayList overrides that default implementation, and sorts the backing array directly.



Collections's sort:



public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}


List's sort:



default void sort(Comparator<? super E> c) {
Object a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}


ArrayList's sort:



public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}


In other words, this has been addressed in JDK versions newer than the one you are looking at.



You can look at the source here (thanks to eckes's link).






share|improve this answer





















  • 1





    The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

    – Slaw
    Jan 13 at 10:48











  • Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

    – Joe C
    Jan 13 at 10:56








  • 1





    @Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

    – Eran
    Jan 13 at 10:57











  • Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

    – Eran
    Jan 13 at 11:00











  • @Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

    – eckes
    Jan 13 at 11:09














10












10








10







You are looking at an older JDK version. At least since since JDK 1.8.0_162 Collections.sort() calls List's sort(Comparator<? super E> c). And while the default implementation creates an array from the List and sorts that array, ArrayList overrides that default implementation, and sorts the backing array directly.



Collections's sort:



public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}


List's sort:



default void sort(Comparator<? super E> c) {
Object a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}


ArrayList's sort:



public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}


In other words, this has been addressed in JDK versions newer than the one you are looking at.



You can look at the source here (thanks to eckes's link).






share|improve this answer















You are looking at an older JDK version. At least since since JDK 1.8.0_162 Collections.sort() calls List's sort(Comparator<? super E> c). And while the default implementation creates an array from the List and sorts that array, ArrayList overrides that default implementation, and sorts the backing array directly.



Collections's sort:



public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}


List's sort:



default void sort(Comparator<? super E> c) {
Object a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}


ArrayList's sort:



public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}


In other words, this has been addressed in JDK versions newer than the one you are looking at.



You can look at the source here (thanks to eckes's link).







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 13 at 11:14

























answered Jan 13 at 10:44









EranEran

283k37458546




283k37458546








  • 1





    The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

    – Slaw
    Jan 13 at 10:48











  • Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

    – Joe C
    Jan 13 at 10:56








  • 1





    @Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

    – Eran
    Jan 13 at 10:57











  • Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

    – Eran
    Jan 13 at 11:00











  • @Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

    – eckes
    Jan 13 at 11:09














  • 1





    The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

    – Slaw
    Jan 13 at 10:48











  • Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

    – Joe C
    Jan 13 at 10:56








  • 1





    @Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

    – Eran
    Jan 13 at 10:57











  • Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

    – Eran
    Jan 13 at 11:00











  • @Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

    – eckes
    Jan 13 at 11:09








1




1





The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

– Slaw
Jan 13 at 10:48





The link to the source file (supposedly for JDK8, though I don't fully understand the repo's organization) does show Collections.sort using the default List.sort implementation directly.

– Slaw
Jan 13 at 10:48













Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

– Joe C
Jan 13 at 10:56







Not what I see; I see sorting done on the toArray() on line 230 (perhaps you're looking at the synchronized wrapper?). That said, OpenJDK11 is showing what you're saying in its source.

– Joe C
Jan 13 at 10:56






1




1





@Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

– Eran
Jan 13 at 10:57





@Slaw the linked source file seems to be jdk8-b132. My answer contains code of JDK 1.8.0_162

– Eran
Jan 13 at 10:57













Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

– Eran
Jan 13 at 11:00





Here's the JDK9 version - hg.openjdk.java.net/jdk9/jdk9/jdk/file/d966fc5a7be5/src/…. I'll link to JDK 1.8.0_162 if I can find it

– Eran
Jan 13 at 11:00













@Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

– eckes
Jan 13 at 11:09





@Eran hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/c00bdbbd9a77/src/share/…

– eckes
Jan 13 at 11:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54168052%2fwhy-isnt-collections-sort-optimized-for-arraylist-but-is-for-linkedlist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mario Kart Wii

The Binding of Isaac: Rebirth/Afterbirth

What does “Dominus providebit” mean?