Don't even ask...
In theory, I'm trying to create the most inefficient sorting algorithm of all time.
Why? I'm bored and don't feel like doing my comp sci homework where we're supposed to use the MOST efficient sorting algorithm we can find.
Anyway, here's my code. I know it's really messy right now and can be made into much less lines of code and such, but I wrote this pretty late at night, so I'll just update it whenever I get the chance. Also pushing to github whenever I get it set up on this computer, lol.
What it does RIGHT NOW:
Right now, it only checks the first number, but this is how it works:
Compare the first number to the second number. Are they equal? If so, compare the first number to the third number. If they aren't, randomize the entire array and start over.
Questions? Comments? Suggestions? Criticism? All are welcome.Code:import java.util.*; class EfficientSort { public static void main(String[] args) { //Print title and stuff System.out.println("This is EfficientSort, made by 323."); //Initialize Random Num Gen Random rgen = new Random(); //Initialize array int[] numbers = new int[11]; //Populate array with 0-10 for (int i=0; i<numbers.length; i++) { numbers[i] = i; } //Print original array before randomization System.out.println("Original array:"); for (int z=0; z<numbers.length; z++) { System.out.print(numbers[z] + ", "); } //Start new line for next row of numbers System.out.println(""); //Randomize array for (int x=0; x<numbers.length; x++) { int randomPosition = rgen.nextInt(numbers.length); int temp = numbers[x]; numbers[x] = numbers[randomPosition]; numbers[randomPosition] = temp; } //Print array after randomization System.out.println("Randomized Array:"); for (int y=0; y<numbers.length; y++) { System.out.print(numbers[y] + ", "); } //Print starting message System.out.println(""); System.out.println("Beginning sorting algorithm..."); //Check if the numbers are in the right order int r = 0; int num = 1; while (r == 0) //r only = 1 when entire set is sorted { while (num<numbers.length) { if (numbers[0] < numbers[num]) { num++; } else { //Randomize array for (int x=0; x<numbers.length; x++) { int randomPosition = rgen.nextInt(numbers.length); int temp = numbers[x]; numbers[x] = numbers[randomPosition]; numbers[randomPosition] = temp; } for (int z=0; z<numbers.length; z++) { System.out.print(numbers[z] + ", "); } System.out.println(""); } } System.out.println(""); System.out.println("Sorted First Number!"); System.out.println("First Number Sorted Array"); for (int z=0; z<numbers.length; z++) { System.out.print(numbers[z] + ", "); } r = 1; } } }
Results 1 to 27 of 27
Thread: My retarded idea
- 19 Mar. 2013 02:19am #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.25
My retarded idea
Last edited by 323; 19 Mar. 2013 at 04:18pm.
- 19 Mar. 2013 02:43am #2
Ok.
- 19 Mar. 2013 04:00am #3Code:
var a = [5, 4, 3, 2, 1], sort = function(a) { var e = a.length; for (var x = 0; x < e - 1; x++) { if (a[x] > a[x + 1]) { var n = []; for (var y = 0; y < e; y++) n.push(a[Math.random() < 0.5 ? "pop" : "shift"]()); return sort(n); } } return a; }; alert(sort(a));
- 19 Mar. 2013 04:44am #4
Neat code, think psuedorandom numbers is definitely the way to go.
Can it have external unrelated road blocks added, such as overflows and unnecessarily taking up excess memory?I don't get tired.
- 19 Mar. 2013 01:17pm #5
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 1.93
Ooh, that's a really good idea. I think I'll try to add that, random memory leaks and stuff haha. Or maybe every time it's randomized, it's stored in ANOTHER array, causing a huge number of arrays to be created, and thus a huge amount of memory being used.
Yeah, a lot easier when you're not trying to use Java, haha. Nice code though.
- 19 Mar. 2013 04:01pm #6
- 19 Mar. 2013 04:17pm #7
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.25
Or maybe the browser doesn't have a good randomize function o: I've heard of that being a problem in some browsers.
Also, updated code with some bugfixes, fixed first number discovery. Moving on to second number.
EDIT: Got it to calculate the first number, super easy because the chances are 1 in 10. Now attempting the second number, it's been on for an hour, still no results hahahaha. I guess the chances of getting an array starting with 0, 1 are insanely small.Last edited by 323; 19 Mar. 2013 at 04:25pm.
- 19 Mar. 2013 04:32pm #8
What?
Are you trying to randomly generate an array [1, 2, 3, 4, ...]?
- 19 Mar. 2013 04:43pm #9
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 2.25
So, you start with an array
numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Then, it puts it into a random order.
Then, it checks to see if it's in the right order.
If even one number is out of order, it will re-shuffle the array and start over.
I based it off of the BogoSort algorithm, but I'm going to try to add more horrible slowness to make it an even more inefficient sorting algorithm, haha.
- 20 Mar. 2013 08:21pm #10
I was going to recommend the BogoSort, but it seems like that was the motivation for your algorithm. XD
You should look into Statistics. You can actually calculate the percentage of success of your algorithm, since it is random. From memory, I believe you multiply the chance percent of each number being in the right position... so: for an array of length 10, you would have 1/10 chance to get 1 number in its rightful position, therefore 1/100 to get 2 numbers in the right position... and so on. So for 10 numbers, you would have a 1/(10^10) chance of getting them all in order! Of course computers do not generate completely random numbers, so it is more or less an approximation of your success chance.
I may be completely incorrect on how to calculate the success chance, it's been a couple of years since I've actually had a math class or had to use statistics.
- 21 Mar. 2013 04:45pm #11
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.25
Oh cool, thanks for the information, haha.
Also, put it on my github, check it out here: http://github.com/323
- 21 Mar. 2013 04:53pm #12
You're a little off.
Since the first number is correct (1/10 chance), then the second number has a 1/9 chance of being correct (since the first number is no longer in the pool of numbers). And the third, 1/8, and so on and so forth.
So the odds are 1/(10!) or 1 in 10 factorial.
- 21 Mar. 2013 06:29pm #13
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 1.95
Oh cool, thanks for the clarification. And 10 factorial = 3628800, so there's a 1/3,628,800 chance of getting all of the numbers correct. Now I just need to figure out how many tries it does a second, and then I can figure out about how long it takes. I think in the final version of this I'm going to use a set of 100 numbers though, haha.
- 21 Mar. 2013 11:08pm #14
Tries it does a second?
Why not just time how long it takes to complete...?
startTime = currentTimeWithMilliseconds();
doSort();
endTime = currentTimeWithMilliseconds();
total = endTime - startTime;
- 21 Mar. 2013 11:55pm #15
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.84
- 22 Mar. 2013 01:33am #16
That again depends on the length of the array being sorted, and as stated, even pure randomness has a limit to how many iterations it would take. 1 in 3 million would not take until the heat death of the universe.
- 24 Mar. 2013 05:18pm #17
Oh, you're right! I thought there was something iffy in my solution. Silly me, I'm too old to remember stuff anymore.
Well, you have 1/3M chance to get the sort right, every time you sort. So it is possible that you will never get a sorted array! EVER! The chances of that happening are slim, as time goes to infinity... but it could still happen!
- 24 Mar. 2013 11:43pm #18
Right, but the average length of a calculation wouldn't nearly be the heat death of the universe. To generate a ten digit number. Especially if any two or more digits are the same.
- 25 Mar. 2013 07:38pm #19
Of course not! Especially not in computers, which cannot generate completely random numbers.
- 25 Mar. 2013 07:44pm #20
- 27 Mar. 2013 06:19pm #21
- 27 Mar. 2013 06:46pm #22
Not sure if you can technically call anything genuinely "random." I don't know about quantum entanglement in particular, but almost everything has a clause or offset.
- 27 Mar. 2013 06:49pm #23
- 28 Mar. 2013 11:49pm #24
- 29 Mar. 2013 12:05am #25
No. Even with quantum entanglement, we don't know enough about any of it yet. When we get down to a quantum level, many things appear to be random, but there are often statistical models at work behind the scenes.
- 01 Apr. 2013 05:50am #26
Give the program to a toddler once you are finished and watch it go nuts.
- 01 Apr. 2013 03:41pm #27