`{"id":"f2e063ae-4078-464b-b85d-bb7378ee9f96","name":"Bubble Sort","description":"1. You are given an array(arr) of integers.\r\n2. You have to sort the given array in increasing order using bubble 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 bubbleSort(int arr[],int n){\n // write your code here\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 bubbleSort(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 bubbleSort(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 bubbleSort(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 bubbleSort(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) \nbubbleSort(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\nSwapping -2 and 7\r\nComparing 4 and 7\r\nSwapping 4 and 7\r\nComparing 1 and 7\r\nSwapping 1 and 7\r\nComparing 3 and 7\r\nSwapping 3 and 7\r\nComparing 4 and -2\r\nComparing 1 and 4\r\nSwapping 1 and 4\r\nComparing 3 and 4\r\nSwapping 3 and 4\r\nComparing 1 and -2\r\nComparing 3 and 1\r\nComparing 1 and -2\r\n-2\r\n1\r\n3\r\n4\r\n7","questionVideo":"https://www.youtube.com/embed/Jv-eGC2xmtU?end=325","hints":[],"associated":[{"id":"a43f2e36-510a-46e6-a6d5-9bd8e36f50aa","name":"Is this funtion will give us required output","slug":"is-this-funtion-will-give-us-required-output","type":4},{"id":"d7633cda-02d5-4200-a46f-3579d172ef71","name":"In bubble sorting in every iteration how many pair of elements gets sorted at a time?","slug":"in-bubble-sorting-in-every-iteration-how-many-pair-of-elements-gets-sorted-at-a-time","type":4},{"id":"f0a15c94-473e-4802-87bb-626f1374b4d0","name":"What will be the best time and space complexity Bubble sort?","slug":"what-will-be-the-best-time-and-space-complexity-bubble-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":"44298a4a-374c-4d47-ae87-cae8a6c1175f","name":"Bubble Sort","slug":"bubble-sort","type":1}],"next":{"id":"04798191-c8a7-4929-b0c0-cccdccf7456c","name":"Bubble Sort","type":3,"slug":"bubble-sort"},"prev":null}}`

# Bubble Sort

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

`{"id":"f2e063ae-4078-464b-b85d-bb7378ee9f96","name":"Bubble Sort","description":"1. You are given an array(arr) of integers.\r\n2. You have to sort the given array in increasing order using bubble 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 bubbleSort(int arr[],int n){\n // write your code here\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 bubbleSort(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 bubbleSort(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 bubbleSort(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 bubbleSort(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) \nbubbleSort(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\nSwapping -2 and 7\r\nComparing 4 and 7\r\nSwapping 4 and 7\r\nComparing 1 and 7\r\nSwapping 1 and 7\r\nComparing 3 and 7\r\nSwapping 3 and 7\r\nComparing 4 and -2\r\nComparing 1 and 4\r\nSwapping 1 and 4\r\nComparing 3 and 4\r\nSwapping 3 and 4\r\nComparing 1 and -2\r\nComparing 3 and 1\r\nComparing 1 and -2\r\n-2\r\n1\r\n3\r\n4\r\n7","questionVideo":"https://www.youtube.com/embed/Jv-eGC2xmtU?end=325","hints":[],"associated":[{"id":"a43f2e36-510a-46e6-a6d5-9bd8e36f50aa","name":"Is this funtion will give us required output","slug":"is-this-funtion-will-give-us-required-output","type":4},{"id":"d7633cda-02d5-4200-a46f-3579d172ef71","name":"In bubble sorting in every iteration how many pair of elements gets sorted at a time?","slug":"in-bubble-sorting-in-every-iteration-how-many-pair-of-elements-gets-sorted-at-a-time","type":4},{"id":"f0a15c94-473e-4802-87bb-626f1374b4d0","name":"What will be the best time and space complexity Bubble sort?","slug":"what-will-be-the-best-time-and-space-complexity-bubble-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":"44298a4a-374c-4d47-ae87-cae8a6c1175f","name":"Bubble Sort","slug":"bubble-sort","type":1}],"next":{"id":"04798191-c8a7-4929-b0c0-cccdccf7456c","name":"Bubble Sort","type":3,"slug":"bubble-sort"},"prev":null}}`

Editor

# Bubble Sort

easy

1. You are given an array(arr) of integers. 2. You have to sort the given array in increasing order using bubble 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

```.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 7 -2 4 1 3```

### 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;}Comparing -2 and 7 Swapping -2 and 7 Comparing 4 and 7 Swapping 4 and 7 Comparing 1 and 7 Swapping 1 and 7 Comparing 3 and 7 Swapping 3 and 7 Comparing 4 and -2 Comparing 1 and 4 Swapping 1 and 4 Comparing 3 and 4 Swapping 3 and 4 Comparing 1 and -2 Comparing 3 and 1 Comparing 1 and -2 -2 1 3 4 7```

Question Video

Discussions

Show Discussion

Related Resources