`{"id":"b371b912-9ad5-4d71-85fb-a05e4d175910","name":"Sort 01","description":"1. You are given an array(arr) containing only 0's and 1's.\r\n2. You have to sort the given array in increasing order and in linear time.","inputFormat":"An Integer N \r\narr1\r\narr2..\r\nn integers","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= N &lt;= 10000\r\narr[i] = 0,1","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<vector>\n\nusing namespace std;\n\n\nvoid swap(vector<int> &arr, int i, int j){\n cout<<\"Swapping index \" << i << \" and index \" << j << endl;\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n}\n\n\nvoid sort01(vector<int>& arr) {\n // write your code here\n \n}\n\n\nvoid Display(vector<int>& arr){\n for(int ele : arr){\n cout<< ele << endl;\n }\n}\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n\n sort01(arr);\n Display(arr);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void sort01(int[] arr){\r\n //write your code here\r\n \r\n }\r\n\r\n // used for swapping ith and jth elements of array\r\n public static void swap(int[] arr, int i, int j) {\r\n System.out.println(\"Swapping index \" + i + \" and index \" + j);\r\n int temp = arr[i];\r\n arr[i] = arr[j];\r\n arr[j] = temp;\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 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 for(int i = 0 ;i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n sort01(arr);\r\n print(arr);\r\n }\r\n\r\n}"},"python":{"code":"def swap(arr, i, j):\n print(\"Swapping index\" , i , \"and index\" , j);\n temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n\n\ndef sort01(arr):\n # write your code here\n\n\n\ndef Display(arr):\n for i in arr:\n print(i);\n \n\ndef main():\n n = int(input());\n arr = [];\n \n for i in range(0, n):\n ele = int(input());\n arr.append(ele);\n\n sort01(arr);\n Display(arr);\n \nif __name__ == \"__main__\":\n main();"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n0\r\n1\r\n0\r\n1\r\n0","sampleOutput":"Swapping index 0 and index 0\r\nSwapping index 2 and index 1\r\nSwapping index 4 and index 2\r\n0\r\n0\r\n0\r\n1\r\n1","questionVideo":"https://www.youtube.com/embed/jFrUwjx4eoA?end=411","hints":[],"associated":[{"id":"24ad9b64-931a-40d9-a24d-e9f6ed7762aa","name":"How many segments we are maintaining in single pass algorithm?","slug":"how-many-segments-we-are-maintaining-in-single-pass-algorithm","type":4},{"id":"42ee0cba-76e1-4751-b235-31c65c93c0c7","name":"Initial size of 1's segment ?","slug":"initial-size-of-1-s-segment","type":4},{"id":"43bfe525-3571-4422-bea4-a6d443c5b4aa","name":"Time complexity of SORT 012 ?","slug":"time-complexity-of-sort-012","type":4},{"id":"adb26f9c-9748-41c3-9976-0047f1c527e9","name":"Initial size of 0's segment","slug":"initial-size-of-0-s-segment","type":4},{"id":"b97fc638-f947-42f2-ad15-2f84d2aa5f56","name":"Space complexity of SORT 012 ?","slug":"space-complexity-of-sort-012","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":"1eb876cd-c67c-4dff-825f-1bf77f9a4003","name":"Sort 01","slug":"sort-01","type":1}],"next":{"id":"0a2805ae-c8a1-4681-affa-904b12caf035","name":"Sort 01","type":3,"slug":"sort-01"},"prev":{"id":"0cb4a244-f820-4073-a82b-7a3a5991d792","name":"SORT DATES","type":3,"slug":"sort-dates"}}}`

# Sort 01

1. You are given an array(arr) containing only 0's and 1's. 2. You have to sort the given array in increasing order and in linear time.

