From 8f7864fc9c442f3c7ee9c9cdc5a99ce34589e9fa Mon Sep 17 00:00:00 2001 From: Amal Bijoy Date: Sun, 5 Apr 2026 23:22:34 +0530 Subject: [PATCH] Add consistent precondition guards across sorting/searching --- Searching-algorithms/Binarysearch.java | 22 +++++++++++++++ Searching-algorithms/Fibonaccisearch.java | 22 +++++++++++++++ Searching-algorithms/Interpolationsearch.java | 22 +++++++++++++++ Searching-algorithms/Linearsearch.java | 20 +++++++++++++ .../Uniform_binarysearch.java | 22 +++++++++++++++ Sorting-algorithms/Bubblesort.java | 20 +++++++++++++ Sorting-algorithms/Heapsort.java | 23 +++++++++++++++ Sorting-algorithms/Insertionsort.java | 20 +++++++++++++ Sorting-algorithms/Mergesort.java | 28 +++++++++++++++---- Sorting-algorithms/Quicksort_Hoare.java | 23 +++++++++++++++ Sorting-algorithms/Quicksort_Lomuto.java | 26 ++++++++++++++--- Sorting-algorithms/Radixsort.java | 20 +++++++++++++ Sorting-algorithms/Selectionsort.java | 20 +++++++++++++ Sorting-algorithms/Shellsort.java | 20 +++++++++++++ 14 files changed, 299 insertions(+), 9 deletions(-) diff --git a/Searching-algorithms/Binarysearch.java b/Searching-algorithms/Binarysearch.java index c86bbe5..81a2d16 100644 --- a/Searching-algorithms/Binarysearch.java +++ b/Searching-algorithms/Binarysearch.java @@ -2,10 +2,26 @@ import java.util.Arrays; public class Binarysearch { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays return not found (-1). + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int size = sc.nextInt(); + if (size < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (size == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter Array elements in non-decreasing (sorted) order: "); int arr[] = new int[size]; for (int i = 0; i < size; i++) { @@ -30,6 +46,9 @@ public static void main(String[] args) { sc.close(); } public static int binarysearch(int[] arr, int key) { + if (arr == null || arr.length == 0) { + return -1; + } int low = 0; int high = arr.length - 1; while (low <= high) { @@ -45,6 +64,9 @@ else if (key < arr[mid]) } public static boolean isSorted(int[] arr) { + if (arr == null || arr.length < 2) { + return true; + } for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) { return false; diff --git a/Searching-algorithms/Fibonaccisearch.java b/Searching-algorithms/Fibonaccisearch.java index fc06bdb..03184f3 100644 --- a/Searching-algorithms/Fibonaccisearch.java +++ b/Searching-algorithms/Fibonaccisearch.java @@ -2,11 +2,27 @@ import java.util.Scanner; public class Fibonaccisearch { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays return not found (-1). + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the array: "); int size = sc.nextInt(); + if (size < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (size == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } int[] arr = new int[size]; System.out.println("Enter array elements in non-decreasing (sorted) order: "); @@ -36,6 +52,9 @@ public static void main(String[] args) { } public static int fibonaccisearch(int[] arr, int key) { + if (arr == null || arr.length == 0) { + return -1; + } int n = arr.length; int fnminus2 = 0; int fnminus1 = 1; @@ -74,6 +93,9 @@ public static int fibonaccisearch(int[] arr, int key) { } public static boolean isSorted(int[] arr) { + if (arr == null || arr.length < 2) { + return true; + } for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) { return false; diff --git a/Searching-algorithms/Interpolationsearch.java b/Searching-algorithms/Interpolationsearch.java index c4576a2..cb12f1b 100644 --- a/Searching-algorithms/Interpolationsearch.java +++ b/Searching-algorithms/Interpolationsearch.java @@ -2,10 +2,26 @@ import java.util.Arrays; public class Interpolationsearch { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays return not found (-1). + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter Array elements in non-decreasing (sorted) order: "); int arr[] = new int[n]; for (int i = 0; i < n; i++) { @@ -30,6 +46,9 @@ public static void main(String[] args) { sc.close(); } public static int interpolationSearch(int arr[], int lo, int hi, int x) { + if (arr == null || arr.length == 0 || lo > hi) { + return -1; + } int pos; if (lo <= hi && x >= arr[lo] && x <= arr[hi]) { if (arr[lo] == arr[hi]) { @@ -49,6 +68,9 @@ public static int interpolationSearch(int arr[], int lo, int hi, int x) { } public static boolean isSorted(int[] arr) { + if (arr == null || arr.length < 2) { + return true; + } for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) { return false; diff --git a/Searching-algorithms/Linearsearch.java b/Searching-algorithms/Linearsearch.java index 48b3846..a0680ae 100644 --- a/Searching-algorithms/Linearsearch.java +++ b/Searching-algorithms/Linearsearch.java @@ -2,10 +2,26 @@ import java.util.Arrays; public class Linearsearch { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays return not found (-1). + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int size = sc.nextInt(); + if (size < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (size == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter Array elements: "); int arr[] = new int[size]; for (int i = 0; i < size; i++) { @@ -20,8 +36,12 @@ public static void main(String[] args) { } else { System.out.println("Element " + key + " was NOT Found in the entered Array."); } + sc.close(); } public static int linearsearch(int[] arr, int key){ + if (arr == null || arr.length == 0) { + return -1; + } for (int i = 0; i < arr.length; i++) { if (arr[i] == key) { return i; diff --git a/Searching-algorithms/Uniform_binarysearch.java b/Searching-algorithms/Uniform_binarysearch.java index 0f9ecab..aae2038 100644 --- a/Searching-algorithms/Uniform_binarysearch.java +++ b/Searching-algorithms/Uniform_binarysearch.java @@ -2,10 +2,26 @@ import java.util.Arrays; public class Uniform_binarysearch { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays return not found (-1). + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the length of the Array: "); int size = sc.nextInt(); + if (size < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (size == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter Array elements in non-decreasing (sorted) order: "); int arr[] = new int[size]; for (int i = 0; i < size; i++) { @@ -30,6 +46,9 @@ public static void main(String[] args) { sc.close(); } public static int binunisearch(int[] arr, int key) { + if (arr == null || arr.length == 0) { + return -1; + } int n = arr.length; int k = (int)(Math.log(n) / Math.log(2)); System.out.println("n = " + n); @@ -52,6 +71,9 @@ public static int binunisearch(int[] arr, int key) { } public static boolean isSorted(int[] arr) { + if (arr == null || arr.length < 2) { + return true; + } for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) { return false; diff --git a/Sorting-algorithms/Bubblesort.java b/Sorting-algorithms/Bubblesort.java index 58a42a3..ce6c283 100644 --- a/Sorting-algorithms/Bubblesort.java +++ b/Sorting-algorithms/Bubblesort.java @@ -2,10 +2,26 @@ import java.util.Scanner; import java.util.Arrays; public class Bubblesort{ + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays are ignored by the algorithm. + */ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the array size : "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } int[] arr = new int[n]; for(int i = 0; i < n; i ++){ arr[i] = sc.nextInt(); @@ -13,8 +29,12 @@ public static void main(String[] args){ System.out.println("Entered array : "+ Arrays.toString(arr)); bubblesort(arr); System.out.println("Sorted array : "+ Arrays.toString(arr)); + sc.close(); } public static void bubblesort(int[] arr){ + if (arr == null || arr.length == 0) { + return; + } int n = arr.length; for(int i = 0; i < n - 1; i++){ for(int j = 0; j < n - 1 - i; j++){ diff --git a/Sorting-algorithms/Heapsort.java b/Sorting-algorithms/Heapsort.java index 6f7d6b4..3b0d22b 100644 --- a/Sorting-algorithms/Heapsort.java +++ b/Sorting-algorithms/Heapsort.java @@ -1,7 +1,16 @@ import java.util.Scanner; import java.util.Arrays; public class Heapsort { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays are ignored by sort/heapify. + */ public void sort(int arr[]) { + if (arr == null || arr.length == 0) { + return; + } int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); @@ -14,6 +23,9 @@ public void sort(int arr[]) { } } void heapify(int arr[], int n, int i) { + if (arr == null || n <= 0) { + return; + } int max = i; int leftChild = 2 * i + 1; int rightChild = 2 * i + 2; @@ -32,6 +44,16 @@ public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of elements in the array : "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter array elements : "); int[] arr = new int[n]; for(int i = 0; i = right) { + return; } + int mid = left + (right - left) / 2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr, left, mid, right); } public static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; diff --git a/Sorting-algorithms/Quicksort_Hoare.java b/Sorting-algorithms/Quicksort_Hoare.java index fa8f3e4..c27fda3 100644 --- a/Sorting-algorithms/Quicksort_Hoare.java +++ b/Sorting-algorithms/Quicksort_Hoare.java @@ -2,10 +2,26 @@ import java.util.Scanner; import java.util.Arrays; public class Quicksort_Hoare{ + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays and one-element ranges stop recursion. + */ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array : "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter array elements : "); int[] arr = new int[n]; for(int i = 0; i < n; i ++){ @@ -14,6 +30,7 @@ public static void main(String[] args){ System.out.println("Entered array : "+ Arrays.toString(arr)); quicksortrecursion(arr,0,n-1); System.out.println("Sorted Array : "+Arrays.toString(arr)); + sc.close(); } public static int partition(int[] arr, int low,int high){ int pivot = arr[(low + high)/2]; @@ -35,6 +52,12 @@ public static int partition(int[] arr, int low,int high){ return low; } public static void quicksortrecursion(int[] arr, int low, int high){ + if (arr == null || arr.length == 0) { + return; + } + if (low >= high) { + return; + } int pi = partition(arr,low,high); if(low < pi-1){ quicksortrecursion(arr,low,pi-1); diff --git a/Sorting-algorithms/Quicksort_Lomuto.java b/Sorting-algorithms/Quicksort_Lomuto.java index dcfd5fd..7c5d67c 100644 --- a/Sorting-algorithms/Quicksort_Lomuto.java +++ b/Sorting-algorithms/Quicksort_Lomuto.java @@ -18,16 +18,33 @@ private static int partition(int[] array, int low, int high) { return i + 1; } private static void quickSort(int[] array, int low, int high) { - if (low < high) { - int pi = partition(array, low, high); - quickSort(array, low, pi - 1); - quickSort(array, pi + 1, high); + if (array == null || array.length == 0 || low >= high) { + return; } + int pi = partition(array, low, high); + quickSort(array, low, pi - 1); + quickSort(array, pi + 1, high); } + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays stop recursion immediately. + */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of elements: "); int n = scanner.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + scanner.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + scanner.close(); + return; + } int[] array = new int[n]; System.out.println("Enter the elements:"); for (int i = 0; i < n; i++) { @@ -35,5 +52,6 @@ public static void main(String[] args) { } quickSort(array, 0, n - 1); System.out.println("Sorted array in ascending order:"+ Arrays.toString(array)); + scanner.close(); } } diff --git a/Sorting-algorithms/Radixsort.java b/Sorting-algorithms/Radixsort.java index 5f43b62..3c514af 100644 --- a/Sorting-algorithms/Radixsort.java +++ b/Sorting-algorithms/Radixsort.java @@ -2,10 +2,26 @@ import java.util.Scanner; import java.util.Arrays; public class Radixsort { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays are ignored by radix sort. + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter array size : "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter array elements : "); int[] arr = new int[n]; for (int i = 0; i < n; i++) { @@ -14,8 +30,12 @@ public static void main(String[] args) { System.out.println("Entered array : " + Arrays.toString(arr)); radixsort(arr); System.out.println("Sorted array : " + Arrays.toString(arr)); + sc.close(); } public static void radixsort(int[] arr) { + if (arr == null || arr.length == 0) { + return; + } int max = getMax(arr); for (int exp = 1; max / exp > 0; exp *= 10) { countSort(arr, exp); diff --git a/Sorting-algorithms/Selectionsort.java b/Sorting-algorithms/Selectionsort.java index 0ba2db3..d69a262 100644 --- a/Sorting-algorithms/Selectionsort.java +++ b/Sorting-algorithms/Selectionsort.java @@ -2,10 +2,26 @@ import java.util.Scanner; import java.util.Arrays; public class Selectionsort{ + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays are ignored by the algorithm. + */ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the array size : "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } int[] arr = new int[n]; for(int i = 0; i < n; i ++){ arr[i] = sc.nextInt(); @@ -13,8 +29,12 @@ public static void main(String[] args){ System.out.println("Entered array : "+ Arrays.toString(arr)); selectionsort(arr); System.out.println("Sorted array : "+ Arrays.toString(arr)); + sc.close(); } public static void selectionsort(int[] arr){ + if (arr == null || arr.length == 0) { + return; + } int n = arr.length; for(int i = 0; i < n-1;i++){ int min_index = i; diff --git a/Sorting-algorithms/Shellsort.java b/Sorting-algorithms/Shellsort.java index c3408d1..c9fc7c8 100644 --- a/Sorting-algorithms/Shellsort.java +++ b/Sorting-algorithms/Shellsort.java @@ -2,10 +2,26 @@ import java.util.Scanner; import java.util.Arrays; public class Shellsort { + /* + * Edge-case behavior: + * - Negative size is rejected. + * - Size 0 exits early with "Nothing to sort/search". + * - Null/empty arrays are ignored by the algorithm. + */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter array size : "); int n = sc.nextInt(); + if (n < 0) { + System.out.println("Invalid input: size cannot be negative."); + sc.close(); + return; + } + if (n == 0) { + System.out.println("Nothing to sort/search"); + sc.close(); + return; + } System.out.println("Enter array elements : "); int[] arr = new int[n]; for (int i = 0; i < n; i++) { @@ -14,8 +30,12 @@ public static void main(String[] args) { System.out.println("Entered array : " + Arrays.toString(arr)); shellSort(arr); System.out.println("Sorted array : " + Arrays.toString(arr)); + sc.close(); } public static void shellSort(int[] arr) { + if (arr == null || arr.length == 0) { + return; + } int n = arr.length; for (int gap = n / 2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) {