Skip to content

Commit 6243a60

Browse files
authored
Update 2022-10-26-check-if-array-is-sorted-java.md
1 parent 8b3b3b8 commit 6243a60

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

‎_posts/java-programs/2022-10-26-check-if-array-is-sorted-java.md

+35-45
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ title: "Check If An Array Is Sorted In Java"
44
author: gaurav
55
categories: [Java Programs, Array]
66
toc: true
7-
description: "In this tutorial, we will see a Java program to check if an array is sorted in java."
7+
description: "In this tutorial, we will see a Java program to check if an array is sorted in Java."
88
---
99

10-
In this tutorial, we will see a Java program to check if an array is sorted in java.
10+
In this tutorial, we will see a Java program to check if an array is sorted in Java.
1111

1212
## Introduction
1313

@@ -27,40 +27,37 @@ We will take a sample integer array `arr = {1, 3 , 5, 9};` We will return `true`
2727
*/
2828
public class SortedArrayTester {
2929

30-
public static void main(String[] args) {
31-
32-
int[] arr = {2, 5, 9, 8, 11, 18, 13};
33-
34-
boolean isSorted = isArraySorted(arr);
35-
36-
// use this to check the sorted case
37-
38-
/*
39-
* int[] arr2 = {2, 5, 9, 11, 18};
40-
* boolean isSorted = isArraySorted(arr2);
41-
*/
42-
43-
if(isSorted) {
44-
System.out.println("The array is sorted.");
45-
}
46-
else {
47-
System.out.println("The array is not sorted.");
48-
}
49-
}
50-
51-
private static boolean isArraySorted(int[] arr) {
52-
int n = arr.length;
53-
54-
for(int i=0; i<n-1; i++) {
55-
56-
//in sorted array current number should be
57-
//always less than the next number in array
58-
if(arr[i]>arr[i+1]) {
59-
return false;
60-
}
61-
}
62-
return true;
63-
}
30+
public static void main(String[] args) {
31+
int[] arr = {2, 5, 9, 8, 11, 18, 13};
32+
33+
boolean isSorted = isArraySorted(arr);
34+
35+
//Use this to check the sorted case
36+
37+
/*
38+
* int[] arr2 = {2, 5, 9, 11, 18};
39+
* boolean isSorted = isArraySorted(arr2);
40+
*/
41+
if(isSorted) {
42+
System.out.println("The array is sorted.");
43+
}
44+
else {
45+
System.out.println("The array is not sorted.");
46+
}
47+
}
48+
49+
private static boolean isArraySorted(int[] arr) {
50+
int n = arr.length;
51+
52+
for(int i=0; i<n-1; i++) {
53+
//in a sorted array current number should be
54+
//always less than the next number in the array
55+
if(arr[i]>arr[i+1]) {
56+
return false;
57+
}
58+
}
59+
return true;
60+
}
6461
}
6562
```
6663

@@ -75,22 +72,15 @@ The array is not sorted.
7572
1. In the `main` method we are taking a sample array `arr` and passing it to the `isArraySorted()` method to check if the array is sorted or not. This method returns a `boolean` value. `true` - if an array is sorted, `false` - if an array is not sorted
7673
2. In the `isArraySorted()` method, we are iterating over an array from index `0` to `n-1`, (`n`-length of the array) and we will check if the current number (number at index `i`) is greater than the next number ( number at index `i+1`).
7774
3. If the current number (number at index `i`) is greater than the next number ( number at index `i+1`), that means the array is not sorted, and we will return `false`.
78-
4. If the control came out of for loop, that means the array is sorted and we will return `true`.
75+
4. If the control came out of the for loop, that means the array is sorted and we will return `true`.
7976
5. When we receive the `boolean` result `isSorted` we will print the output.
8077

81-
82-
83-
## For array of custom object
84-
85-
1. if custom object implements comparable (Coming Soon)
86-
2. if custom object does not implement comparable (Coming Soon)
87-
8878
## Conclusion
8979

9080
we can check if the primitive arrays are sorted or not by comparing the adjacent array elements.
9181

9282
---
9383

94-
The example java programs used in the above article can be found at this GitHub repository, [blogpost-coding-examples/java-programs/second-largest-element-in-an-array/](https://github.com/coderolls/blogpost-coding-examples/tree/main/java-programs/second-largest-element-in-an-array).
84+
The example Java programs used in the above article can be found at this GitHub repository, [blogpost-coding-examples/java-programs/second-largest-element-in-an-array/](https://github.com/coderolls/blogpost-coding-examples/tree/main/java-programs/second-largest-element-in-an-array).
9585

9686
Please write your thoughts in the comment section below.

0 commit comments

Comments
 (0)