You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/java-programs/2022-10-26-check-if-array-is-sorted-java.md
+35-45
Original file line number
Diff line number
Diff line change
@@ -4,10 +4,10 @@ title: "Check If An Array Is Sorted In Java"
4
4
author: gaurav
5
5
categories: [Java Programs, Array]
6
6
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."
8
8
---
9
9
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.
11
11
12
12
## Introduction
13
13
@@ -27,40 +27,37 @@ We will take a sample integer array `arr = {1, 3 , 5, 9};` We will return `true`
27
27
*/
28
28
publicclassSortedArrayTester {
29
29
30
-
publicstaticvoidmain(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
-
privatestaticbooleanisArraySorted(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
-
returnfalse;
60
-
}
61
-
}
62
-
returntrue;
63
-
}
30
+
publicstaticvoidmain(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
+
privatestaticbooleanisArraySorted(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
+
returnfalse;
57
+
}
58
+
}
59
+
returntrue;
60
+
}
64
61
}
65
62
```
66
63
@@ -75,22 +72,15 @@ The array is not sorted.
75
72
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
76
73
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`).
77
74
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`.
79
76
5. When we receive the `boolean` result `isSorted` we will print the output.
80
77
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
-
88
78
## Conclusion
89
79
90
80
we can check if the primitive arrays are sorted or not by comparing the adjacent array elements.
91
81
92
82
---
93
83
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).
95
85
96
86
Please write your thoughts in the comment section below.
0 commit comments