Skip to content

Commit 667e76d

Browse files
committed
Array Examples
1 parent 6cfe79e commit 667e76d

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

Samples/src/main/java/okaram/samples/ArrayExamples.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public static int min(int[] arr) {
4949
}
5050
return theMin;
5151
}
52+
53+
public static boolean isSorted(int[] arr) {
54+
for(int i=1; i<arr.length; ++i) {
55+
if(arr[i]>arr[i-1])
56+
return false;
57+
}
58+
return true;
59+
}
5260

5361
public static void main(String args[]) {
5462
int arr1[]={1,2,3,4,5};

content/Arrays.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,32 @@ Most of our loops over arrays, end up looking like those above; using a variable
7070
}
7171
}
7272
```
73-
73+
# More examples
74+
```java
75+
public static int min(int[] arr) {
76+
// we assume the array is not empty
77+
int theMin=arr[0];
78+
for(int i=1; i<arr.length; ++i) {
79+
if(arr[i]<theMin)
80+
theMin=arr[i];
81+
}
82+
return theMin;
83+
}
84+
```
85+
```java
86+
public static boolean isSorted(int[] arr) {
87+
for(int i=1; i<arr.length; ++i) {
88+
if(arr[i]>arr[i-1])
89+
return false;
90+
}
91+
return true;
92+
}
93+
```
7494
# Array Problem patterns
95+
+ do something with all the elements
96+
+ do something to all the elements
97+
+ use an accumulator
98+
99+
# Common problems
100+
+ min / max
101+
+ sorting

0 commit comments

Comments
 (0)