From 945dcd55ef81f61ad297d6b19059eb046166aa04 Mon Sep 17 00:00:00 2001 From: Kiranmayep Date: Thu, 1 Oct 2020 09:58:18 +0530 Subject: [PATCH] Added New Sorting Algorithms --- sorting and basics/bucket_sort.py | 43 +++++++++++++++++++++++ sorting and basics/radix_sort.py | 57 +++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 sorting and basics/bucket_sort.py create mode 100644 sorting and basics/radix_sort.py diff --git a/sorting and basics/bucket_sort.py b/sorting and basics/bucket_sort.py new file mode 100644 index 0000000..ffb8fd8 --- /dev/null +++ b/sorting and basics/bucket_sort.py @@ -0,0 +1,43 @@ +//Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. +# python3 program to sort an array + +def insertionSort(b): + for i in range(1, len(b)): + up = b[i] + j = i - 1 + while j >= 0 and b[j] > up: + b[j + 1] = b[j] + j -= 1 + b[j + 1] = up + return b + +def bucketSort(x): + arr = [] + slot_num = 10 # 10 means 10 slots, each + # slot's size is 0.1 + for i in range(slot_num): + arr.append([]) + + # Put array elements in different buckets + for j in x: + index_b = int(slot_num * j) + arr[index_b].append(j) + + # Sort individual buckets + for i in range(slot_num): + arr[i] = insertionSort(arr[i]) + + # concatenate the result + k = 0 + for i in range(slot_num): + for j in range(len(arr[i])): + x[k] = arr[i][j] + k += 1 + return x + +# Driver Code +x = [0.897, 0.565, 0.656, + 0.1234, 0.665, 0.3434] +print("Sorted Array is") +print(bucketSort(x)) + diff --git a/sorting and basics/radix_sort.py b/sorting and basics/radix_sort.py new file mode 100644 index 0000000..a93de82 --- /dev/null +++ b/sorting and basics/radix_sort.py @@ -0,0 +1,57 @@ +# Python program for implementation of Radix Sort + +def countingSort(arr, exp1): + + n = len(arr) + + # The output array elements that will have sorted arr + output = [0] * (n) + + # initialize count array as 0 + count = [0] * (10) + + # Store count of occurrences in count[] + for i in range(0, n): + index = (arr[i]/exp1) + count[ (index)%10 ] += 1 + + # Change count[i] so that count[i] now contains actual + # position of this digit in output array + for i in range(1,10): + count[i] += count[i-1] + + # Build the output array + i = n-1 + while i>=0: + index = (arr[i]/exp1) + output[ count[ (index)%10 ] - 1] = arr[i] + count[ (index)%10 ] -= 1 + i -= 1 + + # Copying the output array to arr[], + # so that arr now contains sorted numbers + i = 0 + for i in range(0,len(arr)): + arr[i] = output[i] + +# Method to do Radix Sort +def radixSort(arr): + + # Find the maximum number to know number of digits + max1 = max(arr) + + # Do counting sort for every digit. Note that instead + # of passing digit number, exp is passed. exp is 10^i + # where i is current digit number + exp = 1 + while max1/exp > 0: + countingSort(arr,exp) + exp *= 10 + +# Driver code to test above +arr = [ 170, 45, 75, 90, 802, 24, 2, 66] +radixSort(arr) + +for i in range(len(arr)): + print(arr[i]), +