Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:38:21+00:00 2026-05-25T23:38:21+00:00

What is an efficient way to test if rows in a matrix are sorted?

  • 0

What is an efficient way to test if rows in a matrix are sorted? [Update: see Aaron’s Rcpp answer – straightforward & very fast.]

I am porting some code that uses issorted(,'rows') from Matlab. As it seems that is.unsorted does not extend beyond vectors, I’m writing or looking for something else. The naive method is to check that the sorted version of the matrix (or data frame) is the same as the original, but that’s obviously inefficient.

NB: For sorting, a la sortrows() in Matlab, my code essentially uses SortedDF <- DF[do.call(order, DF),] (it’s wrapped in a larger function that converts matrices to data frames, passes parameters to order, etc.). I wouldn’t be surprised if there are faster implementations (data table comes to mind).


Update 1: To clarify: I’m not testing for sorting intra-row or intra-columns. (Such sorting generally results in an algebraically different matrix.)

As an example for creating an unsorted matrix:

set.seed(0)
x <- as.data.frame(matrix(sample(3, 60, replace = TRUE), ncol = 6, byrow = TRUE))

Its sorted version is:

y <- x[do.call(order, x),]

A proper test, say testSorted, would return FALSE for testSorted(x) and TRUE for testSorted(y).

Update 2:
The answers below are all good – they are concise and do the test. Regarding efficiency, it looks like these are sorting the data after all.

I’ve tried these with rather large matrices, such as 1M x 10, (just changing the creation of x above) and all have about the same time and memory cost. What’s peculiar is that they all consume more time for unsorted objects (about 5.5 seconds for 1Mx10) than for sorted ones (about 0.5 seconds for y). This suggests they’re sorting before testing.

I tested by creating a z matrix:

z <- y
z[,2] <- y[,1]
z[,1] <- y[,2]

In this case, all of the methods take about 0.85 seconds to complete. Anyway, finishing in 5.5 seconds isn’t terrible (in fact, that seems to be right about the time necessary to sort the object), but knowing that a sorted matrix is 11X faster suggests that a test that doesn’t sort could be even faster. In the case of the 1M row matrix, the first three rows of x are:

  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1  3  1  2  2  3  1  3  3  2   2
2  1  1  1  3  2  3  2  3  3   2
3  3  3  1  2  1  1  2  1  2   3

There’s no need to look beyond row 2, though vectorization isn’t a bad idea.

(I’ve also added the byrow argument for the creation of x, so that row values don’t depend on the size of x.)

Update 3:
Another comparison for this testing can be found with the sort -c command in Linux. If the file is already written (using write.table()), with 1M rows, then time sort -c myfile.txt takes 0.003 seconds for the unsorted data and 0.101 seconds for the sorted data. I don’t intend to write out to a file, but it’s a useful comparison.

Update 4:
Aaron’s Rcpp method bested all other methods offered here and that I’ve tried (including the sort -c comparison above: in-memory is expected to beat on-disk). As for the ratio relative to other methods, it’s hard to tell: the denominator is too small to give an accurate measurement, and I’ve not extensively explored microbenchmark. The speedups can be very large (4-5 orders of magnitude) for some matrices (e.g. one made with rnorm), but this is misleading – checking can terminate after only a couple of rows. I’ve had speedups with the example matrices of about 25-60 for the unsorted and about 1.1X for the sorted, as the competing methods were already very fast if the data is sorted.

Since this does the right thing (i.e. no sorting, just testing), and does it very quickly, it’s the accepted answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:38:22+00:00Added an answer on May 25, 2026 at 11:38 pm

    Newer: I decided I could use the Rcpp practice…

    library(Rcpp)
    library(inline)
    isRowSorted <- cxxfunction(signature(A="numeric"), body='
      Rcpp::NumericMatrix Am(A);
      for(int i = 1; i < Am.nrow(); i++) {
        for(int j = 0; j < Am.ncol(); j++) {
          if( Am(i-1,j) < Am(i,j) ) { break; }
          if( Am(i-1,j) > Am(i,j) ) { return(wrap(false)); }
        }
      }
      return(wrap(true));
    ', plugin="Rcpp")
    
    rownames(y) <- NULL # because as.matrix is faster without rownames
    isRowSorted(as.matrix(y))
    

    New: This R-only hack is the same speed for all matrices; it’s definitely faster for sorted matrices; for unsorted ones it depends on the nature of the unsortedness.

    iss3 <- function(x) {
      x2 <- sign(do.call(cbind, lapply(x, diff)))
      x3 <- t(x2)*(2^((ncol(x)-1):0))
      all(colSums(x3)>=0)
    }
    

    Original: This is faster for some unsorted matrices. How much faster will depends on where the unsorted elements are; this looks at the matrix column by column so unsortedness on the left side will be noticed much faster than unsorted on the right, while top/bottomness doesn’t matter nearly as much.

    iss2 <- function(y) {
      b <- c(0,nrow(y))
      for(i in 1:ncol(y)) {
        z <- rle(y[,i])
        b2 <- cumsum(z$lengths)
        sp <- split(z$values, cut(b2, breaks=b))
        for(spi in sp) {
          if(is.unsorted(spi)) return(FALSE)
        }
        b <- c(0, b2)
      }
      return(TRUE)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find an efficient way to pair together rows of data containing
I am searching for an efficient way to test if files exists which have
Possible Duplicate: Most efficient way to test equality of lambda expressions How to check
What is the most efficient way to check if the current QTP test execution
Possible Duplicate: Efficient way to test string for certain words I want to check
I'm trying to come up with an efficient way to index my JUnit test,
What's the most efficient way to test if an array contains any element from
What is an efficient way to test that a hash contains specific keys and
What is the most efficient way to get Elastic Map Reduce output into SimpleDB?
Is there a more efficient way to list files from a bucket in Amazon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.