Skip to main content
deleted 26 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}
Rollback to Revision 1
Source Link
Phrancis
  • 20.4k
  • 6
  • 68
  • 154

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

Edit: I tried an iterator approach using the modern C++ way as suggested:
Could someone tell me if there are mistakes since I am not used to this approach.

template<typename Iterator, typename Comparator>
void selection_sort(Iterator begin, Iterator end, Comparator comparator) {
    for (; begin != end; ++begin) {
        auto min = std::min_element(begin, end, comparator);
        if (comparator(*min, *begin)) {
            std::iter_swap(min, begin);
        }
    }
}
template<typename Iterator>
void selection_sort(Iterator begin, Iterator end) {
    typedef typename std::iterator_traits<Iterator>::value_type value_type;
    selection_sort(begin, end, std::less<value_type>());
}

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

Edit: I tried an iterator approach using the modern C++ way as suggested:
Could someone tell me if there are mistakes since I am not used to this approach.

template<typename Iterator, typename Comparator>
void selection_sort(Iterator begin, Iterator end, Comparator comparator) {
    for (; begin != end; ++begin) {
        auto min = std::min_element(begin, end, comparator);
        if (comparator(*min, *begin)) {
            std::iter_swap(min, begin);
        }
    }
}
template<typename Iterator>
void selection_sort(Iterator begin, Iterator end) {
    typedef typename std::iterator_traits<Iterator>::value_type value_type;
    selection_sort(begin, end, std::less<value_type>());
}

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}
Added iterator approach.
Source Link
puls99
  • 323
  • 1
  • 8

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

Edit: I tried an iterator approach using the modern C++ way as suggested:
Could someone tell me if there are mistakes since I am not used to this approach.

template<typename Iterator, typename Comparator>
void selection_sort(Iterator begin, Iterator end, Comparator comparator) {
    for (; begin != end; ++begin) {
        auto min = std::min_element(begin, end, comparator);
        if (comparator(*min, *begin)) {
            std::iter_swap(min, begin);
        }
    }
}
template<typename Iterator>
void selection_sort(Iterator begin, Iterator end) {
    typedef typename std::iterator_traits<Iterator>::value_type value_type;
    selection_sort(begin, end, std::less<value_type>());
}

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

Hello fellow programmers, I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong?

template<typename Element>
void selection_sort(Element arr[], size_t size) {
    auto index_toSort = 0u;
    while (index_toSort < size) {
        auto index_smallest = index_toSort;
        for (auto index = index_toSort; index < size; ++index) {
            if (arr[index] < arr[index_smallest]) {
                index_smallest = index;
            }
        }
        std::swap(arr[index_toSort], arr[index_smallest]);
        ++index_toSort;
    }
}

Edit: I tried an iterator approach using the modern C++ way as suggested:
Could someone tell me if there are mistakes since I am not used to this approach.

template<typename Iterator, typename Comparator>
void selection_sort(Iterator begin, Iterator end, Comparator comparator) {
    for (; begin != end; ++begin) {
        auto min = std::min_element(begin, end, comparator);
        if (comparator(*min, *begin)) {
            std::iter_swap(min, begin);
        }
    }
}
template<typename Iterator>
void selection_sort(Iterator begin, Iterator end) {
    typedef typename std::iterator_traits<Iterator>::value_type value_type;
    selection_sort(begin, end, std::less<value_type>());
}
Source Link
puls99
  • 323
  • 1
  • 8
Loading