| | |
Bucket Sort Integers
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Bucket sort is a very simple, fast sorting algorithm specialized in integers.
public void BucketSort(int[] integers) { //Verify input if (integers == null || integers.Length == 0) return; //Find the maximum and minimum values in the array int maxValue = integers[0]; //start with first element int minValue = integers[0]; //Note: start from index 1 for (int i = 1; i < integers.Length; i++) { if (integers[i] > maxValue) maxValue = integers[i]; if (integers[i] < minValue) minValue = integers[i]; } //Create a temporary "bucket" to store the values in order //each value will be stored in its corresponding index //scooting everything over to the left as much as possible (minValue) //e.g. 34 => index at 34 - minValue List<int>[] bucket = new List<int>[maxValue - minValue + 1]; //Initialize the bucket for (int i = 0; i < bucket.Length; i++) { bucket[i] = new List<int>(); } //Move items to bucket for (int i = 0; i < integers.Length; i++) { bucket[integers[i] - minValue].Add(integers[i]); } //Move items in the bucket back to the original array in order int k = 0; //index for original array for (int i = 0; i < bucket.Length; i++) { if (bucket[i].Count > 0) { for (int j = 0; j < bucket[i].Count; j++) { integers[k] = bucket[i][j]; k++; } } } }
0
•
•
•
•
Nice!
Have you read http://www.daniweb.com/code/snippet979.html?
Maybe you can test wich of the three is the fastest?
Have you read http://www.daniweb.com/code/snippet979.html?
Maybe you can test wich of the three is the fastest?
Similar Threads
- bucket or radix sort not sure but need help (C++)
- descending bucket sort in 68000 (Assembly)
- I have Problems with Airline Reservations System, A bucket sort and a Turtle Graphic (C++)
- Could someone please help me with a bucket sort. (C)
- bucket sort (Computer Science)
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml



