1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int[] unsorted = 6 { 7 15, 19, 13, 19, 10, 33, 12, 14, 13, 10, 8 }; 9 10 RadixSort(unsorted); 11 12 foreach (var key in unsorted) 13 { 14 Console.Write("{0} ", key); 15 } 16 17 Console.Read(); 18 } 19 20 static void RadixSort(int[] unsorted) 21 { 22 // our helper array 23 int[] t = new int[unsorted.Length]; 24 25 // number of bits our group will be long 26 // try to set this also to 2, 8 or 16 to see if it is quicker or not 27 int r = 4; 28 29 // number of bits of a C# int 30 int b = 32; 31 32 // counting and prefix arrays 33 // (note dimensions 2^r which is the number of 34 // all possible values of a r-bit number) 35 int[] count = new int[1 << r]; 36 int[] pref = new int[1 << r]; 37 38 // number of groups 39 int groups = (int)Math.Ceiling((double)b / (double)r); 40 41 // the mask to identify groups 42 int mask = (1 << r) - 1; 43 44 // the algorithm: 45 for (int c = 0, shift = 0; c < groups; c++, shift += r) 46 { 47 // reset count array 48 for (int j = 0; j < count.Length; j++) 49 count[j] = 0; 50 51 // counting elements of the c-th group 52 for (int i = 0; i < unsorted.Length; i++) 53 count[(unsorted[i] >> shift) & mask]++; 54 55 // calculating prefixes 56 pref[0] = 0; 57 for (int i = 1; i < count.Length; i++) 58 pref[i] = pref[i - 1] + count[i - 1]; 59 60 // from a[] to t[] elements ordered by c-th group 61 for (int i = 0; i < unsorted.Length; i++) 62 t[pref[(unsorted[i] >> shift) & mask]++] = unsorted[i]; 63 64 // a[]=t[] and start again until the last group 65 t.CopyTo(unsorted, 0); 66 } 67 // a is sorted 68 } 69 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int[] unsorted = 6 { 7 15, 19, 13, 19, 10, 33, 12, 14, 13, 10, 8 }; 9 10 BucketSort(unsorted); 11 12 foreach (var key in unsorted) 13 { 14 Console.Write("{0} ", key); 15 } 16 17 Console.Read(); 18 } 19 20 static void BucketSort(int[] unsorted) 21 { 22 // find the maximum and minimum values in the array 23 int max = unsorted[0]; //start with first element 24 int min = unsorted[0]; 25 26 // start from index 1 27 for (int i = 1; i < unsorted.Length; i++) 28 { 29 if (unsorted[i] < min) min = unsorted[i]; 30 else if (unsorted[i] > max) max = unsorted[i]; 31 } 32 33 // create a temporary "buckets" to store the values in order 34 // each value will be stored in its corresponding index 35 // scooting everything over to the left as much as possible 36 // e.g. 34 => index at 34 - minValue 37 List<int>[] buckets = new List<int>[max - min + 1]; 38 39 // initialize the buckets 40 for (int i = 0; i < buckets.Length; i++) 41 { 42 buckets[i] = new List<int>(); 43 } 44 45 // move items to bucket 46 for (int i = 0; i < unsorted.Length; i++) 47 { 48 buckets[unsorted[i] - min].Add(unsorted[i]); 49 } 50 51 // move items in the bucket back to the original array in order 52 int k = 0; //index for original array 53 for (int i = 0; i < buckets.Length; i++) 54 { 55 if (buckets[i].Count > 0) 56 { 57 for (int j = 0; j < buckets[i].Count; j++) 58 { 59 unsorted[k] = buckets[i][j]; 60 k++; 61 } 62 } 63 } 64 } 65 }