#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;
}
Ages SortSelection Sort
Page 1 of 1
7 Replies - 555 Views - Last Post: 08 November 2009 - 01:41 PM
#1
Ages Sort
Posted 08 November 2009 - 12:50 PM
Replies To: Ages Sort
#2
Re: Ages Sort
Posted 08 November 2009 - 01:00 PM
#3
Re: Ages Sort
Posted 08 November 2009 - 01:05 PM
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
#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:
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
#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:
#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!
|
|

New Topic/Question
Reply




MultiQuote





|