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

后端 未结 12 2626
滥情空心
滥情空心 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:26

    Simple way :

    int a[]={6,2,5,1};
                System.out.println(Arrays.toString(a));
                 int temp;
                 for(int i=0;i<a.length-1;i++){
                     for(int j=0;j<a.length-1;j++){
                         if(a[j] > a[j+1]){   // use < for Descending order
                             temp = a[j+1];
                             a[j+1] = a[j];
                             a[j]=temp;
                         }
                     }
                 }
                System.out.println(Arrays.toString(a));
    
        Output:
        [6, 2, 5, 1]
        [1, 2, 5, 6]
    
    0 讨论(0)
  • 2021-02-14 01:29

    Bubble sort can be used here:

     //Time complexity: O(n^2)
    public static int[] bubbleSort(final int[] arr) {
    
        if (arr == null || arr.length <= 1) {
            return arr;
        }
    
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length - i; j++) {
                if (arr[j - 1] > arr[j]) {
                    arr[j] = arr[j] + arr[j - 1];
                    arr[j - 1] = arr[j] - arr[j - 1];
                    arr[j] = arr[j] - arr[j - 1];
                }
            }
        }
    
        return arr;
    }
    
    0 讨论(0)
  • 2021-02-14 01:30

    Simple sorting algorithm Bubble sort:

    public static void main(String[] args) {
        int[] arr = new int[] { 6, 8, 7, 4, 312, 78, 54, 9, 12, 100, 89, 74 };
    
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int tmp = 0;
                if (arr[i] > arr[j]) {
                    tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-14 01:31

    This will surely help you.

    int n[] = {4,6,9,1,7};
    
        for(int i=n.length;i>=0;i--){
            for(int j=0;j<n.length-1;j++){
                if(n[j] > n[j+1]){
                    swapNumbers(j,j+1,n);
                }
            }
    
        }
        printNumbers(n);
    }
    private static void swapNumbers(int i, int j, int[] array) {
    
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    
    private static void printNumbers(int[] input) {
    
        for (int i = 0; i < input.length; i++) {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-14 01:41

    int[] arr = {111, 111, 110, 101, 101, 102, 115, 112};
    
    /* for ascending order */
    
    System.out.println(Arrays.toString(getSortedArray(arr)));
    /*for descending order */
    System.out.println(Arrays.toString(getSortedArray(arr)));
    
    private int[] getSortedArray(int[] k){  
    
            int localIndex =0;
            for(int l=1;l<k.length;l++){
                if(l>1){
                    localIndex = l;
                    while(true){
                        k = swapelement(k,l);
                        if(l-- == 1)
                            break;
                    }
                    l = localIndex;
                }else
                    k = swapelement(k,l);   
            }
            return k;
        }
    
        private int[] swapelement(int[] ar,int in){
            int temp =0;
            if(ar[in]<ar[in-1]){
                temp = ar[in];
                ar[in]=ar[in-1];
                ar[in-1] = temp;
            }
    
            return ar;
        }
    
    private int[] getDescOrder(int[] byt){
            int s =-1;
            for(int i = byt.length-1;i>=0;--i){
                  int k = i-1;
                  while(k >= 0){
                      if(byt[i]>byt[k]){
                          s = byt[k];
                          byt[k] = byt[i];
                          byt[i] = s;
                      }
                      k--;
                  }
               }
            return byt;
        }
    


    output:-
    ascending order:- 101, 101, 102, 110, 111, 111, 112, 115


    descending order:- 115, 112, 111, 111, 110, 102, 101, 101

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