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

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

    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;l1){
                    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]=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

提交回复
热议问题