<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 6.4 &#8212; Sorting an array using selection sort</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: lampshade</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-96573</link>
		<dc:creator>lampshade</dc:creator>
		<pubDate>Thu, 08 Dec 2011 15:06:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-96573</guid>
		<description>needed to make that outerloop&#039;s loop variable = to zero... silly me.</description>
		<content:encoded><![CDATA[<p>needed to make that outerloop&#8217;s loop variable = to zero&#8230; silly me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lampshade</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-96572</link>
		<dc:creator>lampshade</dc:creator>
		<pubDate>Thu, 08 Dec 2011 15:04:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-96572</guid>
		<description>Can&#039;t seem to get the following to work in bloodshed C++. The output just brings me the unsorted list.


#include 
#include 
#include 



int main(){
using namespace std;
const int size=6;
int array[size]={30, 60, 20, 50, 40, 10};



for(int outer; outer&lt;size; outer++)
{ 
 
  int smallest=outer;
  

         for(int inner=outer+1;inner&lt;size;inner++)
                 {if(array[inner] &lt; array[smallest]) 
                 smallest=inner;}
                 
                 swap(array[outer], array[smallest]);
                                
  }
    
for (int nIndex=0; nIndex &lt; size; nIndex++){
    cout &lt;&lt; array[nIndex] &lt;&lt; endl; 

}
cin.clear();
cin.ignore(255, &#039;\n&#039;);
cin.get();
}

anyone see where this went wrong?</description>
		<content:encoded><![CDATA[<p>Can&#8217;t seem to get the following to work in bloodshed C++. The output just brings me the unsorted list.</p>
<p>#include<br />
#include<br />
#include </p>
<p>int main(){<br />
using namespace std;<br />
const int size=6;<br />
int array[size]={30, 60, 20, 50, 40, 10};</p>
<p>for(int outer; outer&lt;size; outer++)<br />
{ </p>
<p>  int smallest=outer;</p>
<p>         for(int inner=outer+1;inner&lt;size;inner++)<br />
                 {if(array[inner] &lt; array[smallest])<br />
                 smallest=inner;}</p>
<p>                 swap(array[outer], array[smallest]);</p>
<p>  }</p>
<p>for (int nIndex=0; nIndex &lt; size; nIndex++){<br />
    cout &lt;&lt; array[nIndex] &lt;&lt; endl; </p>
<p>}<br />
cin.clear();<br />
cin.ignore(255, &#039;\n&#039;);<br />
cin.get();<br />
}</p>
<p>anyone see where this went wrong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Divyank</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95867</link>
		<dc:creator>Divyank</dc:creator>
		<pubDate>Mon, 15 Aug 2011 11:59:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95867</guid>
		<description>Thanks for the help guys
I was so puzzled by the problem that I even tried another compiler.
AWSOME tutorials, Alex</description>
		<content:encoded><![CDATA[<p>Thanks for the help guys<br />
I was so puzzled by the problem that I even tried another compiler.<br />
AWSOME tutorials, Alex</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: octavsly</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95865</link>
		<dc:creator>octavsly</dc:creator>
		<pubDate>Mon, 15 Aug 2011 07:40:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95865</guid>
		<description>@Divyank

You need to change the 
&lt;code&gt;
if(anarray[current]&lt;anarray[start])
&lt;/code&gt;
with 
&lt;code&gt;
if(anarray[current]&lt;anarray[small])
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Divyank</p>
<p>You need to change the<br />
<code><br />
if(anarray[current]&lt;anarray[start])<br />
</code><br />
with<br />
<code><br />
if(anarray[current]&lt;anarray[small])<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zingmars</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95863</link>
		<dc:creator>zingmars</dc:creator>
		<pubDate>Mon, 15 Aug 2011 07:29:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95863</guid>
		<description>Because you don&#039;t need to compare if current number is bigger than the &#039;start&#039;, you need to compare it to the smallest index (small).</description>
		<content:encoded><![CDATA[<p>Because you don&#8217;t need to compare if current number is bigger than the &#8216;start&#8217;, you need to compare it to the smallest index (small).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Divyank</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95859</link>
		<dc:creator>Divyank</dc:creator>
		<pubDate>Sun, 14 Aug 2011 13:18:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95859</guid>
		<description>Can anyone tell me what is wrong with this code ?

&lt;pre&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

int main()
{   using namespace std ;

const int x = 9 ;
int anarray[x] = {5,3,767,234,656,34,786,264,237} ;
for(int start = 0 ; start&lt;x ; start++)
{
    int small = start ;
    for(int current = start +1 ;current&lt;x ; current++)
    {
        if(anarray[current]&lt;anarray[start])
        small = current ;
    }
    swap(anarray[start],anarray[small]) ;
}
cout&lt;&lt;anarray[0]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[1]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[2]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[3]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[4]&lt;&lt;&quot;\t&quot;&lt;&lt;
anarray[5]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[6]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[7]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[8] ;
return 0 ; }
&lt;/pre&gt;
 
It outputs -


3       5       237     34      264     234     767     656     786</description>
		<content:encoded><![CDATA[<p>Can anyone tell me what is wrong with this code ?</p>
<pre>
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

int main()
{   using namespace std ;

const int x = 9 ;
int anarray[x] = {5,3,767,234,656,34,786,264,237} ;
for(int start = 0 ; start&lt;x ; start++)
{
    int small = start ;
    for(int current = start +1 ;current&lt;x ; current++)
    {
        if(anarray[current]&lt;anarray[start])
        small = current ;
    }
    swap(anarray[start],anarray[small]) ;
}
cout&lt;&lt;anarray[0]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[1]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[2]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[3]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[4]&lt;&lt;&quot;\t&quot;&lt;&lt;
anarray[5]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[6]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[7]&lt;&lt;&quot;\t&quot;&lt;&lt;anarray[8] ;
return 0 ; }
</pre>
<p>It outputs -</p>
<p>3       5       237     34      264     234     767     656     786</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AUASP</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95653</link>
		<dc:creator>AUASP</dc:creator>
		<pubDate>Sat, 02 Jul 2011 10:45:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95653</guid>
		<description>a single statement can swap 2 values.
a^=b^=a^=b;//swaps a and b</description>
		<content:encoded><![CDATA[<p>a single statement can swap 2 values.<br />
a^=b^=a^=b;//swaps a and b</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: GeexLenuDe</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95094</link>
		<dc:creator>GeexLenuDe</dc:creator>
		<pubDate>Thu, 06 Jan 2011 21:28:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95094</guid>
		<description>It can be done without #include  
&lt;code&gt;&lt;pre&gt;
//take the comments if you are using vc++ version 2005
//#include “stdafx.h” 
//including the input and output header library
#include &lt;iostream&gt; 

//using the namespace std for not putting it again
using namespace std;

//declaring a constant value that is the size of our array
const int nNumStudents = 5;

//declaring all the functions that won&#039;t return any value
void printArray(), swapNum(int, int), sortArray(), findLower(int);

//declaring our array
int anScores[nNumStudents] = { 50, 43, 80, 70, 57 };

//declaring the min score variables 
int nMinScore = 0, nMinScoreIndex = 0;

//defining the function that will print the array
void printArray(){
    
    //using a for loop to print the array
    for(int nStudent = 0;nStudent &lt; nNumStudents; nStudent++) {
       
        //printing our array
        cout&lt;&lt; anScores[nStudent]&lt;&lt; endl;
    }
}

//defining our function that will find the lowest number from an starting index
void findLower(int nStartIndex = 0){

    //defining the lowest score in of all the array from an starting index
    nMinScore = anScores[nStartIndex];
    
    //say that if the starting index is greater than the size of the array
    if(nStartIndex &gt; nNumStudents){

        //then the starting index is as the size of the array
        nStartIndex = nNumStudents;
    } else if(nStartIndex &lt; 0) {

        //but if is lower than cero then our starting index is 0
        nStartIndex = 0;
    }

    //increase the starting index while it is lower than our array size
    for(;nStartIndex &lt; nNumStudents; nStartIndex++) { 

        //checking if the current score is lower than the lowest so
        if (anScores[nStartIndex] &lt;= nMinScore) {
         
            //the lowest score is the same as the current score
            nMinScore = anScores[nStartIndex];

            //defining the min score index that is the same as current index
            nMinScoreIndex = nStartIndex;
        }
    }
}

//defining our function swap to flip the elements of the array
void swapNum(int nArrayIndex = 0, int nArrayIndex2 = 0){

    //checking if both index are lower than the array size
    if(nArrayIndex&lt;nNumStudents&amp;&amp;nArrayIndex2&lt;nNumStudents){

        //defining the values of each number from the index given in 2 variables
        int nValue  = anScores[nArrayIndex];
        int nValue2 = anScores[nArrayIndex2];

        //swapping the values of each index
        anScores[nArrayIndex2]  = nValue;
        anScores[nArrayIndex]  = nValue2;
    }
}

//now defining the function that will sort our array
void sortArray(){
    
    //using a for loop to do so 
    for(int nStudent = 0; nStudent &lt; nNumStudents; nStudent++){
    
        //find the lowest number and using the current student as starting index
        findLower(nStudent);
        //swapping the current student with the lowest value index of the array
        swapNum(nStudent, nMinScoreIndex);
    }
}

//defining the main function
int main(){

    //outputting the following text
    cout&lt;&lt; &quot;The scores are:&quot; &lt;&lt; endl;
 
    // printing our array
    printArray();

    //sorting the array
    sortArray();

    //outputting the following text
    cout&lt;&lt; &quot;The sorted scores are:&quot; &lt;&lt; endl;

    //printing again the array to check how it was sorted
    printArray();

    //pause used in windows systems
    system(&quot;pause&quot;);
}
&lt;/pre&gt;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>It can be done without #include<br />
<code>&lt;pre&gt;<br />
//take the comments if you are using vc++ version 2005<br />
//#include “stdafx.h”<br />
//including the input and output header library<br />
#include &lt;iostream&gt; </p>
<p>//using the namespace std for not putting it again<br />
using namespace std;</p>
<p>//declaring a constant value that is the size of our array<br />
const int nNumStudents = 5;</p>
<p>//declaring all the functions that won&#039;t return any value<br />
void printArray(), swapNum(int, int), sortArray(), findLower(int);</p>
<p>//declaring our array<br />
int anScores[nNumStudents] = { 50, 43, 80, 70, 57 };</p>
<p>//declaring the min score variables<br />
int nMinScore = 0, nMinScoreIndex = 0;</p>
<p>//defining the function that will print the array<br />
void printArray(){</p>
<p>    //using a for loop to print the array<br />
    for(int nStudent = 0;nStudent &lt; nNumStudents; nStudent++) {</p>
<p>        //printing our array<br />
        cout&lt;&lt; anScores[nStudent]&lt;&lt; endl;<br />
    }<br />
}</p>
<p>//defining our function that will find the lowest number from an starting index<br />
void findLower(int nStartIndex = 0){</p>
<p>    //defining the lowest score in of all the array from an starting index<br />
    nMinScore = anScores[nStartIndex];</p>
<p>    //say that if the starting index is greater than the size of the array<br />
    if(nStartIndex &gt; nNumStudents){</p>
<p>        //then the starting index is as the size of the array<br />
        nStartIndex = nNumStudents;<br />
    } else if(nStartIndex &lt; 0) {</p>
<p>        //but if is lower than cero then our starting index is 0<br />
        nStartIndex = 0;<br />
    }</p>
<p>    //increase the starting index while it is lower than our array size<br />
    for(;nStartIndex &lt; nNumStudents; nStartIndex++) { </p>
<p>        //checking if the current score is lower than the lowest so<br />
        if (anScores[nStartIndex] &lt;= nMinScore) {</p>
<p>            //the lowest score is the same as the current score<br />
            nMinScore = anScores[nStartIndex];</p>
<p>            //defining the min score index that is the same as current index<br />
            nMinScoreIndex = nStartIndex;<br />
        }<br />
    }<br />
}</p>
<p>//defining our function swap to flip the elements of the array<br />
void swapNum(int nArrayIndex = 0, int nArrayIndex2 = 0){</p>
<p>    //checking if both index are lower than the array size<br />
    if(nArrayIndex&lt;nNumStudents&amp;&amp;nArrayIndex2&lt;nNumStudents){</p>
<p>        //defining the values of each number from the index given in 2 variables<br />
        int nValue  = anScores[nArrayIndex];<br />
        int nValue2 = anScores[nArrayIndex2];</p>
<p>        //swapping the values of each index<br />
        anScores[nArrayIndex2]  = nValue;<br />
        anScores[nArrayIndex]  = nValue2;<br />
    }<br />
}</p>
<p>//now defining the function that will sort our array<br />
void sortArray(){</p>
<p>    //using a for loop to do so<br />
    for(int nStudent = 0; nStudent &lt; nNumStudents; nStudent++){</p>
<p>        //find the lowest number and using the current student as starting index<br />
        findLower(nStudent);<br />
        //swapping the current student with the lowest value index of the array<br />
        swapNum(nStudent, nMinScoreIndex);<br />
    }<br />
}</p>
<p>//defining the main function<br />
int main(){</p>
<p>    //outputting the following text<br />
    cout&lt;&lt; &quot;The scores are:&quot; &lt;&lt; endl;</p>
<p>    // printing our array<br />
    printArray();</p>
<p>    //sorting the array<br />
    sortArray();</p>
<p>    //outputting the following text<br />
    cout&lt;&lt; &quot;The sorted scores are:&quot; &lt;&lt; endl;</p>
<p>    //printing again the array to check how it was sorted<br />
    printArray();</p>
<p>    //pause used in windows systems<br />
    system(&quot;pause&quot;);<br />
}<br />
&lt;/pre&gt;</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: azmath</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95092</link>
		<dc:creator>azmath</dc:creator>
		<pubDate>Thu, 06 Jan 2011 05:26:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95092</guid>
		<description>can you give me an example that will sort the keyborad input value using c++.(string compare)</description>
		<content:encoded><![CDATA[<p>can you give me an example that will sort the keyborad input value using c++.(string compare)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: azmath</title>
		<link>http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/comment-page-1/#comment-95091</link>
		<dc:creator>azmath</dc:creator>
		<pubDate>Thu, 06 Jan 2011 05:15:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/64-sorting-an-array-using-selection-sort/#comment-95091</guid>
		<description>i am a new c++ programmer. i can&#039;t arrange insert keyboard value assending order using c++. please help me.</description>
		<content:encoded><![CDATA[<p>i am a new c++ programmer. i can&#8217;t arrange insert keyboard value assending order using c++. please help me.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

