Skip to content

Commit 1f02344

Browse files
selection
1 parent 9df5fb9 commit 1f02344

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

lectures/10-binary search/code/src/com/kunal/RBS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// https://leetcode.com/problems/search-in-rotated-sorted-array/submissions/
33
public class RBS {
44
public static void main(String[] args) {
5-
int[] arr = {4,5,6,7,0,1,2};
6-
System.out.println(findPivot(arr));
5+
int[] arr = {1,2,3,4,5,5,6};
6+
System.out.println(findPivotWithDuplicates(arr));
77
}
88

99
static int search(int[] nums, int target) {

lectures/11-sorting/Bubble Sort.pdf

1.71 MB
Binary file not shown.
428 KB
Binary file not shown.

lectures/11-sorting/code/src/com/kunal/Main.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,36 @@
55
public class Main {
66

77
public static void main(String[] args) {
8-
int[] arr = {3, 1, 5, 4, 2};
9-
bubble(arr);
8+
int[] arr = {4, 5, 1, 2, 3};
9+
selection(arr);
1010
System.out.println(Arrays.toString(arr));
1111
}
1212

13+
static void selection(int[] arr) {
14+
for (int i = 0; i < arr.length; i++) {
15+
// find the max item in the remaining array and swap with correct index
16+
int last = arr.length - i - 1;
17+
int maxIndex = getMaxIndex(arr, 0, last);
18+
swap(arr, maxIndex, last);
19+
}
20+
}
21+
22+
static void swap(int[] arr, int first, int second) {
23+
int temp = arr[first];
24+
arr[first] = arr[second];
25+
arr[second] = temp;
26+
}
27+
28+
static int getMaxIndex(int[] arr, int start, int end) {
29+
int max = start;
30+
for (int i = start; i <= end; i++) {
31+
if (arr[max] < arr[i]) {
32+
max = i;
33+
}
34+
}
35+
return max;
36+
}
37+
1338
static void bubble(int[] arr) {
1439
boolean swapped;
1540
// run the steps n-1 times

0 commit comments

Comments
 (0)