|
| 1 | +/** |
| 2 | + * Stalin Sort Algorithm |
| 3 | + * |
| 4 | + * Description: |
| 5 | + * Stalin Sort is a humorous and simple sorting algorithm that removes elements from the list if they |
| 6 | + * are smaller than the previous one. This results in a list of elements where each element is greater |
| 7 | + * than or equal to the one before it. The goal of this algorithm is not to sort all elements but to |
| 8 | + * remove elements that break the "non-decreasing" sequence. |
| 9 | + * |
| 10 | + * Approach: |
| 11 | + * - The algorithm iterates through the input array from the second element onwards. |
| 12 | + * - For each element, if it is greater than or equal to the last valid element (tracked by an index), |
| 13 | + * it is kept; otherwise, it is discarded. |
| 14 | + * - The elements are placed in the array in such a way that the valid elements are at the front, and |
| 15 | + * the invalid ones are ignored. |
| 16 | + * - The result is a subsequence where the elements are in non-decreasing order. |
| 17 | + * |
| 18 | + * Use Cases: |
| 19 | + * - **Data Cleaning**: This algorithm could be used for cleaning data where you need to remove values |
| 20 | + * that are smaller than the ones before them (e.g., in sensor data where the data should be increasing). |
| 21 | + * - **Simplified Sorting**: Useful in situations where you only care about retaining values that |
| 22 | + * create a non-decreasing sequence. |
| 23 | + * |
| 24 | + * Time Complexity: |
| 25 | + * - O(n) where n is the number of elements in the array. |
| 26 | + * - The algorithm only traverses the array once, making a constant-time comparison for each element. |
| 27 | + * |
| 28 | + * Space Complexity: |
| 29 | + * - O(1), as it modifies the input array in place and uses a constant amount of extra space (only a few variables). |
| 30 | + * |
| 31 | + * Algorithm Steps: |
| 32 | + * 1. Initialize `index` to track the position of the last valid element. |
| 33 | + * 2. Iterate through the array, starting from the second element. |
| 34 | + * 3. If the current element is greater than or equal to the last valid element, place it in the valid part. |
| 35 | + * 4. Finally, print the result array containing only valid elements. |
| 36 | + */ |
| 37 | +public class StalinSort { |
| 38 | + |
| 39 | + /** |
| 40 | + * Stalin Sort function that modifies the input array in place. |
| 41 | + * |
| 42 | + * @param nums The array of integers to be processed. |
| 43 | + */ |
| 44 | + public static void stalinSort(int[] nums) { |
| 45 | + // The index of the last valid element (elements in non-decreasing order) |
| 46 | + int index = 0; |
| 47 | + |
| 48 | + // Iterate through the array starting from the second element |
| 49 | + for (int i = 1; i < nums.length; i++) { |
| 50 | + // If the current element is greater than or equal to the last valid element |
| 51 | + if (nums[i] >= nums[index]) { |
| 52 | + index++; // Move index forward to accept the current element |
| 53 | + nums[index] = nums[i]; // Place the element in the "valid" part of the array |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Print the "sorted" part of the array, which is the valid subsequence |
| 58 | + System.out.print("Sorted array: "); |
| 59 | + for (int i = 0; i <= index; i++) { |
| 60 | + System.out.print(nums[i] + " "); |
| 61 | + } |
| 62 | + System.out.println(); |
| 63 | + } |
| 64 | + |
| 65 | + public static void main(String[] args) { |
| 66 | + // Test case: Initial unsorted array |
| 67 | + int[] nums = {3, 1, 4, 2, 5, 6, 3, 8}; |
| 68 | + stalinSort(nums); // Sort using Stalin Sort |
| 69 | + |
| 70 | + // Additional test cases: |
| 71 | + int[] nums2 = {1, 2, 3, 4, 5}; |
| 72 | + stalinSort(nums2); // Already sorted, should print all elements. |
| 73 | + |
| 74 | + int[] nums3 = {5, 4, 3, 2, 1}; |
| 75 | + stalinSort(nums3); // Decreasing sequence, only the first element (5) will be kept. |
| 76 | + |
| 77 | + int[] nums4 = {10, 20, 10, 30, 25, 35, 30}; |
| 78 | + stalinSort(nums4); // Mixed sequence, will keep 10, 20, 30, 35. |
| 79 | + } |
| 80 | +} |
0 commit comments