Sorting (names) using Merge Sort

前端 未结 3 505
借酒劲吻你
借酒劲吻你 2020-12-20 20:31

having problem sorting repeated Strings,

and here\'s my code..

i successfully sorted the first array but in the second (with repeated strings) it seems not

相关标签:
3条回答
  • 2020-12-20 20:45

    Change

    if (names.length > 2) {
    

    with

    if (names.length >= 2) {
    

    output

    Ara
    Bubbles
    Chan
    Cristine
    Darryl
    Inasal
    Jollibee.
    Kitty
    Kring
    Madonna
    Maria
    Matang
    Miley
    Minnie
    Panda
    Rose
    Soliel
    Zoom-zoom
    
    0 讨论(0)
  • 2020-12-20 20:58

    Why not just concatenate the two arrays (String ClassOne and ClassTwo) into one and then call a MergeSort on one array? Your process makes the program more ambiguous in my opinion.

    0 讨论(0)
  • 2020-12-20 21:00

    Just change this:

    if (names.length > 2)
    

    to

    if (names.length > 1)
    

    mergeSort runs recursively spliting the array into two halves, then merges them and return back up the call chain. When the length of the array passed to mergeSort is <= 1 it considers the array sorted, this is called the base case.

    0 讨论(0)
提交回复
热议问题