dimanche 14 octobre 2018

Converting insertion sort from vector to list using STL Lib

So I'm having problem converting this insertion sort using lists Can someone please guide me how can I do it? This is the code :

void insertion_sort(std::vector <int> &num) {
int i, j, key;
bool insertionNeeded = false;

for (j = 1; j < num.size(); j++) {
key = num[j];
insertionNeeded = false;
for (i = j - 1; i >= 0; i--) { // larger values move right

    if (key < num[i]) {
        num[i + 1] = num[i];
        insertionNeeded = true;
    }
    else
        break;
}
if (insertionNeeded)
    num[i + 1] = key;    //Put key into its proper location
  }
}

Aucun commentaire:

Enregistrer un commentaire