Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Short and Fast (10 Lines)
- private static int[] bubbleSort(int[] sort) {
- int i, x=sort.length;
- for(boolean again = true; again;)
- for (x--, i = 1, again = false; i < x; i++)
- if (sort[i-1]>sort[i]) {
- int t = sort[i-1];
- sort[i-1] = sort[i];
- sort[i]=t;
- again=true;
- }
- return sort;
- }
- //Just really short. (7 Lines)
- private static int[] bubbleSort(int[] sort) {
- for(int i, x=sort.length, a=1; a==1;)
- for (x--, i = 1, a = 0; i < x; i++)
- if (sort[i-1]>sort[i]) {
- sort[i]=(sort[i-1]^=sort[i]^=sort[i-1])^sort[i];
- a=1;
- }
- return sort;
- }
- //Even smaller! (5 lines)
- private static int[] bubbleSort(int[] sort) {
- for(int i, a=1, x=sort.length+1; a==1;)
- for (x--, i = 1, a = 0; i < x; i++)
- for(;sort[i-1]>sort[i];a=1)
- sort[i]=(sort[i-1]^=sort[i]^=sort[i-1])^sort[i];
- return sort;
- }
- //And finally, in four lines:
- private static int[] bubbleSort(int[] sort) {
- for(int i, t, a=1, x=sort.length; a==1; x--)
- for (i=1, a=0; i < x; i++)
- for(a=1; (t=sort[i-1]) > sort[i]; sort[i-1]=sort[i], sort[i]=t);
- return sort;
- }
- //DO NOT USE, IN PROGRESS:
- private static int[] bubbleSort(int[] sort, int l) {
- for (int i=1;i<l;i++)
- for(int t=sort[i-1];sort[i-1]>sort[i];sort[i-1]=sort[i],sort[i]=t,bubbleSort(sort, l--));
- return sort;
- }
- //This would be called using this: bubbleSort(unsorted, unsorted.length);
Advertisement
Add Comment
Please, Sign In to add comment