There are plenty of sorting methods available, but as we all know, the king of them all is Quick Sort.
It’s kind of hard to understand the differences in time it takes to sort a number of elements, but I’ve seen this nice page that has visually shown the amount of time needed to sort with some sorting methods.
All “famous” methods are shown, like the most original “Bubble sort” which all programmers have used atleast once (atleast most of them).
Here is the function they use for bubble sort with Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. class BubbleSort2Algorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = a.length; --i>=0; ) { boolean flipped = false; for (int j = 0; j<i; j++) { if (stopRequested) { return; } if (a[j] > a[j+1]) { int T = a[j]; a[j] = a[j+1]; a[j+1] = T; flipped = true; } pause(i,j); } if (!flipped) { return; } } } } |
To give the function a challange, here is their fastest function of Quick Sort, called Fast Quick Sort.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. public class FastQSortAlgorithm extends SortAlgorithm { /** This is a generic version of C.A.R Hoare's Quick Sort * algorithm. This will handle arrays that are already * sorted, and arrays with duplicate keys.<BR> * * If you think of a one dimensional array as going from * the lowest index on the left to the highest index on the right * then the parameters to this function are lowest index or * left and highest index or right. The first time you call * this function it will be with the parameters 0, a.length - 1. * * @param a an integer array * @param lo0 left boundary of array partition * @param hi0 right boundary of array partition */ private void QuickSort(int a[], int l, int r) throws Exception { int M = 4; int i; int j; int v; if ((r-l)>M) { i = (r+l)/2; if (a[l]>a[i]) swap(a,l,i); // Tri-Median Methode! if (a[l]>a[r]) swap(a,l,r); if (a[i]>a[r]) swap(a,i,r); j = r-1; swap(a,i,j); i = l; v = a[j]; for(;;) { while(a[++i]<v); while(a[--j]>v); if (j<i) break; swap (a,i,j); pause(i,j); if (stopRequested) { return; } } swap(a,i,r-1); pause(i); QuickSort(a,l,j); QuickSort(a,i+1,r); } } private void swap(int a[], int i, int j) { int T; T = a[i]; a[i] = a[j]; a[j] = T; } private void InsertionSort(int a[], int lo0, int hi0) throws Exception { int i; int j; int v; for (i=lo0+1;i<=hi0;i++) { v = a[i]; j=i; while ((j>lo0) && (a[j-1]>v)) { a[j] = a[j-1]; pause(i,j); j--; } a[j] = v; } } public void sort(int a[]) throws Exception { QuickSort(a, 0, a.length - 1); InsertionSort(a,0,a.length-1); pause(-1,-1); } } |
To the point:
Check this site: http://www.cs.ubc.ca/~harrison/Java/index.html
Another great resource which has the same style is http://cg.scs.carleton.ca/~morin/misc/sortalg/
On that site, you can press the images and you will see the time needed to sort with each of these functions. All functions also have a source-code for those who want to try it.
Another funny resource for sorting is this famous movie (sorting out sorting) which many computer scientists have seen:
(It’s horrible quality)