How do you sort an array without using API

Questions by mananssl

Showing Answers 1 - 4 of 4 Answers

public class sortArray {
    
       public sortArray() {
    }
   
 
   public int[] sortToArray(int []a)
   {
       int len=a.length;
       int temp;
       for(int i=0;i<len-1;i++)
       {
          
               if(a[i]<a[i+1])
               {
                temp=a[i];
                   a[i]=a[i+1];
                   a[i+1]=temp;
               }
          
          
       }
       return a;
   }
   public static void main(String argd[])
   {
         int a[]=new int[] {5,9,8,2,1};
        sortArray s=new sortArray();
       int b[]= s.sortToArray(a);
        for(int i=0;i<b.length;i++)
        {
            System.out.println("sort array is" + b[i]);
        }
   }
}

  Was this answer useful?  Yes

thrinadh

  • Mar 25th, 2015
 

Code
  1. span style="color: #0000ff;">"sort array is" + b[i]);

  2.         }

  3.    }

  4. }

  Was this answer useful?  Yes

Manpreet Kesar

  • May 26th, 2016
 

Hi
Your Code is totally wrong... it works only on integers what you have provided....but it doesnt work on another integers set.
Actually U have tried Bubble Sort , but you dont know bubble sort requires two loops one for passes and one for to swap elements.

  Was this answer useful?  Yes

vedansh

  • Oct 18th, 2016
 

Your Given Algo doesnt work ,Can be done this way :

Code
  1.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions