• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sort the Arrays!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made an array
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....
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
I made an array
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....


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing that you could do is this
Arrays.sort(nameOfArray);
kevin
 
Pay attention! Tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic