{"id":310,"date":"2025-06-13T14:11:59","date_gmt":"2025-06-13T08:41:59","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=310"},"modified":"2025-06-13T14:12:00","modified_gmt":"2025-06-13T08:42:00","slug":"check-if-array-is-sorted","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/","title":{"rendered":"Check if array is sorted | Explained using Python Code"},"content":{"rendered":"\n<p>Learn a one-pass way to check if array is sorted in non-decreasing order. Intuition, commented Python code, dry run, and clear Big-O analysis included.<\/p>\n\n\n\n<p>So let&#8217;s get started with the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/check-if-an-array-is-sorted0701\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>].<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-45cab0e5-ba78-4994-bc8a-4f90af701223\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#0-1-what-does-the-problem-ask\" style=\"\">1. What does the problem ask?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#1-2-quick-examples\" style=\"\">2. Quick examples<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#2-3-intuition-amp-approach\" style=\"\">3. Intuition &amp; approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#3-4-code\" style=\"\">4. Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#4-5-step-by-step-explanation\" style=\"\">5. Step-by-step explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#5-6-dry-run-on-3-5-5-2-6-\" style=\"\">6. Dry run on [3, 5, 5, 2, 6]<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#6-7-complexity\" style=\"\">7. Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/check-if-array-is-sorted\/#7-8-conclusion\" style=\"\">8. Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1-what-does-the-problem-ask\">1. What does the problem ask?<\/h2>\n\n\n\n<p>Given an integer array <code>arr<\/code>, decide if it is <strong>sorted in non-decreasing order<\/strong> (each element is <strong>no larger<\/strong> than the next).<br>Return <strong><code>True<\/code><\/strong> if the whole array obeys this rule, otherwise return <strong><code>False<\/code><\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-2-quick-examples\">2. Quick examples<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>arr<\/code><\/th><th>Output<\/th><th>Why<\/th><\/tr><\/thead><tbody><tr><td><code>[1, 2, 2, 5]<\/code><\/td><td><code>True<\/code><\/td><td>Every element \u2264 the next one.<\/td><\/tr><tr><td><code>[3, 4, 1]<\/code><\/td><td><code>False<\/code><\/td><td><code>4 &gt; 1<\/code> breaks the rule.<\/td><\/tr><tr><td><code>[7]<\/code><\/td><td><code>True<\/code><\/td><td>A single element is always sorted.<\/td><\/tr><tr><td><code>[]<\/code><\/td><td><code>True<\/code><\/td><td>An empty list is trivially sorted.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-3-intuition-amp-approach\">3. Intuition &amp; approach<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Sliding window of size 2<\/strong> \u2013 look at every <em>neighbour<\/em> pair <code>arr[i]<\/code> and <code>arr[i + 1]<\/code>.<\/li>\n\n\n\n<li>The array is sorted <strong>only<\/strong> if <strong>all<\/strong> pairs satisfy <code>arr[i] \u2264 arr[i+1]<\/code>.<\/li>\n\n\n\n<li>The <strong>first<\/strong> pair that violates this instantly proves the array is not sorted, so we can stop early.<\/li>\n<\/ol>\n\n\n\n<p>Think of walking from left to right; the moment you step down a hill, you know the landscape is not purely uphill.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-4-code\">4. Code<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def arraySortedOrNot(self, arr) -&gt; bool:\n        n = len(arr)\n        # Scan pairs (i, i+1)\n        for i in range(n - 1):           # last index is n-2\n            if arr[i] &gt; arr[i + 1]:      # found a drop\n                return False\n        return True                      # never dropped \u2192 sorted\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">arraySortedOrNot<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">bool<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Scan pairs (i, i+1)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):           <\/span><span style=\"color: #6A9955\"># last index is n-2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[i] &gt; arr[i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]:      <\/span><span style=\"color: #6A9955\"># found a drop<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">                      <\/span><span style=\"color: #6A9955\"># never dropped \u2192 sorted<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-5-step-by-step-explanation\">5. Step-by-step explanation<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Length check<\/strong> \u2013 no special case needed; the loop runs <code>0<\/code> times when <code>n \u2264 1<\/code>, returning <code>True<\/code>.<\/li>\n\n\n\n<li><strong>Loop<\/strong> from index <code>0<\/code> to <code>n \u2212 2<\/code>.\n<ul class=\"wp-block-list\">\n<li>Compare current element with the next.<\/li>\n\n\n\n<li>If <strong>ever<\/strong> <code>arr[i] > arr[i+1]<\/code>, immediately return <code>False<\/code> (unsorted).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If the loop finishes without early exit, every pair was in order \u2192 return <code>True<\/code>.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-6-dry-run-on-3-5-5-2-6-\">6. Dry run on <code>[3, 5, 5, 2, 6]<\/code><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>i<\/code><\/th><th>Compare<\/th><th>Result<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td><code>3 \u2264 5<\/code><\/td><td>OK<\/td><\/tr><tr><td>1<\/td><td><code>5 \u2264 5<\/code><\/td><td>OK<\/td><\/tr><tr><td>2<\/td><td><code>5 \u2264 2<\/code><\/td><td><strong>False \u2192 exit<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The function returns <code>False<\/code> after meeting the first violation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-7-complexity\">7. Complexity<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Metric<\/th><th>Value<\/th><th>Reason<\/th><\/tr><\/thead><tbody><tr><td><strong>Time<\/strong><\/td><td><strong>O(n)<\/strong><\/td><td>Single pass over the array.<\/td><\/tr><tr><td><strong>Space<\/strong><\/td><td><strong>O(1)<\/strong><\/td><td>Only a few counters; no extra data structures.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-8-conclusion\">8. Conclusion<\/h2>\n\n\n\n<p>To check if an array is sorted, you only need to compare each element with its neighbour once. This linear scan is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simple<\/strong> \u2013 five short lines of code.<\/li>\n\n\n\n<li><strong>Efficient<\/strong> \u2013 runs in <code>O(n)<\/code> time and <code>O(1)<\/code> space.<\/li>\n\n\n\n<li><strong>Early-exit capable<\/strong> \u2013 stops the moment an out-of-order pair appears.<\/li>\n<\/ul>\n\n\n\n<p>A perfect example where the straightforward approach is already optimal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn a one-pass way to check if array is sorted in non-decreasing order. Intuition, commented Python code, dry run, and clear Big-O analysis included. So let&#8217;s get started with the [Problem Link]. 1. What does the problem ask? Given an integer array arr, decide if it is sorted in non-decreasing order (each element is no<\/p>\n","protected":false},"author":1,"featured_media":311,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,8],"class_list":{"0":"post-310","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-easy"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/check-if-array-is-sorted-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions\/313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/311"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}