-4

Can someone please explain how this sorting algorithm works? It's called "sequential_sort"

void sequential_sort(std::vector<unsigned int>& X) {
    unsigned int i, j, count, N = X.size();
    std::vector<unsigned int > tmp(N);

    for (i = 0; i < N; i++) {
        count = 0;
        for (j = 0; j < N; j++) {
            if (X[j] < X[i] || X[j] == X[i] && j < i)
                count++;
        }
        tmp[count] = X[i];
    }

    std::copy(tmp.begin(), tmp.end(), X.begin());
}
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
Nina
  • 1

1 Answers1

1

This just figures out which slot each element needs to go into by counting the number of elements less than it and puts each element in the appropriate slot.

If there are zero elements less than a particular element, then that elements goes in slot 0 of the sorted set. If there is exactly one element less than a particular element, that that particular element goes in slot 1 of the sorted set. And so on.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278