i need to write a Selection Sort program using the following functions: Get data(), Smallest(), and Exchange(). The program should include pointers. The following data should be included in the output: 23, 78, 45, 8, 32, 56.

SAMPLE OUTPUT:

23 78 45 8 32 56

8 78 45 23 32 56

8 23 45 78 32 56

8 23 32 78 45 56

8 23 32 45 78 56

8 23 32 45 56 78

I'll gladly appreciate it if anyone can help me with this

How are you defining "help"? You said "**I** need to write", but neglected to do anything except post your requirements. This suggests that you want us to write it for you and give you the code. So perhaps you should tell us what you've tried so far, and how it hasn't worked. That will prove that you've at least attempted the problem and give us ideas of where to help you.

>hey how do i copy my code here

Copy and paste it, right where you put the "and". Very simple.

```
void selectionSort(int *array,int length)//selection sort function
{
int i,j,min,minat;
for(i=0;i<(length-1);i++)
{
minat=i;
min=array[i];
for(j=i+1;j<(length);j++) //select the min of the rest of array
{
if(min>array[j]) //ascending order for descending reverse
{
minat=j; //the position of the min element
min=array[j];
}
}
int temp=array[i] ;
array[i]=array[minat]; //swap
array[minat]=temp;
}
}
void printElements(int *array,int length) //print array elements
{
int i=0;
for(i=0;i<10;i++)
cout<<array[i]<<endl;
}
void main()
{
int a[]={23,78,45,8,32,56}; // array to sort
selectionSort(a,6); //call to selection sort
printElements(a,0); // print elements
}
```

There's an edit button, you know. You don't have to make a new post if you don't get it right the first time. There's also a preview button when composing a post. I recommend you take advantage of it. :icon_rolleyes:

Okay, so you've got a working program. What's the problem? Are you having trouble breaking the sort down into functions that use Exchange, GetData and Smallest functions?

yes i do and also i only get the output line by line but i want it outputed like this:

23 78 45 8 32 56

8 78 45 23 32 56

8 23 45 78 32 56

8 23 32 78 45 56

8 23 32 45 78 56

8 23 32 45 56 78

Replacing the operations with functions is actually pretty easy. All you have to do is cut out the pieces and give them a function interface:

```
int Smallest ( int a[], int start, int n )
{
int smallest = start;
for ( int i = start; i < n; i++ ) {
if ( GetData ( a, smallest ) > GetData ( a, i ) )
smallest = i;
}
return smallest;
}
void selection_sort ( int a[], int n )
{
for ( int i = 0; i < n - 1; i++ ) {
int minat = Smallest ( a, i, n );
Exchange ( GetData ( a, i ), GetData ( a, minat ) );
}
}
```

I'll let you do GetData and Exchange. The next part is printing the entire list after each exchange, which is pretty trivial since you already have a printElements function. Just tack on a call to that after calling Exchange:

```
void selection_sort ( int a[], int n )
{
for ( int i = 0; i < n - 1; i++ ) {
int minat = Smallest ( a, i, n );
Exchange ( GetData ( a, i ), GetData ( a, minat ) );
printElements ( a, n );
}
}
```

To get the exact output you'll have to tweak some things, but it's not overly difficult. See how far you can get with all of this.

```
#include<iostream.h>
#include<conio.h>
void Selsort(int X[], int SIZE)
{
int pos,small,temp;
for (int i=0; i<SIZE-1; i++) {
small=X[i];
for (int j=i+1; j<SIZE; j++)
{
if (X[j]<small)
{small=X[j];
pos=j;}
}
temp=X[i];
X[i]=X[pos];
X[pos]=temp;
} }
void main(void)
{ clrscr();
int A[10];
int size;
cout<<"\n Enter array size :";
cin>>size;
cout<<"\n Enter array elements :";
for (int i=0; i<size; i++)
{
cin>>A[i];
}
Selsort(A,size);
cout<<"\n The sorted array is as shown below :";
for (int l=0; l<size; l++)
{cout<<A[l];}
getch();
}
```