#include<iostream>
void selectsort(int*,int*);//function prototype
int *smallest(int*,int*);//function prototype
void exchange(int*,int*);//function prototype
void printlist(int*,int*);//function prototype

using namespace std;
void main()
//this will ask the user to give five integers inside the array and will display it
{
	
	int *name;
	const int size=5;
	int size2=5;
	name = &size2;
	int ary[size];
	int *plast;
	 plast = ary + size - 1;
	int *pwalk;

	for(pwalk = ary; pwalk <= plast; pwalk++)//this will create a array with integers the users adds
	{
		cout<<"please enter an integer:"<<endl;
		cin>>*pwalk;
	}
	
	 selectsort(ary,name);
     cout<<endl;
	 

	
}

	void selectsort(int *ary,int *size)
	{
		int small_number;
		int pass;
	
		for(pass = 0; pass <= *size - 1; pass++)// a loop that will go to each number in the array
		{
			 small_number = pass;// predicting that this is smallest 
		}
	}



	int *smallest(int *ary,int *size)
	{
		int small_number;
		int pass;

		for(int j = pass + 1; j < *size; j++)
		{
			if(ary[j] < ary[small_number])// this will check if the p is less the small number and if not then it will exchange
			small_number = j; // this will be used for a exchange if the if statement is false
		}
		return small_number;
	}
	void exchange(int *ary,int *size)
	{
		int small_number;
		int pass;
		{	
		int temp = ary[pass];
		ary[pass] = ary[small_number];
		ary[small_number] = temp;
		}
		
	}
	void printlist(int *ary, int *size)
	{
		int *pwalk;
		int *plast;
		for(pwalk = ary; pwalk <= plast; pwalk++)// this will display the array
	{
		cout<<*pwalk<<endl;
	}
	
}

Recommended Answers

All 3 Replies

There are litterally hundreds of examples all over the net. All you have to do is use google to find them. Here is one example.

that is a very good example that you have shown me. However the problem is that i have to use pointers inside my program that i'm not very familiar with

The algorithm just says :

//Input Array[0..n-1] unsorted
//Output Array[0..n-1] sorted

for i ranging from 0 to n-2
    min = i;
    for j ranging from i+1 to n-1
        if Arr[j] < Arr[min]
             min = j;
    Swap( Arr[i] , Arr[min] )

You can implement the above algorithm of selection sort in any way you want using variables or pointers .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.