Sort elements of an array in ascending order

后端 未结 5 1894
陌清茗
陌清茗 2020-12-18 12:26

I\'m trying to sort an array in ascending order. For some reason it only performs the for loop once. Why doesn\'t it keep going until everything is sorted?

It\'s for

相关标签:
5条回答
  • 2020-12-18 12:55

    Sorting array in ascending order

    private static int[] sort(int[] array) {
            int[] new_array = new int[array.length];
            int count=0;
    
            for (int i=0; i<array.length; i++) {
                for(int j=i+1; j<array.length+i;j++) {
                    if(array[i]>=array[j%array.length])
                        count++;
                }
    
                for(int loc=count; loc>0;loc--) {
                    if(new_array[loc]==0)
                    {
                        new_array[loc]=array[i];
                        break;
                    }
                }
                count=0;                            
            }
    
            return new_array;
        }
    
    0 讨论(0)
  • 2020-12-18 13:13
    public static int[] sortArray(int[] nonSortedArray) {
            int[] sortedArray = new int[nonSortedArray.length];
            int temp;
            for (int j = 0; j < nonSortedArray.length - 1; j++) {// added this for loop, think about logic why do we have to add this to make it work
    
            for (int i = 0; i < nonSortedArray.length - 1; i++) {
                if (nonSortedArray[i] > nonSortedArray[i + 1]) {
                    temp = nonSortedArray[i];
                    nonSortedArray[i] = nonSortedArray[i + 1];
                    nonSortedArray[i + 1] = temp;
                    sortedArray = nonSortedArray;
    
                }
            }
            }
            return sortedArray;
        }
    

    output:[1, 2, 3, 4, 5]

    or

    //making use of j
    
    public static int[] sortArray(int[] nonSortedArray){
        int[] sortedArray = new int[nonSortedArray.length];
        int temp;
        for (int i = 0; i <= nonSortedArray.length; i++) 
        {
            for (int j = i+1; j < nonSortedArray.length; j++)
            {
                if (nonSortedArray[i] > nonSortedArray[j]) 
                {
                    temp = nonSortedArray[i];
                    nonSortedArray[i] = nonSortedArray[j];
                    nonSortedArray[j] = temp;
                    sortedArray = nonSortedArray;
                }
            }
        }
        return sortedArray;
    }
    
    0 讨论(0)
  • 2020-12-18 13:18

    This will sort an array in ascending order

    int arr[]={33,3,4,5,1};
    Arrays.sort(arr);
    System.out.println(Arrays.toString (arr));
    

    output will:-[1,3,4,5,33]

    0 讨论(0)
  • 2020-12-18 13:21
     arr = new int[Item];
      Arrays.sort(arr);
    

    Built in function in java to sort array in asceding order.

    0 讨论(0)
  • 2020-12-18 13:21

    You are trying to sort an array using a single loop. You would be needing two loops to ensure that the elements are in sorted order.

    public static int[] sortArray(int[] nonSortedArray) 
    {
        int[] sortedArray = new int[nonSortedArray.length];
        int temp;
        for (int i = 0; i < nonSortedArray.length-1; i++) 
        {
            for (int j = i+1; j < nonSortedArray.length; j++)
            {
                if (nonSortedArray[i] > nonSortedArray[j]) 
                {
                    temp = nonSortedArray[i];
                    nonSortedArray[i] = nonSortedArray[j];
                    nonSortedArray[j] = temp;
                    sortedArray = nonSortedArray;
                }
            }
        }
        return sortedArray;
    }
    
    0 讨论(0)
提交回复
热议问题