Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Sort the Arrays!

Khurram Akhter
Greenhorn
Posts: 3
int []aa = { 1,5,2,6,7,9,8}
I want to sort this array with the help of for loop, so what
should i do now....

sandeep jakkaraju
Greenhorn
Posts: 8
You have to use ... Sorting algos ... like insert sort, merge ..etc
I am giving you the code for Insert Sort Ok !!
int temp ;
for( int j = 1; j<aa.length ; j++)>
{
for( int i = 0; i<j ; i++)>
{
temp = aa[i];
aa[i] = aa[j];
aa[j] = temp;
}
}

Originally posted by Khurram Akhter:
int []aa = { 1,5,2,6,7,9,8}
I want to sort this array with the help of for loop, so what
should i do now....

M Iftikhar
Greenhorn
Posts: 3
There are two methods of sorting .
1 Bubble sorting
2 Internal sorting
Following is the procedure of internal sortting
int i=0,j=0,temp=0;
for(i=0 ; i < aa.length() ; i++){
for(j=i+1; j<=aa.length() ;j++){
if(aa[i] > aa[j]){
temp=aa[i];
aa[i] = aa[j];
aa[j] = temp;
}
}
}
Note :
Not sure aboout the method of aa.length() or aa.length

kevin schmidt
Ranch Hand
Posts: 85
Another thing that you could do is this
Arrays.sort(nameOfArray);
kevin