Python program for Counting sort Algorithm














































Python program for Counting sort Algorithm



Counting sort is a sorting technique based non keys between the specific range.It works by counting the number of objects having distinct key values(knd of hashing).Then it calculates the position for each object in the output sequence. The Time complexity is O(n^2)
class Sort():
    
    #Default constructor for the class Sort
    def __init__(self, ele_list):
        self.ele_list = ele_list

    #Function for count sort
    def count_sort(self):
        output = [0 for i in range(256)]
        count = [0 for i in range(256)]

        #storing the result as string is immutable
        ans = ["" for _ in self.ele_list]
        #Store count of each character
        for i in self.ele_list:
            count[ord(i)] += 1

        #It stores the actual position of alphabets
        for i in range(256):
            count[i] += count[i - 1]

        #Build the output character array
        for i in range(len(self.ele_list)):
            output[count[ord(self.ele_list[i])] - 1] = self.ele_list[i]
            count[ord(self.ele_list[i])] -= 1

        #The sorted array
        for i in range(len(self.ele_list)):
            ans[i] = output[i]
        return ans

#Main function             
def main():
    
    ele_list = "cppsecrets"
    #Object for the class Sort
    obj_Sort = Sort(ele_list)
    ans = obj_Sort.count_sort()
    print("Sorted character array is: ",ans)

#Driver code
if __name__ == "__main__":
    main()

OUTPUT:
Sorted character array is:  ['c', 'c', 'e', 'e', 'p', 'p', 'r', 's', 's', 't']
        

More Articles of Anjani Dubey:

Name Views Likes
Python program to find the solutions for a Quadratic equation 1737 18
Python program to convert multiple integers into single integer 1384 22
Program to generate the Armstrong numbers for a specific range 800 21
Python program to combine two sorted lists and make another sorted list 945 16
Python program to calculate perimeter of a Rectangle 753 18
Python program to display powers of 2 1008 20
Python program for Insertion sort Algorithm 1128 23
Python Program to check whether a number is Palindrome or not 1111 11
Python program to convert tuple to a string 1167 26
Python program to calculate the Square root of a number 1026 19
Program to Display the Fibonacci series using Recursion 746 14
Python program to convert Octal to Decimal, Binary, Hexadecimal 3261 28
Python program for UNIX domain socket UDP client 832 15
Python program to convert BST into MinHeap 888 18
Python program to find all Permutations of a string 989 11
Python program to convert Nested list into a flat list 1167 17
Python program to insert an element in the Binary Tree 1849 18
Python program to convert POS to SOP 1047 15
Python UNIX domain socket TCP Server 1009 16
Python program to calculate perimeter of Triangle 1563 23
Python program for One Dimensional Array 817 23
Program to find average of numbers 808 16
Python Program for calculating the Area of Triangle 910 22
Program to calculate area of Rectangle 720 25
Program to find factors of a number 734 18
Python program to find all pairs of an integer array whose sum is equal to a given number 2823 18
Python program for Binary Search Tree 904 20
Python program to generate Armstrong numbers for a specific range 744 17
Python program to find the sum of natural number using Recursion 825 17
Python program to reverse the string using recursion 1012 15
Python program to print the Floyd Triangle 775 15
Python program to insert and find an element in Binary Search Tree 1141 18
Python program to Shuffle deck of cards 2046 18
Python program to check if a Binary tree is height balanced or not 2700 16
Python program to calculate the area of Rectangle 852 17
Python program for Bubble Sort algorithm 959 12
Python program to delete an element in Binary search tree 1487 18
Python program to print duplicate characters and count from a string 2893 20
Program to calculate the perimeter of rectangle 1015 14
Python program to find hash of a string 905 28
Python program to print strings 802 14
Python program to calculate the factors of a number 795 11
Python program to find numbers Divisible by Another number 1958 24
Program to solve the Quadratic Equation 842 19
Program to print the Floyd Triangle 806 20
Program to calculate Square root of a number 677 18
Program to calculate sum of natural numbers using Recursion 796 18
Python program for Counting sort Algorithm 780 13
Python program to delete an element into AVL Tree 2906 19
Python program for traversing the Tree in PreOrder, PostOrder and InOrder 2132 13
Python program to find the Fibonacci sequence recursion 955 30
Python program to merge mails 791 19
Program to check whether a number is Palindrome or not 1226 17
Python Program to calculate the Grade of the Student 13127 21
Python program to find Average of numbers 813 23
Python UNIX domain socket UDP server 1261 24
Python program for Merge Sort Algorithm 877 21
Python UNIX domain socket TCP Client 964 16
Python program to find height of the Binary Search Tree(BST) 1821 11
Python program to insert an element in AVL Tree 1057 27
Python program to convert Hexadecimal to Decimal, Binary, Octal 3326 27
Program to calculate the perimeter of triangle 796 22
Python program to check two strings are anagrams of each other 1764 22
Advantages of UNIX domain socket over Normal sockets 1638 24

Comments