Ages Sort

Selection Sort

Page 1 of 1

7 Replies - 744 Views - Last Post: 08 November 2009 - 01:41 PM Rate Topic: -----

#1 hottestchica2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-October 09

Ages Sort

Post icon  Posted 08 November 2009 - 12:50 PM

I'm trying to do a selection sort using an array of ages which i have already set. My book is in pseudocode and my assignment is due in C++. I managed to get it to show the unsorted array and then for the unsorted array it comes out with just one 0. not sure what im doing wrong, but i've tried a couple of different things and looked up selection sort online, but still hitting a wall.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
	const int NumAges = 5;
	int Ages[NumAges], min, k, j, index, temp, exit;
	Ages[0] = 24;
	Ages[1] = 3;
	Ages[2] = 62;
	Ages[3] = 4;
	Ages[4] = 61;
	cout << "The following list is my family's ages in no certain";
	cout << " order:\n";
	for (k = 0; k < NumAges; k++)
	{
		cout << Ages[k] <<endl;
		min = Ages[k];
		index = k;
		for (j = k+1; j <= NumAges; j++)
		{
			if (Ages[j] < min)		  
				min = Ages[j];
				index = j;
		}
		if (k != index)
			 temp = Ages[k];
			 Ages[k] = Ages[index];
			 Ages[index] = temp;
	}
	cout << "Sorted List:\n";
		cout << Ages[k] << endl;
		cin >> exit;
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Ages Sort

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Ages Sort

Posted 08 November 2009 - 01:00 PM

Your output for the sorted array isn't in a loop, so the value of k will never change.
Was This Post Helpful? 1
  • +
  • -

#3 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Ages Sort

Posted 08 November 2009 - 01:05 PM

Here is how the Selection Sort Algorithm works.

The algorithm works as follows:
Find the minimum value in the list
Swap it with the value in the first position
Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)

http://en.wikipedia..../Selection_sort
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Ages Sort

Posted 08 November 2009 - 01:11 PM

He's doing a selection sort with some extra junk thrown in that isn't needed.
Was This Post Helpful? 0
  • +
  • -

#5 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Ages Sort

Posted 08 November 2009 - 01:21 PM

Heres a selection sort snippet
http://www.dreaminco...snippet2212.htm
Was This Post Helpful? 1
  • +
  • -

#6 hottestchica2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-October 09

Re: Ages Sort

Posted 08 November 2009 - 01:25 PM

View PostMomerath, on 8 Nov, 2009 - 12:00 PM, said:

Your output for the sorted array isn't in a loop, so the value of k will never change.

Now it gives an output in the sorted list except its the same as the original list except the number starts at 0 then stops at 4.
//This program is an example of a selection sort
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
	const int NumAges = 5;
	int Ages[NumAges], min, k, j, index, temp, exit;
	Ages[0] = 24;
	Ages[1] = 3;
	Ages[2] = 62;
	Ages[3] = 4;
	Ages[4] = 61;
	cout << "The following list is my family's ages in no certain";
	cout << " order:\n";
	for (k = 0; k < NumAges; k++)
	{
		cout << Ages[k] <<endl;
		min = Ages[k];
		index = k;
		for (j = k+1; j <= NumAges; j++)
		{
			if (Ages[j] < min)		  
			   min = Ages[j];
				index = j;
		}
		if (k != index)
		   temp = Ages[k];
			 Ages[k] = Ages[index];
			 Ages[index] = temp;
		   
	}
	cout << "Sorted List:\n";
	for(k=0; k < NumAges; k++)
	{
	 cout << Ages[k] << endl;
	}
	cin >> exit;
	return 0;
}


This post has been edited by hottestchica2004: 08 November 2009 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#7 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Ages Sort

Posted 08 November 2009 - 01:35 PM

This is re-written with the above snippet i listed.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void SelectionSort(int A[], int length)
{
	int i, j, min, minat;
		for(i = 0; i<(length-1); i++)
		{
				minat = i;
				min = A[i];
 
	  for(j = i+1;j < length; j++) //select the min of the rest of array
		  {
				  if(min > A[j])   //ascending order for descending reverse
				  {
						  minat = j;  //the position of the min element
						  min = A[j];
				  }
		  }
		  int temp = A[i];
		  A[i] = A[minat];  //swap
		  A[minat]=temp;	   
		}
}//end selection sort
//This program is an example of a selection sort

int main()
{
	const int NumAges = 5;
	int Ages[NumAges];
	Ages[0] = 24;
	Ages[1] = 3;
	Ages[2] = 62;
	Ages[3] = 4;
	Ages[4] = 61;
	cout << "The following list is my family's ages in no certain";
	cout << " order:\n";

	for(int i = 0; i < NumAges; i++)
		cout << Ages[i]<< endl;

	SelectionSort(Ages,NumAges);

	cout << "The Sort List: ";
	for (int i = 0; i < NumAges; i++)
		cout << Ages[i] <<endl;

}



Look very closely at the selection sort function. Try and make sure you understand what is going on in there.

This post has been edited by Zerobu: 08 November 2009 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#8 hottestchica2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-October 09

Re: Ages Sort

Posted 08 November 2009 - 01:41 PM

View PostZerobu, on 8 Nov, 2009 - 12:35 PM, said:

This is re-written with the above snippet i listed.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void SelectionSort(int A[], int length)
{
	int i, j, min, minat;
		for(i = 0; i<(length-1); i++)
		{
				minat = i;
				min = A[i];
 
	  for(j = i+1;j < length; j++) //select the min of the rest of array
		  {
				  if(min > A[j])   //ascending order for descending reverse
				  {
						  minat = j;  //the position of the min element
						  min = A[j];
				  }
		  }
		  int temp = A[i];
		  A[i] = A[minat];  //swap
		  A[minat]=temp;	   
		}
}//end selection sort
//This program is an example of a selection sort

int main()
{
	const int NumAges = 5;
	int Ages[NumAges];
	Ages[0] = 24;
	Ages[1] = 3;
	Ages[2] = 62;
	Ages[3] = 4;
	Ages[4] = 61;
	cout << "The following list is my family's ages in no certain";
	cout << " order:\n";

	for(int i = 0; i < NumAges; i++)
		cout << Ages[i]<< endl;

	SelectionSort(Ages,NumAges);

	cout << "The Sort List: ";
	for (int i = 0; i < NumAges; i++)
		cout << Ages[i] <<endl;

}



Look very closely at the selection sort function. Try and make sure you understand what is going on in there.

I got it thank you very much for the help!!!! Im gonna be studying this for the test this Tues!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1