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

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

    here is the Sorting Simple Example try it

    public class SortingSimpleExample {
    
        public static void main(String[] args) {
            int[] a={10,20,1,5,4,20,6,4,2,5,4,6,8,-5,-1};
                  a=sort(a);
            for(int i:a)
                  System.out.println(i);
    }
        public static int[] sort(int[] a){
    
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a.length;j++){
                    int temp=0;
                    if(a[i]<a[j]){
                        temp=a[j];                  
                        a[j]=a[i];                  
                        a[i]=temp;      
                      }         
                    }
            }
            return a;
    
        }
    }
    
    0 讨论(0)
  • 2021-02-14 01:45
    class Sort 
    {
        public static void main(String[] args) 
        {
        System.out.println("Enter the range");
        java.util.Scanner sc=new java.util.Scanner(System.in);
        int n=sc.nextInt();
        int arr[]=new int[n];
        System.out.println("Enter the array values");
        for(int i=0;i<=n-1;i++)
        {
            arr[i]=sc.nextInt();
    
        }
        System.out.println("Before sorting array values are");
         for(int i=0;i<=n-1;i++)
        {
         System.out.println(arr[i]);
           }
        System.out.println();
        for(int pass=1;pass<=n;pass++)
        {
        for(int i=0;i<=n-1;i++)
        {
            if(i==n-1)
            {
                break;
            }
            int temp;
            if(arr[i]>arr[i+1])
            {
    
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        }
    
        System.out.println("After sorting array values are");
           for(int i=0;i<=n-1;i++)
        {
         System.out.println(arr[i]);
           }
    
    }
    }    
    
    0 讨论(0)
  • 2021-02-14 01:51

    Here is one simple solution

    public static void main(String[] args) {        
            //Without using Arrays.sort function
            int i;
            int nos[] = {12,9,-4,-1,3,10,34,12,11};
            System.out.print("Values before sorting: \n");
            for(i = 0; i < nos.length; i++)
                System.out.println( nos[i]+"  ");               
            sort(nos, nos.length);
            System.out.print("Values after sorting: \n");       
            for(i = 0; i <nos.length; i++){
                System.out.println(nos[i]+"  ");
            }
        }
    
        private static void sort(int nos[], int n) {
         for (int i = 1; i < n; i++){
              int j = i;
              int B = nos[i];
              while ((j > 0) && (nos[j-1] > B)){
                nos[j] = nos[j-1];
                j--;
              }
              nos[j] = B;
            }
        }
    

    And the output is:

    Values before sorting:

    12
    9
    -4
    -1
    3
    10
    34
    12
    11

    Values after sorting:

    -4
    -1
    3
    9
    10
    11
    12
    12
    34

    0 讨论(0)
  • 2021-02-14 01:51
    int x[] = { 10, 30, 15, 69, 52, 89, 5 };
        int max, temp = 0, index = 0;
        for (int i = 0; i < x.length; i++) {
            int counter = 0;
            max = x[i];
            for (int j = i + 1; j < x.length; j++) {
    
                if (x[j] > max) {
                    max = x[j];
                    index = j;
                    counter++;
                }
    
            }
            if (counter > 0) {
                temp = x[index];
                x[index] = x[i];
                x[i] = temp;
            }
        }
        for (int i = 0; i < x.length; i++) {
            System.out.println(x[i]);
        }
    
    0 讨论(0)
  • 2021-02-14 01:51

    Array Sorting without using built in functions in java ......just make new File unsing this name -> (ArraySorting.java) ..... Run the Project and Enjoy it !!!!!

        import java.io.*;
        import java.util.Arrays;
        import java.util.Scanner;
        public class ArraySorting 
        {
        public static void main(String args[])
        {
          int temp=0;   
          Scanner user_input=new Scanner(System.in);
           System.out.println("enter Size of Array...");
        int Size=user_input.nextInt();
    
        int[] a=new int[Size];
        System.out.println("Enter element Of an Array...");
        for(int j=0;j<Size;j++)
        {
            a[j]=user_input.nextInt();
        }     
        for(int index=0;index<a.length;index++)
        {
            for(int j=index+1;j<a.length;j++)
            {
                 if(a[index] > a[j] ) 
                 {
                     temp = a[index];
                     a[index] = a[j];
                     a[j] = temp;
                 }
            }
        }
        System.out.print("Output is:- ");
        for(int i=0;i<a.length;i++)
        {
            System.out.println(a[i]);
        }
    
    }
    }
    
    0 讨论(0)
  • 2021-02-14 01:52

    I would recommend looking at Selection sort or Insertion sort if you aren't too worried about performance. Maybe that will give you some ideas.

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