Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi ..
I'm Trying To Implement The Algorithmic Steps of Selection Sort.

1 -> Find The Min Index
2 -> Exchange It With The Top & Increment.

The Output Is Not Correct .. Please Have A L^_^K. >!?

C++
void in(int arr[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << "Element # " << i+1 << ": ";
        cin >> arr[i];
    }
}
void out(int arr[], int size)
{
    for(int j = 0; j < size; j++)
    {
        cout << arr[j] << "||";
    }
}
int find_min(int arr[], int size)
{
    int index = 0;
    for(int k = 0; k < size; k++)
    {
        if( arr[index] > arr[k])
        {
            index = k;
        }
    }
    return index;
}
void exchange(int &x, int &y)
{
    int temp = x;
    x = y;
    y = temp;
}
void selection_sort(int arr[], int size)
{
    int loc;
    for(int i = 0; i < size; i++)
    {
        loc = find_min(arr, size - i);
        exchange(arr[loc], arr[i]);
    }
} 

int main()
{
    const int size = 5;
    int my_array[size];
    in(my_array, size);
    out(my_array, size);
    cout << "After Selection Sort.." << "\n\n";
    selection_sort(my_array, size);
    cout << "\n\t" << "p r i n t i n g.." << endl;
    out(my_array, size);

    return 0;
}


= = > Update(V2)
C++
int find_min(int ar[], int size)
{
    int index = 0;
	bool found = false;
	for(int i = 1; i < size; i++)
    {
		if(ar[index] > ar[i])
        {
			index = i;
			found = true;
        }
    }

	if (found)
	{
		return index;
	}
	else
		return -1;
	
}

void selection_sort(int arr[], int size)
{
    int loc;
	for(int count = 0; count < size; count++)
    {
		loc = find_min(arr, size - count);
		if (loc >= 0)
		{
			exchange(arr[count], arr[loc]);
		}
    }
}
Posted
Updated 21-Sep-13 9:58am
v4

1 solution

The main error is that in step i you should find the minimum of the elements i ... N-1 and switch places with arr[i]. So shouldn't it be
C++
for(int i = 0; i < size; i++)
{
    loc = find_min(arr+i, size - i);
    exchange(arr[loc], arr[i]);
}

A more cosmetic problem is that you do one superfluous compare in your find_min function:
C++
int find_min(int arr[], int size)
{
    int index = 0;
    for(int k = 0; k < size; k++)
    {
        if( arr[index] > arr[k])

In the first iteration you are comparing arr[0] with arr[0]. So why don't you start the loop with k = 1:
C++
int find_min(int arr[], int size)
{
    int index = 0;
    for(int k = 1; k < size; k++)
    {
        if( arr[index] > arr[k])
 
Share this answer
 
Comments
Usman Hunjra 19-Sep-13 11:35am    
Thank You nv3.
But Sir.. The Result Is Still not correct ..
FurtherMore i can't understand these statements;
for(int i = 0; i < size; i++)
{
loc = find_min(arr+i, size - i); // why add +i in arr.. ??????
exchange(arr[loc], arr[i]);
}
nv3 19-Sep-13 13:19pm    
arr+i does not add i to the array elements, but evaluates to a pointer to the i-th element of the array. (Remember: The name of an array is at the same time a pointer to the first element). And that's what you want, to find the smallest of the elements i ... N-1.
Usman Hunjra 19-Sep-13 15:13pm    
yes thank you ..
But still the output is not correct .. !!?!???
nv3 19-Sep-13 15:18pm    
Your welcome. Then your next step should be to run your program with a debugger and observe what happens in each iteration of your loop. It is important that you learn how to that, so better get familiar with the debugger on your system right away.
Usman Hunjra 20-Sep-13 11:05am    
yeah i did .. but i can't figure out the mistake ..
Any Further Help .. ??!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900