c++ - How do I fix my code for selection sort? -


this code implementing selection sort, output not in correct order. can please tell me problem is?

so code implementing selection sort:

#include <iostream> using namespace std;  class sort { public:     void ssort(string[], int[]);     void print(string[], int);  };  // sorting function // problem here? void sort::ssort(string arr[], int numarray[]) {     int min;      (int = 0; < 8; ++i)     {         min = i;         //loop through array find         (int j = + 1; j < 9; ++j)         {             if (numarray[j] < numarray[min])             {                 //found new minimum position, if present                 min = j;             }         }         //swap values         swap(arr[i], arr[min]);     } }  void sort::print(string arr[], int num) {     (int = 0; < num; ++i)     {         cout << arr[i] << endl;     }     cout << endl; }  //main function main() {     sort s;     int num = 8;     string arr[8];     string array[8];     int numarray[8];       cout << "input 8 strings sort: " << endl;     (int = 0; < 8; i++) {         getline(cin, arr[i]);     }     cout << endl;      (int = 0; < 8; i++) {         array[i] = arr[i].substr(0, 1);          if (array[i] == "a")             numarray[i] = 1;         if (array[i] == "b")             numarray[i] = 2;         if (array[i] == "c")             numarray[i] = 3;         if (array[i] == "d")             numarray[i] = 4;         if (array[i] == "e")             numarray[i] = 5;         if (array[i] == "f")             numarray[i] = 6;         if (array[i] == "g")             numarray[i] = 7;         if (array[i] == "h")             numarray[i] = 8;         if (array[i] == "i")             numarray[i] = 9;         if (array[i] == "j")             numarray[i] = 10;         if (array[i] == "k")             numarray[i] = 11;         if (array[i] == "l")             numarray[i] = 12;         if (array[i] == "m")             numarray[i] = 13;         if (array[i] == "n")             numarray[i] = 14;         if (array[i] == "o")             numarray[i] = 15;         if (array[i] == "p")             numarray[i] = 16;         if (array[i] == "q")             numarray[i] = 17;         if (array[i] == "r")             numarray[i] = 18;         if (array[i] == "s")             numarray[i] = 19;         if (array[i] == "t")             numarray[i] = 20;         if (array[i] == "u")             numarray[i] = 21;         if (array[i] == "v")             numarray[i] = 22;         if (array[i] == "w")             numarray[i] = 23;         if (array[i] == "x")             numarray[i] = 24;         if (array[i] == "y")             numarray[i] = 25;         if (array[i] == "z")             numarray[i] = 26;      }      cout << "the initial input: " << endl;     s.print(arr, num);      s.ssort(arr, numarray);      cout << endl << "the input after sorting: " << endl;     s.print(arr, num); } 

what can this?

i identified 3 issues quick reading of code:

  • first inner loop condition wrong

    void sort::ssort(string arr[], int numarray[]) {   int min;    (int = 0; < 8; ++i) {     min = i;     //loop through array find     (int j = + 1; j < 9; ++j) // out-of-bound access on j == 8 
  • second: ssort function sort strings according first character. not sorting strings.

  • third (and big algorithmic error): you're not swapping numarray vector elements after swapped arr elements. means swaps after first 1 (where indices in right position) potentially wrong.

    void sort::ssort(string arr[], int numarray[]) {   int min;    (int = 0; < 8; ++i)   {     min = i;     //loop through array find     (int j = + 1; j < 8; ++j)     {       if (numarray[j] < numarray[min])       {           //found new minimum position, if present         min = j;       }     }     //swap values     swap(arr[i], arr[min]);     // swap comparison values     swap(numarray[i], numarray[min]); // <-- missing!   } } 

tips improve:

  • grab algorithms book , study it
  • grab debugger , use it

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -