#include<iOStream>
#include
// prints a 2d array of strings
void print2d(const std::string* arr, const size_t rows, const size_t cols)
{
for(size_t row=0; row { for(size_t col=0; col { std::cout< } std::cout< arr+=cols; } } int main() { // example 2d array const size_t rows = 10; const size_t cols = 2; std::string names[rows][cols] = { "John", "Wayne", "Johnny", "Depp", "Michael", "Douglas", "Anthony", "Hopkins", "Kirk", "Douglas", "Daniel", "Craig", "Tom", "Hanks", "Sean", "Connery", "Sean", "Bean", "Adam", "Baldwin" }; std::cout<<"\nUnsorted:\n"< print2d( names[0], rows, cols ); std::cout< // Sorting algorithm: uses insertion sort for(size_t row=1; row { // copy the current name std::string copy[2]; copy[0] = names[row][0]; copy[1] = names[row][1]; // store the gap row size_t gap=row; // store the compare row (always the row above the gap row) size_t cmp=gap-1; // repeat while the gap row is non-zero and the copy name // is less than the compare name (comparing surname first) while(gap && (copy[1] { // move the compare name into the gap names[gap][0]=names[cmp][0]; names[gap][1]=names[cmp][1]; // move the gap up one row --gap; --cmp; } // insert the copy into the gap row names[gap][0]=copy[0]; names[gap][1]=copy[1]; } std::cout<<"\nSorted:\n"< print2d( names[0], rows, cols ); std::cout< } Output Unsorted: John Wayne Johnny Depp Michael Douglas Anthony Hopkins Kirk Douglas Daniel Craig Tom Hanks Sean Connery Sean Bean Adam Baldwin Sorted: Adam Baldwin Sean Bean Sean Connery Daniel Craig Johnny Depp Kirk Douglas Michael Douglas Tom Hanks Anthony Hopkins John Wayne
Copyright © 2026 eLLeNow.com All Rights Reserved.