why is first half of array not sorting in ascending and second half in decending also printing the array giving different result in different segment?

Viewed 24
  static void ascending_sort(int arr[],int lb,int ub)
  {  
    for(int i=lb; i<ub ; i++)
    {
       for(int j=lb; j<ub-i-1; j++)
       {
          if(arr[j]>arr[j+1])
          {
             int temp=arr[j];
             arr[j]=arr[j+1];
             arr[j+1]=temp;
          }
        }
     }
   
          
   }
   static void descending_sort(int arr[],int lb,int ub)
   {
      for(int i=lb; i<ub ; i++)
      {
         for(int j=lb; j<ub-i-1; j++)
         {
            if(arr[j]<arr[j+1])
            {
               int temp=arr[j];
               arr[j]=arr[j+1];
               arr[j+1]=temp;
            }
         }
      }
   }
   static void display(int arr[],int n)
   {
      System.out.println("The array displaying in the display method  after opeartion...");
      
      for(int i=0;i<n;i++)
      {
         System.out.println(arr[i]);
      }
   }
   public static void main(String args[])
   {
       //int arr[]=new int[5];
       int  arr[]={49,80,2,56,10,94,40,12,25,60};
             
             //Sort ob=new Sort();
             
       System.out.println("First part  when displaying in main function..");
       ascending_sort(arr,0,5);
       for(int i=0;i<5;i++)
       {
           System.out.println(arr[i]);
       }
             
             
       descending_sort(arr, 5,10);
       System.out.println("second part when displaying in main function..");
       for(int i=5;i<10;i++)
       {
          System.out.println(arr[i]);
       }
            
       display(arr,10);
             
   } 
        
 

MY OUTPUT IS COMING AS FOLLOWING BUT MY QUESTION IS WHY THE ARRAY AFTER PERFORMING THE OPERATION IS DISPLAYING DIFFERENT IN DIFFERENT PART

First part when displaying in main function.

2
10
49
56
80

Second part when displaying in main function.

94
40
12
25
60

The array displaying in the display method after operation

2
10
49
56
80
94
40
12
25
60

The desired output is arranging the first half in ascending order and second half in descending order

0 Answers
Related