{"id":"1853ec3c-272d-418f-af7b-aeaab7421c6a","name":"Count Sort","description":"1. You are given an array(arr) of integers.\r\n2. You have to sort the given array in increasing order using count sort.","inputFormat":"An Integer n \r\narr1\r\narr2..\r\nn integers","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 10000\r\n-10^8 &lt;= arr[i] &lt;= 10^8","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<vector>\n#include<climits>\n#include<algorithm>\n\nusing namespace std;\n\nvoid countsort(vector<int> &arr, int max, int min){\n // write your code here\n}\n\nvoid Display(vector<int>& arr){\n for(int ele : arr){\n cout<< ele << endl;\n }\n}\n\n\nint main(){\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n \n for(int i = 0; i < n; i++){ \n cin >> arr[i];\n }\n \n int maxi = (int)-1e9;\n int mini = (int)1e9;\n \n for(int i = 0; i < n; i++){\n mini = min(mini, arr[i]);\n maxi = max(maxi, arr[i]);\n }\n \n countsort(arr, maxi, mini);\n Display(arr);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void countSort(int[] arr, int min, int max) {\r\n //write your code here\r\n \r\n }\r\n\r\n public static void print(int[] arr) {\r\n for (int i = 0; i < arr.length; i++) {\r\n System.out.println(arr[i]);\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n int max = Integer.MIN_VALUE;\r\n int min = Integer.MAX_VALUE;\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n max = Math.max(max, arr[i]);\r\n min = Math.min(min, arr[i]);\r\n }\r\n countSort(arr,min,max);\r\n print(arr);\r\n }\r\n\r\n}"},"python":{"code":"import math\n\ndef countsort(arr, max, min):\n #write your code here\n \n \n\ndef Display(arr):\n for i in range(0, len(arr)):\n print(arr[i]);\n\n\ndef main():\n n = int(input());\n arr = []\n for i in range(0, n):\n ele = int(input());\n arr.append(ele);\n\n maxVal = -math.inf;\n minVal = math.inf;\n \n for i in range(0, n):\n minVal = min(minVal, arr[i]);\n maxVal = max(maxVal, arr[i]);\n\n countsort(arr, maxVal, minVal);\n Display(arr);\n\n\nif __name__ == \"__main__\":\n main()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7 \r\n-2 \r\n4 \r\n1 \r\n3","sampleOutput":"-2\r\n1\r\n3\r\n4\r\n7","questionVideo":"https://www.youtube.com/embed/p-OyKUgIrx4?end=1279","hints":[],"associated":[{"id":"17a6be1f-c220-40b4-a4f0-af30c0e9fb7e","name":"What is the space complexity of Count sort?","slug":"what-is-the-space-complexity-of-count-sort","type":4},{"id":"8fe44d72-1da0-4e16-8418-9e44aecc0c7c","name":"what will be the value of range","slug":"what-will-be-the-value-of-range","type":4},{"id":"ba711e1a-4bbe-41e8-abe0-357356420aa5","name":"do we need to use stable sort","slug":"do-we-need-to-use-stable-sort","type":4},{"id":"ddae504a-d7ce-45aa-91fc-86e62aabde35","name":"What is the time complexity of count sort ?","slug":"what-is-the-time-complexity-of-count-sort","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8e3abac7-a5ab-4090-abd1-b26ef8b53d70","name":"Time and Space Complexity","slug":"time-and-space-complexity","type":0},{"id":"54bb8941-bd76-4de4-9124-07abcdc0b785","name":"Count Sort","slug":"count-sort","type":1}],"next":{"id":"576f4697-e7f1-4ce7-9014-e570ffe3f603","name":"Count Sort","type":3,"slug":"count-sort"},"prev":{"id":"04d7a99b-91f3-4530-94b3-ebef8499df60","name":"Quick Select","type":3,"slug":"quick-select"}}}

Count Sort

1. You are given an array(arr) of integers. 2. You have to sort the given array in increasing order using count sort.

