|
| 1 | +--- |
| 2 | +id: bubble-sort |
| 3 | +title: Bubble Sort |
| 4 | +sidebar_label: Bubble-Sort |
| 5 | +tags: |
| 6 | + - Sorting |
| 7 | + - Algorithms |
| 8 | +description: "This tutorial covers the solution to the Bubble Sort problem from the GeeksforGeeks." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +Given an unsorted array of size `N`, use bubblesort to sort `arr[]` in increasing order. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: |
| 20 | +N = 5 |
| 21 | +arr[] = {4, 1, 3, 9, 7} |
| 22 | +Output: |
| 23 | +1 3 4 7 9 |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: |
| 30 | +N = 10 |
| 31 | +arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} |
| 32 | +Output: |
| 33 | +1 2 3 4 5 6 7 8 9 10 |
| 34 | +``` |
| 35 | + |
| 36 | +## Your Task |
| 37 | +You don't have to read input or print anything. Your task is to complete the function bubblesort() which takes the array and it's size as input and sorts the array using bubble sort algorithm. |
| 38 | + |
| 39 | +Expected Time Complexity: O(N^2) |
| 40 | + |
| 41 | +Expected Auxiliary Space: O(1) |
| 42 | + |
| 43 | +## Constraints |
| 44 | + |
| 45 | +* `1 ≤ N ≤ 10^3` |
| 46 | + |
| 47 | +## Problem Explanation |
| 48 | + |
| 49 | +The task is to traverse the array and sort the array using bubble sort. |
| 50 | + |
| 51 | +## Code Implementation |
| 52 | + |
| 53 | +### C++ Solution |
| 54 | + |
| 55 | +```cpp |
| 56 | +// Optimized implementation of Bubble sort |
| 57 | +#include <bits/stdc++.h> |
| 58 | +using namespace std; |
| 59 | + |
| 60 | +// An optimized version of Bubble Sort |
| 61 | +void bubbleSort(int arr[], int n) |
| 62 | +{ |
| 63 | + int i, j; |
| 64 | + bool swapped; |
| 65 | + for (i = 0; i < n - 1; i++) { |
| 66 | + swapped = false; |
| 67 | + for (j = 0; j < n - i - 1; j++) { |
| 68 | + if (arr[j] > arr[j + 1]) { |
| 69 | + swap(arr[j], arr[j + 1]); |
| 70 | + swapped = true; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // If no two elements were swapped |
| 75 | + // by inner loop, then break |
| 76 | + if (swapped == false) |
| 77 | + break; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Function to print an array |
| 82 | +void printArray(int arr[], int size) |
| 83 | +{ |
| 84 | + int i; |
| 85 | + for (i = 0; i < size; i++) |
| 86 | + cout << " " << arr[i]; |
| 87 | +} |
| 88 | + |
| 89 | +// Driver program to test above functions |
| 90 | +int main() |
| 91 | +{ |
| 92 | + int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; |
| 93 | + int N = sizeof(arr) / sizeof(arr[0]); |
| 94 | + bubbleSort(arr, N); |
| 95 | + cout << "Sorted array: \n"; |
| 96 | + printArray(arr, N); |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +``` |
| 102 | +
|
| 103 | +```java |
| 104 | +// Optimized java implementation of Bubble sort |
| 105 | +
|
| 106 | +import java.io.*; |
| 107 | +
|
| 108 | +class GFG { |
| 109 | + |
| 110 | + // An optimized version of Bubble Sort |
| 111 | + static void bubbleSort(int arr[], int n) |
| 112 | + { |
| 113 | + int i, j, temp; |
| 114 | + boolean swapped; |
| 115 | + for (i = 0; i < n - 1; i++) { |
| 116 | + swapped = false; |
| 117 | + for (j = 0; j < n - i - 1; j++) { |
| 118 | + if (arr[j] > arr[j + 1]) { |
| 119 | + |
| 120 | + // Swap arr[j] and arr[j+1] |
| 121 | + temp = arr[j]; |
| 122 | + arr[j] = arr[j + 1]; |
| 123 | + arr[j + 1] = temp; |
| 124 | + swapped = true; |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + // If no two elements were |
| 129 | + // swapped by inner loop, then break |
| 130 | + if (swapped == false) |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | +
|
| 135 | + // Function to print an array |
| 136 | + static void printArray(int arr[], int size) |
| 137 | + { |
| 138 | + int i; |
| 139 | + for (i = 0; i < size; i++) |
| 140 | + System.out.print(arr[i] + " "); |
| 141 | + System.out.println(); |
| 142 | + } |
| 143 | +
|
| 144 | + // Driver program |
| 145 | + public static void main(String args[]) |
| 146 | + { |
| 147 | + int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; |
| 148 | + int n = arr.length; |
| 149 | + bubbleSort(arr, n); |
| 150 | + System.out.println("Sorted array: "); |
| 151 | + printArray(arr, n); |
| 152 | + } |
| 153 | +} |
| 154 | +
|
| 155 | +
|
| 156 | +
|
| 157 | +``` |
| 158 | + |
| 159 | +```python |
| 160 | +# Optimized Python program for implementation of Bubble Sort |
| 161 | + |
| 162 | + |
| 163 | +def bubbleSort(arr): |
| 164 | + n = len(arr) |
| 165 | + |
| 166 | + # Traverse through all array elements |
| 167 | + for i in range(n): |
| 168 | + swapped = False |
| 169 | + |
| 170 | + # Last i elements are already in place |
| 171 | + for j in range(0, n-i-1): |
| 172 | + |
| 173 | + # Traverse the array from 0 to n-i-1 |
| 174 | + # Swap if the element found is greater |
| 175 | + # than the next element |
| 176 | + if arr[j] > arr[j+1]: |
| 177 | + arr[j], arr[j+1] = arr[j+1], arr[j] |
| 178 | + swapped = True |
| 179 | + if (swapped == False): |
| 180 | + break |
| 181 | + |
| 182 | + |
| 183 | +# Driver code to test above |
| 184 | +if __name__ == "__main__": |
| 185 | + arr = [64, 34, 25, 12, 22, 11, 90] |
| 186 | + |
| 187 | + bubbleSort(arr) |
| 188 | + |
| 189 | + print("Sorted array:") |
| 190 | + for i in range(len(arr)): |
| 191 | + print("%d" % arr[i], end=" ") |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +``` |
| 196 | + |
| 197 | +```javascript |
| 198 | +// Optimized javaScript implementation |
| 199 | +// of Bubble sort |
| 200 | +// An optimized version of Bubble Sort |
| 201 | +function bubbleSort(arr, n) |
| 202 | +{ |
| 203 | + var i, j, temp; |
| 204 | + var swapped; |
| 205 | + for (i = 0; i < n - 1; i++) |
| 206 | + { |
| 207 | + swapped = false; |
| 208 | + for (j = 0; j < n - i - 1; j++) |
| 209 | + { |
| 210 | + if (arr[j] > arr[j + 1]) |
| 211 | + { |
| 212 | + // Swap arr[j] and arr[j+1] |
| 213 | + temp = arr[j]; |
| 214 | + arr[j] = arr[j + 1]; |
| 215 | + arr[j + 1] = temp; |
| 216 | + swapped = true; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + // IF no two elements were |
| 221 | + // swapped by inner loop, then break |
| 222 | + if (swapped == false) |
| 223 | + break; |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +// Function to print an array |
| 228 | +function printArray(arr, size) |
| 229 | +{ |
| 230 | + var i; |
| 231 | + for (i = 0; i < size; i++) |
| 232 | + console.log(arr[i] + " "); |
| 233 | +} |
| 234 | + |
| 235 | +// Driver program |
| 236 | +var arr = [ 64, 34, 25, 12, 22, 11, 90 ]; |
| 237 | +var n = arr.length; |
| 238 | +bubbleSort(arr, n); |
| 239 | +console.log("Sorted array: "); |
| 240 | +printArray(arr, n); |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | +``` |
| 245 | + |
| 246 | +## Solution Logic: |
| 247 | + |
| 248 | +1. The largest element is placed in its correct position, i.e., the end of the array. |
| 249 | + |
| 250 | +2. Place the second largest element at correct position |
| 251 | + |
| 252 | +3. Place the remaining two elements at their correct positions. |
| 253 | + |
| 254 | +4. Total no. of passes: n-1. Total no. of comparisons: n*(n-1)/2 |
| 255 | + |
| 256 | + |
| 257 | +## Time Complexity |
| 258 | + |
| 259 | +* The time complexity is $O(N^2)$ as there are two nested loops |
| 260 | + |
| 261 | +## Space Complexity |
| 262 | + |
| 263 | +* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array. |
0 commit comments