Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if array is already sorted

so how to make such logic

int[] arr = {2, 5, 3};

if (/* arr is sorted */)
    ....
else 
    ...

Its bad that method Array.sort is void

like image 650
OxomHuK Avatar asked Aug 07 '13 18:08

OxomHuK


People also ask

How do you check if the array is already sorted or not?

You don't need to sort your array to check if it's sorted. Loop over each consecutive pair of elements and check if the first is less than the second; if you find a pair for which this isn't true, the array is not sorted.

How check array is sorted or not in C#?

Approach used below is as follows to solve the problem − Take an array arr[] as an input and initialize n with the size of an array. Check if we reached the starting of an array, return true. Check if the previous element of an array is not smaller than the next element, return false. Decrement n and goto step 2.

Is array sorted Java?

sort() in Java with examples. Array class is a class containing static methods that are used with arrays in order to search, sort, compare, insert elements, or return a string representation of an array.

How check array is sorted or not in PHP?

php $sort = array( 0 => 50, 1 => 100, 2 => 150 ); $default = $sort; sort($sort); $flag = true; foreach($sort as $key=>$value) if($value!= $default[$key]) $flag = false; if($flag) echo "Already sorted"; else echo "Not Already sorted"; ?>


7 Answers

You don't need to sort your array to check if it's sorted. Loop over each consecutive pair of elements and check if the first is less than the second; if you find a pair for which this isn't true, the array is not sorted.

boolean sorted = true;

for (int i = 0; i < arr.length - 1; i++) {
    if (arr[i] > arr[i+1]) {
        sorted = false;
        break;
    }
}
like image 176
arshajii Avatar answered Sep 20 '22 16:09

arshajii


public static <T>
boolean isArraySorted(T[] elements, Comparator<? super T> cmp) {
  int n = elements.length;
  for (int i = 1; i < n; ++i) {
    if (cmp.compare(elements[i-1], elements[i]) > 0) { return false; }
  }
  return true;
}
like image 25
Mike Samuel Avatar answered Sep 18 '22 16:09

Mike Samuel


Well you can check it in O(n) worst case linear time. A non-sorted array (assuming you mean sorting in ascending order) will have a trip point. That is at some point arr[i] > arr[i+1]

All you need to do is

boolean is_array_sorted(int arr[]) {
  for(int i=0; i < arr.len-1; i++) {
    if(arr[i] > arr[i+1]) {
       return false;
    }
  }
  return true;
}

Just change the > to < if your array sort is supposed to be descending order

like image 20
Anshul Avatar answered Sep 18 '22 16:09

Anshul


public static boolean isSorted(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        if (a[i + 1] < a[i]) {
            return false;
        };
    }
    return true;
}
like image 29
flavian Avatar answered Sep 19 '22 16:09

flavian


A shorter version:

[0,1,2,3,4].reduce((a,v) => (a!==false) && (a <= v) ? v : false, -Infinity)
[4,3,1,2,0].reduce((a,v) => (a!==false) && (a >= v) ? v : false, +Infinity)

Be careful, as in some cases it isn't effective because it will loop through entire array without breaking prematurely.

Array.prototype.reduce()

like image 29
user2276686 Avatar answered Sep 18 '22 16:09

user2276686


You can use .every

let isSorted = array.every((v, i) => (i === 0 || v <= array[i - 1]))
  || array.every((v, i) => (i === 0 || v >= array[i - 1]))
like image 31
xhg Avatar answered Sep 17 '22 16:09

xhg


function isArraySorted(arr) {
    for (let i=0;i<arr.length;i++) {
        if (arr[i+1] && (arr[i+1] > arr[i])) {
            continue;
        } else if(arr[i+1] && (arr[i+1] < arr[i])) {
            return false;
        }
    }

    return true;
}

This worked for me.

like image 27
Nilanjan Banerjee Avatar answered Sep 16 '22 16:09

Nilanjan Banerjee