Yuvraj Singh Kushwah’s Post

🚀 Day 113 | Java Logical Program ++>> Check Whether an Array Is Sorted or Not Hello coders👋, Today I worked on a simple but very important array logic problem in Java. The task is to check whether an array is sorted in ascending order or not. ------------------------------------------------------------------------------------------- 💡 Code -----------> public class ArraySortedOrNot { public static void main(String[] args) { int[] a = {1, 2, 3, 4, 5, 6}; boolean sorted = true; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { sorted = false; break; } } System.out.println(sorted ? "Sorted Array" : "Not Sorted Array"); } } ------------------------------------------------------------------------------------------- 🖥️ Output --------------> Sorted Array 🔍 Output Explanation The loop compares current element with the next element If any element is greater than the next one → array is not sorted If the loop completes without breaking → array is sorted 📌 Important Concept Array traversal Comparison of adjacent elements Boolean flag usage Early exit using break ⚠️ Important Tip This logic works in O(n) time, which is efficient and recommended for large arrays 🚀 🤔 Engagement How would you check if the array is sorted in descending order? 🤔 👉 CTA If you’re learning Java logic step by step, let’s connect and grow together 🤝💙 #Java #CoreJava #ArrayProgramming #LogicalThinking #JavaDeveloper #CodingJourney #100DaysOfCode #LearnJava #day113 #codewithyuvi

  • text

To view or add a comment, sign in

Explore content categories