{"id":"cb43b8fc-1341-4ba8-97b1-1af4e5422c81","name":"Selection Sort","description":"1. You are given an array(arr) of integers.\r\n2. You have to sort the given array in increasing order using selection 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^9 &lt;= arr[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<iostream>\nusing namespace std;\n\n\nbool isSmaller(int arr[],int i,int j ){\n cout<<\"Comparing \"<<arr[i]<<\" and \"<<arr[j]<<endl;\n if(arr[i]<arr[j]){\n return true;\n }else{\n return false;\n }\n}\n\nvoid swap(int arr[],int i,int j){\n cout<<\"Swapping \"<<arr[i]<<\" and \"<<arr[j]<<endl;\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n \n}\n\n\nvoid selectionSort(int arr[],int n){\n //write your code\n \n}\n\nvoid print(int arr[],int n){\n for(int i=0;i<n;i++){\n cout<<arr[i]<<endl;\n }\n}\n\n\n\nint main(){\n int n;\n cin>>n;\n int arr[n];\n for(int i=0;i<n;i++){\n cin>>arr[i];\n }\n \n selectionSort(arr,n);\n print(arr,n);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void selectionSort(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 \" + arr[i] + \" and \" + arr[j]);\r\n int temp = arr[i];\r\n arr[i] = arr[j];\r\n arr[j] = temp;\r\n }\r\n\r\n // return true if ith element is smaller than jth element\r\n public static boolean isSmaller(int[] arr, int i, int j) {\r\n System.out.println(\"Comparing \" + arr[i] + \" and \" + arr[j]);\r\n if (arr[i] < arr[j]) {\r\n return true;\r\n } else {\r\n return false;\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 for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n selectionSort(arr);\r\n print(arr);\r\n }\r\n\r\n}"},"python":{"code":"def swap(arr,i,j):\n print(\"Swapping\",arr[i],\"and\",arr[j])\n temp=arr[i]\n arr[i]=arr[j]\n arr[j]=temp\n\ndef isSmaller(arr,i,j):\n print(\"Comparing\",arr[i],\"and\",arr[j])\n\n if arr[i]<arr[j]:\n return True\n else :\n return False;\n\ndef selectionSort(arr):\n # write your code\n\n\ndef printList(arr):\n\tfor i in range(len(arr)):\n\t\tprint(arr[i], end=\"\\n\")\n\tprint()\n\n\n\narr = []\n\nn = int(input())\n\nfor i in range(0, n):\n\tele = int(input())\n\n\tarr.append(ele) # adding the element\nselectionSort(arr);\nprintList(arr);"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7 \r\n-2 \r\n4 \r\n1 \r\n3","sampleOutput":"Comparing -2 and 7\r\nComparing 4 and -2\r\nComparing 1 and -2\r\nComparing 3 and -2\r\nSwapping 7 and -2\r\nComparing 4 and 7\r\nComparing 1 and 4\r\nComparing 3 and 1\r\nSwapping 7 and 1\r\nComparing 7 and 4\r\nComparing 3 and 4\r\nSwapping 4 and 3\r\nComparing 4 and 7\r\nSwapping 7 and 4\r\n-2\r\n1\r\n3\r\n4\r\n7","questionVideo":"https://www.youtube.com/embed/EU9FIt1t-Is?end=657","hints":[],"associated":[{"id":"0821b3a5-9d23-42d9-a0fa-68ff3d6050b6","name":"The worst case time complexity of selection sort will occure when","slug":"the-worst-case-time-complexity-of-selection-sort-will-occure-when","type":4},{"id":"17da0d3e-8655-4070-8e63-ee30251fbbb4","name":"How many iterations will be there if number of elements are \"n\" in Selection sort?","slug":"how-many-iterations-will-be-there-if-number-of-elements-are-n-in-selection-sort","type":4},{"id":"b2b5fa70-31e1-441e-a218-bd532043fcef","name":"If the loop (\"i\") for first pointer is starting from \"0\" then loop for second pointer will start from","slug":"if-the-loop-i-for-first-pointer-is-starting-from-0-then-loop-for-second-pointer-will-start-from","type":4},{"id":"c863fcde-d2cd-4ec5-850c-6be6c81be11a","name":"The best case time complexity of selection sort will occure when","slug":"the-best-case-time-complexity-of-selection-sort-will-occure-when","type":4},{"id":"f0dae1f9-754e-448a-8763-659596aee3cb","name":"If we assume that out first pointer is \"i\" then from where you'll initialise the \"min\" pointer ?","slug":"if-we-assume-that-out-first-pointer-is-i-then-from-where-you-ll-initialise-the-min-pointer","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":"20f4aa4c-7b64-4399-a7c9-917313de044d","name":"Selection Sort","slug":"selection-sort","type":1}],"next":{"id":"fc227825-8e2d-4d89-9c5c-148e37c9a9ec","name":"Selection Sort","type":3,"slug":"selection-sort"},"prev":{"id":"04798191-c8a7-4929-b0c0-cccdccf7456c","name":"Bubble Sort","type":3,"slug":"bubble-sort"}}}

Selection Sort

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

{"id":"cb43b8fc-1341-4ba8-97b1-1af4e5422c81","name":"Selection Sort","description":"1. You are given an array(arr) of integers.\r\n2. You have to sort the given array in increasing order using selection 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^9 &lt;= arr[i] &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<iostream>\nusing namespace std;\n\n\nbool isSmaller(int arr[],int i,int j ){\n cout<<\"Comparing \"<<arr[i]<<\" and \"<<arr[j]<<endl;\n if(arr[i]<arr[j]){\n return true;\n }else{\n return false;\n }\n}\n\nvoid swap(int arr[],int i,int j){\n cout<<\"Swapping \"<<arr[i]<<\" and \"<<arr[j]<<endl;\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n \n}\n\n\nvoid selectionSort(int arr[],int n){\n //write your code\n \n}\n\nvoid print(int arr[],int n){\n for(int i=0;i<n;i++){\n cout<<arr[i]<<endl;\n }\n}\n\n\n\nint main(){\n int n;\n cin>>n;\n int arr[n];\n for(int i=0;i<n;i++){\n cin>>arr[i];\n }\n \n selectionSort(arr,n);\n print(arr,n);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void selectionSort(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 \" + arr[i] + \" and \" + arr[j]);\r\n int temp = arr[i];\r\n arr[i] = arr[j];\r\n arr[j] = temp;\r\n }\r\n\r\n // return true if ith element is smaller than jth element\r\n public static boolean isSmaller(int[] arr, int i, int j) {\r\n System.out.println(\"Comparing \" + arr[i] + \" and \" + arr[j]);\r\n if (arr[i] < arr[j]) {\r\n return true;\r\n } else {\r\n return false;\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 for (int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n selectionSort(arr);\r\n print(arr);\r\n }\r\n\r\n}"},"python":{"code":"def swap(arr,i,j):\n print(\"Swapping\",arr[i],\"and\",arr[j])\n temp=arr[i]\n arr[i]=arr[j]\n arr[j]=temp\n\ndef isSmaller(arr,i,j):\n print(\"Comparing\",arr[i],\"and\",arr[j])\n\n if arr[i]<arr[j]:\n return True\n else :\n return False;\n\ndef selectionSort(arr):\n # write your code\n\n\ndef printList(arr):\n\tfor i in range(len(arr)):\n\t\tprint(arr[i], end=\"\\n\")\n\tprint()\n\n\n\narr = []\n\nn = int(input())\n\nfor i in range(0, n):\n\tele = int(input())\n\n\tarr.append(ele) # adding the element\nselectionSort(arr);\nprintList(arr);"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7 \r\n-2 \r\n4 \r\n1 \r\n3","sampleOutput":"Comparing -2 and 7\r\nComparing 4 and -2\r\nComparing 1 and -2\r\nComparing 3 and -2\r\nSwapping 7 and -2\r\nComparing 4 and 7\r\nComparing 1 and 4\r\nComparing 3 and 1\r\nSwapping 7 and 1\r\nComparing 7 and 4\r\nComparing 3 and 4\r\nSwapping 4 and 3\r\nComparing 4 and 7\r\nSwapping 7 and 4\r\n-2\r\n1\r\n3\r\n4\r\n7","questionVideo":"https://www.youtube.com/embed/EU9FIt1t-Is?end=657","hints":[],"associated":[{"id":"0821b3a5-9d23-42d9-a0fa-68ff3d6050b6","name":"The worst case time complexity of selection sort will occure when","slug":"the-worst-case-time-complexity-of-selection-sort-will-occure-when","type":4},{"id":"17da0d3e-8655-4070-8e63-ee30251fbbb4","name":"How many iterations will be there if number of elements are \"n\" in Selection sort?","slug":"how-many-iterations-will-be-there-if-number-of-elements-are-n-in-selection-sort","type":4},{"id":"b2b5fa70-31e1-441e-a218-bd532043fcef","name":"If the loop (\"i\") for first pointer is starting from \"0\" then loop for second pointer will start from","slug":"if-the-loop-i-for-first-pointer-is-starting-from-0-then-loop-for-second-pointer-will-start-from","type":4},{"id":"c863fcde-d2cd-4ec5-850c-6be6c81be11a","name":"The best case time complexity of selection sort will occure when","slug":"the-best-case-time-complexity-of-selection-sort-will-occure-when","type":4},{"id":"f0dae1f9-754e-448a-8763-659596aee3cb","name":"If we assume that out first pointer is \"i\" then from where you'll initialise the \"min\" pointer ?","slug":"if-we-assume-that-out-first-pointer-is-i-then-from-where-you-ll-initialise-the-min-pointer","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":"20f4aa4c-7b64-4399-a7c9-917313de044d","name":"Selection Sort","slug":"selection-sort","type":1}],"next":{"id":"fc227825-8e2d-4d89-9c5c-148e37c9a9ec","name":"Selection Sort","type":3,"slug":"selection-sort"},"prev":{"id":"04798191-c8a7-4929-b0c0-cccdccf7456c","name":"Bubble Sort","type":3,"slug":"bubble-sort"}}}
plane

Editor


Loading...

Selection Sort

easy

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

Constraints

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

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

Comparing -2 and 7 Comparing 4 and -2 Comparing 1 and -2 Comparing 3 and -2 Swapping 7 and -2 Comparing 4 and 7 Comparing 1 and 4 Comparing 3 and 1 Swapping 7 and 1 Comparing 7 and 4 Comparing 3 and 4 Swapping 4 and 3 Comparing 4 and 7 Swapping 7 and 4 -2 1 3 4 7

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode