Skip to content

Commit b228fa7

Browse files
committed
Added Nth Smallest number (some test cases pending)
1 parent fec696a commit b228fa7

File tree

3 files changed

+91
-28
lines changed

3 files changed

+91
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.rampatra.arrays;
2+
3+
/**
4+
* A Google Interview Question. For a simpler version of this question see {@link SmallestAndSecondSmallest}.
5+
*
6+
* @author rampatra
7+
* @since 2019-02-01
8+
*/
9+
public class NthSmallestNumber {
10+
11+
private static int findNthSmallestNumber(int[] arr, int n, int start, int end) {
12+
if (arr.length == 0 || arr.length < n) {
13+
return -1;
14+
}
15+
int temp;
16+
for (int i = start; i < end; i++) {
17+
if (arr[i] < arr[end]) {
18+
temp = arr[i];
19+
arr[i] = arr[start];
20+
arr[start] = temp;
21+
start++;
22+
}
23+
}
24+
temp = arr[end];
25+
arr[end] = arr[start];
26+
arr[start] = temp;
27+
28+
if (start + 1 < n) {
29+
return findNthSmallestNumber(arr, n, 0, start);
30+
} else if (start + 1 > n) {
31+
return findNthSmallestNumber(arr, n, start + 1, end);
32+
} else {
33+
return arr[start];
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
System.out.println(findNthSmallestNumber(new int[]{}, 3, 0, 5));
39+
System.out.println(findNthSmallestNumber(new int[]{0, 1}, 3, 0, 1));
40+
System.out.println(findNthSmallestNumber(new int[]{0, 1}, 2, 0, 1));
41+
System.out.println(findNthSmallestNumber(new int[]{1, 0}, 2, 0, 1));
42+
System.out.println(findNthSmallestNumber(new int[]{2, 3, 5, 10, 9, 4}, 3, 0, 5));
43+
System.out.println(findNthSmallestNumber(new int[]{2, 3, 4, 10, 9, 4}, 3, 0, 5));
44+
System.out.println(findNthSmallestNumber(new int[]{4, 8, 1, 9, 10, 2, 7, 3, 2, 6}, 3, 0, 9));
45+
// System.out.println(findNthSmallestNumber(new int[]{4, 4, 4, 4, 4, 4}, 3, 0, 5));
46+
}
47+
}

src/main/java/com/rampatra/arrays/SmallestAndSecondSmallest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
*
88
* @author rampatra
99
* @since 7/30/15
10-
* @time: 11:13 PM
1110
*/
1211
public class SmallestAndSecondSmallest {
1312

14-
public static int[] getSmallestAndSecondSmallest(int[] a) {
13+
private static int[] getSmallestAndSecondSmallest(int[] a) {
1514
int smallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE;
1615

1716
for (int i = 0; i < a.length; i++) {
@@ -26,7 +25,7 @@ public static int[] getSmallestAndSecondSmallest(int[] a) {
2625
return new int[]{smallest, secondSmallest};
2726
}
2827

29-
public static void main(String a[]) {
28+
public static void main(String[] a) {
3029
System.out.println(Arrays.toString(getSmallestAndSecondSmallest(new int[]{100, 1, 60, -10, -80, 85, 70, -80})));
3130
System.out.println(Arrays.toString(getSmallestAndSecondSmallest(new int[]{100, 1, 60, 10, 80, 85, 70, 0})));
3231
}

src/main/java/com/rampatra/sorting/QuickSort.java

+42-25
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import java.util.Arrays;
44

55
/**
6-
* Created by IntelliJ IDEA.
6+
* The basic Quick Sort. See http://www.csanimated.com/animation.php?t=Quicksort to learn more.
77
*
88
* @author rampatra
99
* @since 7/21/15
10-
* @time: 4:12 PM
11-
* @see: http://www.csanimated.com/animation.php?t=Quicksort
1210
*/
1311
public class QuickSort {
1412

@@ -18,32 +16,30 @@ public class QuickSort {
1816
* pivot element to its right and finally places the pivot element
1917
* at its correct position.
2018
*
21-
* @param ar
19+
* @param arr
2220
* @param startIndex
2321
* @param endIndex
2422
* @return position of the pivot element
2523
*/
26-
public static int partition(int[] ar, int startIndex, int endIndex) {
24+
public static int partition(int[] arr, int startIndex, int endIndex) {
2725
int pivot = endIndex, temp;
2826

2927
for (int i = startIndex; i < endIndex; i++) {
30-
/**
31-
* if ith element is smaller than pivot element then
32-
* swap it with the last larger element known
33-
*/
34-
if (ar[i] < ar[pivot]) {
28+
// if ith element is smaller than pivot element then
29+
// swap it with the last larger element known
30+
if (arr[i] < arr[pivot]) {
3531
// swap a[startIndex] with a[i]
36-
temp = ar[startIndex];
37-
ar[startIndex] = ar[i];
38-
ar[i] = temp;
32+
temp = arr[startIndex];
33+
arr[startIndex] = arr[i];
34+
arr[i] = temp;
3935
startIndex++;
4036
}
4137
}
4238

4339
// place the pivot element in its correct position
44-
temp = ar[startIndex];
45-
ar[startIndex] = ar[pivot];
46-
ar[pivot] = temp;
40+
temp = arr[startIndex];
41+
arr[startIndex] = arr[pivot];
42+
arr[pivot] = temp;
4743

4844
return startIndex;
4945
}
@@ -57,31 +53,52 @@ public static int partition(int[] ar, int startIndex, int endIndex) {
5753
* Best Case: O(nlogn)
5854
* Worst Case: O(n*n)
5955
*
60-
* @param ar
56+
* @param arr
6157
* @param startIndex
6258
* @param endIndex
6359
*/
64-
public static void quickSort(int[] ar, int startIndex, int endIndex) {
60+
public static void quickSort(int[] arr, int startIndex, int endIndex) {
6561
if (startIndex < endIndex) {
66-
int partition = partition(ar, startIndex, endIndex);
67-
quickSort(ar, startIndex, partition - 1);
68-
quickSort(ar, partition + 1, endIndex);
62+
int partition = partition(arr, startIndex, endIndex);
63+
quickSort(arr, startIndex, partition - 1);
64+
quickSort(arr, partition + 1, endIndex);
6965
}
7066
}
7167

7268
/**
7369
* Wrapper method to quick sort the entire array.
7470
*
75-
* @param a
71+
* @param arr the input array to sort
7672
*/
77-
public static void quickSort(int[] a) {
78-
quickSort(a, 0, a.length - 1);
73+
public static void quickSort(int[] arr) {
74+
quickSort(arr, 0, arr.length - 1);
7975
}
8076

81-
public static void main(String a[]) {
77+
public static void main(String[] a) {
8278
int[] ar = {3, 2, 1, 6, 4, 9, 7, 8};
8379
System.out.println(Arrays.toString(ar));
8480
quickSort(ar);
8581
System.out.println(Arrays.toString(ar));
82+
83+
System.out.println("------");
84+
85+
ar = new int[]{3, 2, 6, 8, 4, 3, 7, 8};
86+
System.out.println(Arrays.toString(ar));
87+
quickSort(ar);
88+
System.out.println(Arrays.toString(ar));
89+
90+
System.out.println("------");
91+
92+
ar = new int[]{4, 4, 4, 4, 4, 4, 4};
93+
System.out.println(Arrays.toString(ar));
94+
quickSort(ar);
95+
System.out.println(Arrays.toString(ar));
96+
97+
System.out.println("------");
98+
99+
ar = new int[]{};
100+
System.out.println(Arrays.toString(ar));
101+
quickSort(ar);
102+
System.out.println(Arrays.toString(ar));
86103
}
87104
}

0 commit comments

Comments
 (0)