Skip to content

Added file CheckIfArraySorted #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/concepts/CheckIfArraySorted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Checking if array is sorted
// Working even elements are repeated

const isSort = arr => {
if (arr.length <= 1) return "Array is too small";
const idx = arr.findIndex( (val,id,arr) => val != arr[0]);
const AscDesc = arr[idx] - arr[idx-1];
for (let i = idx; i < arr.length; i++) {
if ((arr[i] - arr[i - 1]) * AscDesc < 0) return "Array not sorted";
}
return AscDesc > 0 ? "Array is sorted Ascending" : "Array is sorted Descending"
};



//example
console.log(isSort([1,1,1,3,4,7,9,57])); // "Array is sorted Ascending"