I got annoyed at work. The result? I made a sorting alogrithm

I haven’t touched Java in 3 years. Most of the time was sorting out syntax.

I don’t know how to make the fancy videos. Also it isn’t meant to be fast.

Nerd Shit hides in here
public class collection {

    //Fields
     int contents[] = new int[25];
     int steps2Sort = 0; //The number of times the order of values in contents[] gets changed.

    //Constructors
    public collection()
    {
        for(int i=0; i < contents.length; i++)
        {
            int addedContent = (int) (Math.random()*1001)+1;
            if(addedContent == 0)
            {
                addedContent = nonZero();
            }
            addedContent = Math.abs(addedContent);
            contents[i] = addedContent;
            if(contents.length-1 == 2)
            {
                contents[i+1] = addedContent;
                break;
            }
        };
    };

    public int sortContents(int lowest) 
    {
        int num1 = 0;
        int num2 = 0;
        int steps = steps2Sort;
        int numLowest = lowest;
        for(int i=0; i < contents.length; i++)
        {
            if(i==0)
            {
                num1 = contents[i];
                if(steps == 0)
                {
                    numLowest = num1;
                }
                continue;
            }
            num2 = contents[i];
            if(num1 > num2)
            {
                contents[i] = num1;
                contents[i-1] = num2;
                if(num2<numLowest)
                {
                    numLowest = num2;
                }
                steps++;
                continue;
            }
            num1 = num2;
        }
        steps2Sort = steps;
        return numLowest;
    };

    //For every value in contents, checks if the previous value(num1) is less than the current value(num2)
    //Returns output[lowest, sorted]
    //sorted: 1 is true, sorted: 0 is false.
    public  int[] checkSort(int lowest)
    {
        int num1 = 0;
        int num2 = 0;
        int[] output = {lowest, 0};
        for(int i=0; i < contents.length; i++) 
        {
            //If first value set it as num1
            if(i == 0)
            {
                if(contents[i] != output[0])
                {
                    output[1] = 0;
                    return output;
                }
                num1 = contents[i];
                continue;
            }
            num2 = contents[i];
            if(num2 >= num1)
            {
                num1 = num2;
                continue;
            }
            else
            {
                output[1] = 0;
                return output;
            }
        };
        output[1] = 1;
        return output;
    };

    private  int nonZero()
    {
        int non0Num;
        do
        {
            non0Num = (int) (Math.random()*1001)+1;
            non0Num = Math.abs(non0Num);
        }while(non0Num == 0);
        return non0Num;
    }

    public static void main(String[] args) 
    {
        collection c1 = new collection();
        int[] checkOutput = c1.checkSort(1005);
        while(checkOutput[1] == 0)
        {
            int sortIterate = c1.sortContents(checkOutput[0]);
            checkOutput = c1.checkSort(sortIterate);
        }
        
        
        for(int i=0; i < c1.contents.length; i++)
        {
            System.out.println(c1.contents[i]);
        }
        System.out.println("Steps: "+ c1.steps2Sort);
    }
};```
1 Like

Cool.
Post must be at least 15 characters.

2 Likes

That makes it twice this year I’ve read someone’s number sorting algorithm made out of boredom. That isn’t a lot, but it’s still weird that it happened twice already.

The other sorting algorithm I saw that was also quite slow and bad, and could only work on positive numbers went something like this:

Take first entry of list.
Wait for [entry] seconds asynchronously
Store [entry] in new list at the next available position in the list
Repeat until entire list is processed

java

Yikes

1 Like

Convert numbers to seconds, print the numbers in a list at their time. So 1 would print after 1 second and 10 would print after 10. Easy sort.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.