Java : Sort integer array without using Arrays.sort()

后端 未结 12 2623
滥情空心
滥情空心 2021-02-14 00:59

This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I \'do my homework\' and I\'m not just being lazy asking someon

12条回答
  •  梦谈多话
    2021-02-14 01:41

    You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:

    Instead of:

     orderedNums[greater]=tenNums[indexL];
    

    you need to do this:

    while (orderedNums[greater] == tenNums[indexL]) {
         greater++;
    }
    orderedNums[greater] = tenNums[indexL];
    

    This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.

    Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need to initiate your sorted array with an especial number that you sure is not in your list e.g: Integer.MAX_VALUE

提交回复
热议问题