Why don't you refer to "The Complete IDIOT'S guide to C++" by Paul Snaith?
2006-10-28 23:07:50
·
answer #1
·
answered by mkm 2
·
0⤊
0⤋
insertion sort.
The algorithm is simple. First find out the smallest number in the array and exchange it with the first element. Continue this untill the entire array is finished.
#include
#include
void main()
{
clrscr();
int number_of_elements = 10;
int arr[10] = {4,6,7,1,3,2,5,9,8,4};
for(int i = 0;i<=number_of_elements-2;i++)
{
//first find out the smallest element starting from arr[i]
int smallest_index = i;//this will not store the number but the index in the array
for(int j = i+1;j<=number_of_elements-1;j++)
if(arr[smallest_index] > arr[j])
smallest_index = j;
//now we know that the smallest number is in a[smallest_index]
//exchange this with the ith element
int temp = arr[i];
arr[i] = arr[smallest_index];
arr[smallest_index] = temp;
}
for(i =0;i<=number_of_elements-1;i++)
printf("%d ",arr[i]);
getch();
}
In the first loop its only upto number_of_elements-2 becuase when all the small elements are sorted, the largest element is always at the last.
2006-10-29 00:13:51
·
answer #2
·
answered by manoj Ransing 3
·
0⤊
0⤋
/* SORT A LIST */
/*gcc compiler*/
#include"iostream"
using namspace std;
int main()
{
int *a,n,i;
cin>>n;
a=(int *)malloc(n * sizeof(int));
for(i=0'i
cin>>(*(a+i));
for(i=0;i
{
for(int j=i+1;j
{
if(a[i]>a[j])
{
//SWAP
a[i]^=a[j];
a[j]^=a[i];
a[i]^=a[j];
}
}
}
//PRINT SORTED LIST
for(i=0;i
cout<
return 0;
}
2006-10-29 08:53:52
·
answer #3
·
answered by Cyberg 1
·
0⤊
0⤋
//BUBBLE SORT <>
#include
#include
main()
{
int x[25],i,j,t,n ;
clrscr();
cout<<"enter no. of elements";
cin>>n;
cout<<"enter"<
for(i=0;i
cin>>x[i];
for(i=0;i
{
for(j=0;j
{
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}}}
cout<<"\n\n\nthe ordered list is:\n";
for(i=0;i
cout<<"\n"<
getch();
}
2006-10-28 23:08:57
·
answer #4
·
answered by Innocence Redefined 5
·
0⤊
0⤋