`{"id":"b371b912-9ad5-4d71-85fb-a05e4d175910","name":"Sort 01","description":"1. You are given an array(arr) containing only 0's and 1's.\r\n2. You have to sort the given array in increasing order and in linear time.","inputFormat":"An Integer N \r\narr1\r\narr2..\r\nn integers","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= N &lt;= 10000\r\narr[i] = 0,1","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<vector>\n\nusing namespace std;\n\n\nvoid swap(vector<int> &arr, int i, int j){\n cout<<\"Swapping index \" << i << \" and index \" << j << endl;\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n}\n\n\nvoid sort01(vector<int>& arr) {\n // write your code here\n \n}\n\n\nvoid Display(vector<int>& arr){\n for(int ele : arr){\n cout<< ele << endl;\n }\n}\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for (int i = 0; i < n; i++) {\n cin >> arr[i];\n }\n\n sort01(arr);\n Display(arr);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void sort01(int[] arr){\r\n //write your code here\r\n \r\n }\r\n\r\n // used for swapping ith and jth elements of array\r\n public static void swap(int[] arr, int i, int j) {\r\n System.out.println(\"Swapping index \" + i + \" and index \" + j);\r\n int temp = arr[i];\r\n arr[i] = arr[j];\r\n arr[j] = temp;\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 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 for(int i = 0 ;i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n sort01(arr);\r\n print(arr);\r\n }\r\n\r\n}"},"python":{"code":"def swap(arr, i, j):\n print(\"Swapping index\" , i , \"and index\" , j);\n temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n\n\ndef sort01(arr):\n # write your code here\n\n\n\ndef Display(arr):\n for i in arr:\n print(i);\n \n\ndef main():\n n = int(input());\n arr = [];\n \n for i in range(0, n):\n ele = int(input());\n arr.append(ele);\n\n sort01(arr);\n Display(arr);\n \nif __name__ == \"__main__\":\n main();"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n0\r\n1\r\n0\r\n1\r\n0","sampleOutput":"Swapping index 0 and index 0\r\nSwapping index 2 and index 1\r\nSwapping index 4 and index 2\r\n0\r\n0\r\n0\r\n1\r\n1","questionVideo":"https://www.youtube.com/embed/jFrUwjx4eoA?end=411","hints":[],"associated":[{"id":"24ad9b64-931a-40d9-a24d-e9f6ed7762aa","name":"How many segments we are maintaining in single pass algorithm?","slug":"how-many-segments-we-are-maintaining-in-single-pass-algorithm","type":4},{"id":"42ee0cba-76e1-4751-b235-31c65c93c0c7","name":"Initial size of 1's segment ?","slug":"initial-size-of-1-s-segment","type":4},{"id":"43bfe525-3571-4422-bea4-a6d443c5b4aa","name":"Time complexity of SORT 012 ?","slug":"time-complexity-of-sort-012","type":4},{"id":"adb26f9c-9748-41c3-9976-0047f1c527e9","name":"Initial size of 0's segment","slug":"initial-size-of-0-s-segment","type":4},{"id":"b97fc638-f947-42f2-ad15-2f84d2aa5f56","name":"Space complexity of SORT 012 ?","slug":"space-complexity-of-sort-012","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":"1eb876cd-c67c-4dff-825f-1bf77f9a4003","name":"Sort 01","slug":"sort-01","type":1}],"next":{"id":"0a2805ae-c8a1-4681-affa-904b12caf035","name":"Sort 01","type":3,"slug":"sort-01"},"prev":{"id":"0cb4a244-f820-4073-a82b-7a3a5991d792","name":"SORT DATES","type":3,"slug":"sort-dates"}}}`

Editor

# Sort 01

easy

1. You are given an array(arr) containing only 0's and 1's. 2. You have to sort the given array in increasing order and in linear time.

## Constraints

1 <= N <= 10000 arr[i] = 0,1

## Format

### Input

An Integer N arr1 arr2.. n integers

### Output

Check the sample output and question video.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 0 1 0 1 0```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}Swapping index 0 and index 0 Swapping index 2 and index 1 Swapping index 4 and index 2 0 0 0 1 1```

Question Video

Discussions

Show Discussion

Related Resources