Trunks
Trunks

Reputation: 1

Is this a new sorting algorithm?

I implemented a custom sorting algorithm called SortTrunks, which progressively filters out the largest elements from an array and moves them to the end, reducing the range of unsorted elements in each iteration. The goal was to minimize unnecessary swaps while maintaining a sorting complexity of approximately O(n²) in the worst case.

The algorithm should sort the array in ascending order by repeatedly identifying the largest element and grouping all its occurrences together at the end. The number of swaps should be reduced compared to traditional sorting methods like Bubble Sort.

package code;
import java.util.Arrays;
public class SortTrunks {
    public static void main(String[] args) {
        int[] M = {0, 0, 100,5, 100, 8,7, 0, 5,0,99};
        sortTrunks(M);
        System.out.println(Arrays.toString(M));
    }
    public static void sortTrunks(int[] M) {
        int h = M.length;
        while (h > 1) {
            int test = M[0];
            int count = 0;  
            for (int i = 0; i < h; i++) {
                if (M[i] > test) {
                    test = M[i];
                    count = 1;  
                } else if (M[i] == test) {
                    count++; 
                }
            }
            int left = 0;
            for (int i = 0; i < h; i++) {
                if (M[i] != test) {
                    if (left != i) {
                        int temp = M[i];
                        M[i] = M[left];
                        M[left] = temp;
                    }
                    left++;
                }
            }
            h -= count;
        }
    }
}

Is this recognized as a new sorting algorithm?

Analysis of SortTrunks Algorithm

The SortTrunks sorting algorithm operates by progressively isolating the largest values from an unsorted array, thereby reducing the number of unnecessary swaps. The core principles behind this approach include:

Finding the Maximum Element:

The algorithm iterates through the unsorted section of the array to determine the largest element.

It also counts the occurrences of this maximum value.

Segregating the Maximum Values:

The algorithm reorganizes the array by moving all elements equal to the identified maximum to the end of the active sorting range.

This effectively removes these elements from further consideration.

Reducing the Sorting Scope:

The unsorted portion of the array is reduced by the count of maximum values found.

The process is repeated on the remaining elements until the entire array is sorted.

Strengths of SortTrunks

✅ Reduced Swap Operations: Unlike traditional sorting algorithms that rely on frequent element swapping, SortTrunks minimizes unnecessary swaps by directly grouping and repositioning the maximum elements.

✅ Improved Stability for Large Datasets with Duplicates: Since duplicate maximum values are handled in a batch, the algorithm exhibits better efficiency when dealing with arrays containing repeated elements.

✅ Conceptually Unique: The approach of progressively isolating elements instead of using standard comparison-based methods (like quicksort or mergesort) provides an alternative way to conceptualize sorting.

The algorithm works correctly for some cases, but in certain scenarios (especially with duplicate elements or already sorted inputs), it still performs redundant operations. The performance is still O(n²), meaning it struggles with large datasets.

The approach seems inefficient when dealing with nearly sorted arrays, as it continues searching for the maximum element even when unnecessary.

Upvotes: -10

Views: 97

Answers (0)

Related Questions