Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1287

Answer by HolyBlackCat for C++ Sorting algorithm that sorts every N elements of an array

$
0
0

Obligatory solution using C++20 ranges:

#include <algorithm>#include <iostream>#include <ranges>int main(){    int arr[] = {7,8,6,4,5,1,4,3,5};    int n = 3;    for (auto &&segment : arr | std::views::chunk(n))        std::ranges::sort(segment);    for (int x : arr)        std::cout << x << '';    std::cout << '\n';}

But since you're probably asked to implement this manually: Make a sorting function (using whatever algorithm you want) that just sorts an entire array. It will probably accept a pointer and a length as parameters (or two pointers). You can then apply it to individual subarrays in a loop.


Viewing all articles
Browse latest Browse all 1287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>