{"id":1298,"date":"2023-12-14T00:00:00","date_gmt":"2023-12-14T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1298"},"modified":"2023-12-14T12:05:49","modified_gmt":"2023-12-14T17:05:49","slug":"python-code-given-an-array-of-colors-represented-by-numbers-0-1-and-2-sort-them-using-counting-sort","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-code-given-an-array-of-colors-represented-by-numbers-0-1-and-2-sort-them-using-counting-sort\/","title":{"rendered":"Python code: Given an array of colors represented by numbers 0, 1, and 2, sort them using counting sort:"},"content":{"rendered":"\n<p>Counting Sort is a linear sorting algorithm that works well when the range of input values is known and relatively small. In this case, you want to sort an array of colors represented by numbers 0, 1, and 2. Here&#8217;s how you can implement Counting Sort in Python to sort the colors in-place:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def sortColors(nums):\r\n    # Count the occurrences of each color\r\n    count = &#91;0] * 3\r\n    \r\n    # Count the occurrences of each color in the input array\r\n    for color in nums:\r\n        count&#91;color] += 1\r\n    \r\n    # Update the input array with sorted colors\r\n    index = 0\r\n    for i in range(3):\r\n        for _ in range(count&#91;i]):\r\n            nums&#91;index] = i\r\n            index += 1\r\n\r\n# Example usage:\r\ncolors = &#91;2, 0, 1, 2, 0, 1, 0, 2, 1, 1, 0, 2]\r\nsortColors(colors)\r\n\r\nprint(\"Sorted Colors:\", colors)\r<\/code><\/pre>\n\n\n\n<p>In this example, the <code>sortColors<\/code> function takes an array <code>nums<\/code> containing colors represented by numbers 0, 1, and 2. It initializes a <code>count<\/code> array to count the occurrences of each color (0, 1, 2). Then, it updates the input array with the sorted colors based on the count of occurrences.<\/p>\n\n\n\n<p>After running this code, the <code>colors<\/code> array will be sorted in-place, and you can print the result to verify the sorting. Adjust the input array as needed for your specific use case.<\/p>\n\n\n\n<p>Following are unit tests for this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import unittest\r\n\r\ndef sortColors(nums):\r\n    count = &#91;0] * 3\r\n    for color in nums:\r\n        count&#91;color] += 1\r\n    index = 0\r\n    for i in range(3):\r\n        for _ in range(count&#91;i]):\r\n            nums&#91;index] = i\r\n            index += 1\r\n\r\nclass TestSortColors(unittest.TestCase):\r\n    \r\n    def test_sort_colors(self):\r\n        # Test case 1\r\n        colors_1 = &#91;2, 0, 1, 2, 0, 1, 0, 2, 1, 1, 0, 2]\r\n        sortColors(colors_1)\r\n        self.assertEqual(colors_1, &#91;0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2])\r\n\r\n        # Test case 2\r\n        colors_2 = &#91;1, 0, 2]\r\n        sortColors(colors_2)\r\n        self.assertEqual(colors_2, &#91;0, 1, 2])\r\n\r\n        # Test case 3\r\n        colors_3 = &#91;2, 2, 2, 1, 1, 1, 0, 0, 0]\r\n        sortColors(colors_3)\r\n        self.assertEqual(colors_3, &#91;0, 0, 0, 1, 1, 1, 2, 2, 2])\r\n\r\n        # Add more test cases as needed\r\n\r\nif __name__ == '__main__':\r\n    unittest.main()\r<\/code><\/pre>\n\n\n\n<p>In this example, the <code>TestSortColors<\/code> class inherits from <code>unittest.TestCase<\/code>, and the <code>test_sort_colors<\/code> method defines individual test cases. The <code>assertEqual<\/code> method is used to check if the actual result matches the expected result after calling <code>sortColors<\/code>. You can add more test cases as needed to thoroughly test your sorting function.<\/p>\n\n\n\n<p>To run the tests, save this script and execute it. If there are any issues with the <code>sortColors<\/code> function, the test cases will help identify them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Counting Sort is a linear sorting algorithm that works well when the range of input values is known and relatively small. In this case, you want to sort an array of colors represented by numbers 0, 1, and 2. Here&#8217;s how you can implement Counting Sort in Python to sort the colors in-place: In this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1462,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[],"class_list":["post-1298","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1298"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1298\/revisions"}],"predecessor-version":[{"id":1299,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1298\/revisions\/1299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1462"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}