#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; }

## 7 Replies - 1284 Views - Last Post: 08 November 2009 - 01:41 PM

### #1

# Ages Sort

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.

##
**Replies To:** Ages Sort

### #2

## 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.

### #3

## 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

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

### #4

## 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.

### #5

## Re: Ages Sort

Posted 08 November 2009 - 01:21 PM

### #6

## Re: Ages Sort

Posted 08 November 2009 - 01:25 PM

Momerath, 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

### #7

## Re: Ages Sort

Posted 08 November 2009 - 01:35 PM

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

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

#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

### #8

## Re: Ages Sort

Posted 08 November 2009 - 01:41 PM

Zerobu, on 8 Nov, 2009 - 12:35 PM, said:

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

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

#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!

Page 1 of 1