{"id":"1853ec3c-272d-418f-af7b-aeaab7421c6a","name":"Count Sort","description":"1. You are given an array(arr) of integers.\r\n2. You have to sort the given array in increasing order using count sort.","inputFormat":"An Integer n \r\narr1\r\narr2..\r\nn integers","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 10000\r\n-10^8 &lt;= arr[i] &lt;= 10^8","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<vector>\n#include<climits>\n#include<algorithm>\n\nusing namespace std;\n\nvoid countsort(vector<int> &arr, int max, int min){\n // write your code here\n}\n\nvoid Display(vector<int>& arr){\n for(int ele : arr){\n cout<< ele << endl;\n }\n}\n\n\nint main(){\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n \n for(int i = 0; i < n; i++){ \n cin >> arr[i];\n }\n \n int maxi = (int)-1e9;\n int mini = (int)1e9;\n \n for(int i = 0; i < n; i++){\n mini = min(mini, arr[i]);\n maxi = max(maxi, arr[i]);\n }\n \n countsort(arr, maxi, mini);\n Display(arr);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void countSort(int[] arr, int min, int max) {\r\n //write your code here\r\n \r\n }\r\n\r\n public static void print(int[] arr) {\r\n for (int i = 0; i < arr.length; i++) {\r\n System.out.println(arr[i]);\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n int max = Integer.MIN_VALUE;\r\n int min = Integer.MAX_VALUE;\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n max = Math.max(max, arr[i]);\r\n min = Math.min(min, arr[i]);\r\n }\r\n countSort(arr,min,max);\r\n print(arr);\r\n }\r\n\r\n}"},"python":{"code":"import math\n\ndef countsort(arr, max, min):\n #write your code here\n \n \n\ndef Display(arr):\n for i in range(0, len(arr)):\n print(arr[i]);\n\n\ndef main():\n n = int(input());\n arr = []\n for i in range(0, n):\n ele = int(input());\n arr.append(ele);\n\n maxVal = -math.inf;\n minVal = math.inf;\n \n for i in range(0, n):\n minVal = min(minVal, arr[i]);\n maxVal = max(maxVal, arr[i]);\n\n countsort(arr, maxVal, minVal);\n Display(arr);\n\n\nif __name__ == \"__main__\":\n main()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7 \r\n-2 \r\n4 \r\n1 \r\n3","sampleOutput":"-2\r\n1\r\n3\r\n4\r\n7","questionVideo":"https://www.youtube.com/embed/p-OyKUgIrx4?end=1279","hints":[],"associated":[{"id":"17a6be1f-c220-40b4-a4f0-af30c0e9fb7e","name":"What is the space complexity of Count sort?","slug":"what-is-the-space-complexity-of-count-sort","type":4},{"id":"8fe44d72-1da0-4e16-8418-9e44aecc0c7c","name":"what will be the value of range","slug":"what-will-be-the-value-of-range","type":4},{"id":"ba711e1a-4bbe-41e8-abe0-357356420aa5","name":"do we need to use stable sort","slug":"do-we-need-to-use-stable-sort","type":4},{"id":"ddae504a-d7ce-45aa-91fc-86e62aabde35","name":"What is the time complexity of count sort ?","slug":"what-is-the-time-complexity-of-count-sort","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8e3abac7-a5ab-4090-abd1-b26ef8b53d70","name":"Time and Space Complexity","slug":"time-and-space-complexity","type":0},{"id":"54bb8941-bd76-4de4-9124-07abcdc0b785","name":"Count Sort","slug":"count-sort","type":1}],"next":{"id":"576f4697-e7f1-4ce7-9014-e570ffe3f603","name":"Count Sort","type":3,"slug":"count-sort"},"prev":{"id":"04d7a99b-91f3-4530-94b3-ebef8499df60","name":"Quick Select","type":3,"slug":"quick-select"}}}
plane

Editor


Loading...

Count Sort

easy

1. You are given an array(arr) of integers. 2. You have to sort the given array in increasing order using count sort.

Constraints

1 <= N <= 10000 -10^8 <= arr[i] <= 10^8

Format

Input

An Integer n arr1 arr2.. n integers

Output

Check the sample ouput and question video.

Example

Sample Input

5 7 -2 4 1 3

Sample Output

-2 1 3 4 7

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode