View Single Post
Old Oct 1st, 2006, 12:04 AM   #27
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
For your edu-tainment, I present, BOGOSORT!
(Implemented (badly) in Java for extra points!)

import java.util.Random;

public class Bogosort {
	public static void main(String[] args) {
		int[] sort={5,7,3,9};
		boolean[] flag=new boolean[sort.length];
		int[] sorted=new int[sort.length];
		Random gen=new Random();
	
		boolean done=false;
		int choice=gen.nextInt(sort.length);
		while(!done) {
			
			for(int n=0; n<flag.length; n++) {
				flag[n]=false;
			}
				
			for(int i=0; i<sort.length; i++) {
				while(flag[choice]==true) {
					choice=gen.nextInt(sort.length);
				}
				flag[choice]=true;
				sorted[i]=sort[choice];
			}
			int last=sorted[0];
			for(int a=0; a<sort.length; a++) {
				if(sorted[a]>=last) {
					done=true;
					last=sorted[a];
				} else {
					done=false;
					break;
				}
			}
		}
		
		for(int b=0; b<sort.length; b++) {
			System.out.println(sorted[b]);
		}
	}
}

Oh... you said efficient?
Erm... Well, technically speaking, in the best case it'll do O(n)!
....
Nevermind.
:p
ZenMasterJG is offline   Reply With Quote