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

后端 未结 12 2603
滥情空心
滥情空心 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: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]);
           }
    
    }
    }    
    

提交回复
热议问题