This shows you the differences between two versions of the page.
ep:laboratoare:02 [2017/10/02 20:26] emilian.radoi |
ep:laboratoare:02 [2017/10/02 20:27] (current) emilian.radoi |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Tutorial 02 ====== | ====== Tutorial 02 ====== | ||
- | |||
- | <code> | ||
- | import random | ||
- | import math | ||
- | |||
- | def bucket_sort(array, bucketSize): | ||
- | if len(array) == 0: | ||
- | return array | ||
- | |||
- | # Determine minimum and maximum values | ||
- | minValue = array[0] | ||
- | maxValue = array[0] | ||
- | for i in range(1, len(array)): | ||
- | if array[i] < minValue: | ||
- | minValue = array[i] | ||
- | elif array[i] > maxValue: | ||
- | maxValue = array[i] | ||
- | |||
- | # Initialize buckets | ||
- | bucketCount = int(math.floor((maxValue - minValue) / bucketSize) + 1) | ||
- | buckets = [] | ||
- | for i in range(0, bucketCount): | ||
- | buckets.append([]) | ||
- | |||
- | # Distribute input array values into buckets | ||
- | for i in range(0, len(array)): | ||
- | buckets[int(math.floor((array[i] - minValue) / bucketSize))].append(array[i]) | ||
- | |||
- | # Sort buckets and place back into input array | ||
- | array = [] | ||
- | for i in range(0, len(buckets)): | ||
- | buckets[i].sort | ||
- | for j in range(0, len(buckets[i])): | ||
- | array.append(buckets[i][j]) | ||
- | |||
- | return array | ||
- | |||
- | if __name__ == '__main__': | ||
- | | ||
- | array = [] | ||
- | for i in range(0, 100): | ||
- | array.append(random.randint(0,10000000)) | ||
- | #print array[i] | ||
- | |||
- | sorted_array = bucket_sort(array, 1) | ||
- | for i in range(1, len(array)): | ||
- | print sorted_array[i] | ||
- | |||
- | </code> | ||
The material for this tutorial was taken from Darren Hoch’s “Linux System and Performance Monitoring”. You can access it at: http://ufsdump.org/papers/oscon2009-linux-monitoring.pdf. | The material for this tutorial was taken from Darren Hoch’s “Linux System and Performance Monitoring”. You can access it at: http://ufsdump.org/papers/oscon2009-linux-monitoring.pdf. |