How do you sort an array without using API

Questions by mananssl

Showing Answers 1 - 12 of 12 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. public class Test

  2. {

  3. public int[] sortToArray(int []a)

  4.    {

  5.        int len=a.length;

  6.        int temp;

  7.        for(int i=0;i<len-1;i++)

  8.        {

  9.           for(int j=0;j<len-1;j++)

  10.           {

  11.                if(a[j]>a[j+1])

  12.                {

  13.                 temp=a[j+1];

  14.                    a[j+1]=a[j];

  15.                    a[j]=temp;

  16.                }

  17.           }

  18.          

  19.        }

  20.        return a;

  21.    }

  22.    public static void main(String argd[])

  23.    {

  24.          int a[]=new int[] {5,9,8,2,1,6};

  25.         Test s=new Test();

  26.        int b[]= s.sortToArray(a);

  27.         for(int i=0;i<b.length;i++)

  28.         {

  29.             System.out.println("sort array is" + b[i]);

  30.         }

  31.    }

  32. }

  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. static void getShort(int[] intArr1) {

  2.                 int[] intArr = intArr1;

  3.                 for (int j = 0; j < intArr.length; j++) {

  4.                         for (int i = j + 1; i < intArr.length ; i++) {

  5.                                 if (intArr[j] > intArr[i]) {

  6.                                         System.out.println(Arrays.toString(intArr));

  7.                                         int temp = intArr[i];

  8.                                         intArr[i] = intArr[j];

  9.                                         intArr[j] = temp;

  10.                                 }

  11.                         }

  12.                 }

  13.                 System.out.println(Arrays.toString(intArr));

  14.         }

  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