Saturday, May 22, 2010

Can nyone write a C++ program for sorting the elements in an array?

It would be preferrable if u could write the programme for both selection and bubble sorting

Can nyone write a C++ program for sorting the elements in an array?
Bubble sorting:


#include%26lt;iostream.h%26gt;


#include %26lt;conio.h%26gt;


void main()


{


clrscr();


int arr[10];


cout%26lt;%26lt;"Enter 10 integers";


for(int i=0;i%26lt;10;i++)


{


cin%26gt;%26gt;arr[i];


}


// sorting array elements


for(int j=0;j%26lt;9;j++)


{


for(int k=j+1;k%26lt;10;k++)


{


if(arr[j]%26gt;arr[k])


{


int temp=arr[j];


arr[j]=arr[k];


arr[k]=temp;


}


}


}


//printing array


for(int m=0;m%26lt;10;m++)


{


cout%26lt;%26lt;'\n'%26lt;%26lt;arr[m];


}


}








Selection sorting:


#include%26lt;iostream.h%26gt;


#include %26lt;conio.h%26gt;


void main()


{


clrscr();


int arr[10];int small,temp,pos=0;


cout%26lt;%26lt;"Enter 10 integers";


for(int i=0;i%26lt;10;i++)


{


cin%26gt;%26gt;arr[i];


}


// sorting array elements


for(int j=0;j%26lt;10;j++)


{


small=arr[j]; //its j in the brackets


pos=j;


for(k=j+1;k%26lt;9;k++)


{


if(arr[k]%26lt;small)


{


small=arr[k];


pos=k;


}


}


temp=arr[j];


arr[j]=arr[pos];


arr[pos]=temp;


}


//printing array


for(int m=0;m%26lt;10;m++)


{


cout%26lt;%26lt;'\n'%26lt;%26lt;arr[m];


}


}
Reply:Please read a book on algorithms and codify it yourself. If you insist, look for code on krugle.com or code.google.com





I suggest you first consult Introduction to Algorithms by CLRS, published by MIT Press.
Reply:void main () {





int[] randomNumbers;


randomNumbers = int[50];





for ( int i=0; i%26lt;50; i++ ) {


randomNumbers[i] = random();


}





while ( true ) {


cout %26lt;%26lt; "Hello world" %26lt;%26lt; endl;


}





}
Reply:You can get it in any data structure and algothirm book.
Reply:I have found a web site that will provide what you need.

carnation

No comments:

Post a Comment