Tri par insertion des éléments dans un vector<string>
Bonjour,
Je suis en train de mettre en oeuvre le tri par insertion des éléments dans un vector<string>, voici comment j'ai conçu mon programme :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#ifndef INSERTIONSORT_H
#define INSERTIONSORT_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class InsertionSort {
public:
InsertionSort(vector<string>);
~InsertionSort();
InsertionSort();
public:
void populate(vector<string>);
void sort(vector<string>);
void display(vector<string>);
void treatment(vector<string>);
};
#endif // INSERTIONSORT_H |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include "InsertionSort.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
InsertionSort::InsertionSort(){}
InsertionSort::InsertionSort(vector<string> words) {
}
InsertionSort::~InsertionSort() {
cout << "instance is destroyed" << endl;
}
void InsertionSort::treatment(vector<string> datastore) {
populate(datastore);
sort(datastore);
display(datastore);
}
void InsertionSort::populate(vector<string> datastore) {
string elements = "";
char answer;
do {
cout << "Enter value : " << flush;
cin >> elements;
datastore.push_back(elements);
cout << "More elements (y/n)? " << flush;
cin >> answer;
} while(answer == 'y' || answer == 'Y');
cout << datastore.size() << " elements are added to your basket " << endl;
// display elements added
vector<string>::iterator it;
for(it=datastore.begin(); it<datastore.end(); ++it) {
cout<< *it << endl;
}
}
/**
* @brief Implémentation du tri par insertion des éléments dans un vector
* @param datastore le vector pour stocker les éléments
*/
void InsertionSort::sort(vector<string> datastore) {
int indice = 1; // le parcours du vecteur débute à partir du 2ème élément
int indiceInserer;
string valeurAInserer;
while(indice < datastore.size()) {
// lors du parcours du tableau on recherche la plus petite valeur et on la stocke dans
// la variable valeurAInserer
valeurAInserer = datastore[indice];
indiceInserer = indice - 1; // indice de la valeur à insérer
while((indiceInserer >= 0) && (valeurAInserer <= datastore[indiceInserer])) {
datastore[indiceInserer + 1] = datastore[indiceInserer];
indiceInserer = indiceInserer - 1;
}
datastore[indiceInserer + 1] = valeurAInserer;
indice++;
}
}
void InsertionSort::display(vector<string> datastore) {
cout << "Display basket :" << endl;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <InsertionSort.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> fruits(0); // initialize an empty vector of strings
InsertionSort basket(fruits);
basket.treatment(fruits);
return EXIT_SUCCESS;
} |
Mon problème est où intégrer dans mon code la fonction de tri et comment l'afficher? Il s'agit d'un problème de conception algorithmique ... mais je coince.
Merci d'avance.
Transact.