Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Bubble Sort | Simple Algorithms
Sorting Algorithms
course content

Зміст курсу

Sorting Algorithms

Sorting Algorithms

1. Simple Algorithms
2. Divide and Conquer Algorithms
3. Problems

Bubble Sort

Bubble sort is a simple algorithm, which works by swapping the adjacent elements if they are placed in wrong order (for example, for ascending order should be: arr[i] <= arr[i+1]). For sorting, iterate array n times, and on each iteration look at pairs of adjacent elements. For each pair, if arr[i+1] > arr[i], swap these elements. This way, in the end of current iteration, we'll move the greatest element to the end of the unsorted array. At the end, you'll get the sorted array.

Example 1

Example 2

Let's do the Bubble Sort setp by step.

arr = [4, 2, 1, -1, 7]

First iteration:

[4, 2,1, -1, 7] -> [2, 4, 1, -1, 7]

[2, 4, 1, -1, 7] -> [2,1, 4, -1, 7]

[2, 1, 4, -1, 7] -> [2, 1, -1, 4, 7]

[2, 1, -1, 4, 7] -> [2, 1, -1, 4, 7]

Second iteration:

[2, 1, -1, 4, 7] -> [1, 2, -1, 4, 7]

[1, 2, -1,4, 7] -> [1, -1, 2, 4, 7]

[1, -1, 2, 4, 7] -> [1, -1, 2, 4, 7]

Third iteration:

[1, -1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Fourth iteration:

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Thus, on each iteration the biggest element from the unsorted part relocates to its position in sorted subarray.

Time complexity of the algorithm is O(N^2), because there are two nested loops of length up to N.

Space complexity: O(1). We can swap elements in-place, so no additional memory is needed.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row

Bubble Sort

Bubble sort is a simple algorithm, which works by swapping the adjacent elements if they are placed in wrong order (for example, for ascending order should be: arr[i] <= arr[i+1]). For sorting, iterate array n times, and on each iteration look at pairs of adjacent elements. For each pair, if arr[i+1] > arr[i], swap these elements. This way, in the end of current iteration, we'll move the greatest element to the end of the unsorted array. At the end, you'll get the sorted array.

Example 1

Example 2

Let's do the Bubble Sort setp by step.

arr = [4, 2, 1, -1, 7]

First iteration:

[4, 2,1, -1, 7] -> [2, 4, 1, -1, 7]

[2, 4, 1, -1, 7] -> [2,1, 4, -1, 7]

[2, 1, 4, -1, 7] -> [2, 1, -1, 4, 7]

[2, 1, -1, 4, 7] -> [2, 1, -1, 4, 7]

Second iteration:

[2, 1, -1, 4, 7] -> [1, 2, -1, 4, 7]

[1, 2, -1,4, 7] -> [1, -1, 2, 4, 7]

[1, -1, 2, 4, 7] -> [1, -1, 2, 4, 7]

Third iteration:

[1, -1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Fourth iteration:

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Thus, on each iteration the biggest element from the unsorted part relocates to its position in sorted subarray.

Time complexity of the algorithm is O(N^2), because there are two nested loops of length up to N.

Space complexity: O(1). We can swap elements in-place, so no additional memory is needed.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row

Bubble Sort

Bubble sort is a simple algorithm, which works by swapping the adjacent elements if they are placed in wrong order (for example, for ascending order should be: arr[i] <= arr[i+1]). For sorting, iterate array n times, and on each iteration look at pairs of adjacent elements. For each pair, if arr[i+1] > arr[i], swap these elements. This way, in the end of current iteration, we'll move the greatest element to the end of the unsorted array. At the end, you'll get the sorted array.

Example 1

Example 2

Let's do the Bubble Sort setp by step.

arr = [4, 2, 1, -1, 7]

First iteration:

[4, 2,1, -1, 7] -> [2, 4, 1, -1, 7]

[2, 4, 1, -1, 7] -> [2,1, 4, -1, 7]

[2, 1, 4, -1, 7] -> [2, 1, -1, 4, 7]

[2, 1, -1, 4, 7] -> [2, 1, -1, 4, 7]

Second iteration:

[2, 1, -1, 4, 7] -> [1, 2, -1, 4, 7]

[1, 2, -1,4, 7] -> [1, -1, 2, 4, 7]

[1, -1, 2, 4, 7] -> [1, -1, 2, 4, 7]

Third iteration:

[1, -1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Fourth iteration:

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Thus, on each iteration the biggest element from the unsorted part relocates to its position in sorted subarray.

Time complexity of the algorithm is O(N^2), because there are two nested loops of length up to N.

Space complexity: O(1). We can swap elements in-place, so no additional memory is needed.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Bubble sort is a simple algorithm, which works by swapping the adjacent elements if they are placed in wrong order (for example, for ascending order should be: arr[i] <= arr[i+1]). For sorting, iterate array n times, and on each iteration look at pairs of adjacent elements. For each pair, if arr[i+1] > arr[i], swap these elements. This way, in the end of current iteration, we'll move the greatest element to the end of the unsorted array. At the end, you'll get the sorted array.

Example 1

Example 2

Let's do the Bubble Sort setp by step.

arr = [4, 2, 1, -1, 7]

First iteration:

[4, 2,1, -1, 7] -> [2, 4, 1, -1, 7]

[2, 4, 1, -1, 7] -> [2,1, 4, -1, 7]

[2, 1, 4, -1, 7] -> [2, 1, -1, 4, 7]

[2, 1, -1, 4, 7] -> [2, 1, -1, 4, 7]

Second iteration:

[2, 1, -1, 4, 7] -> [1, 2, -1, 4, 7]

[1, 2, -1,4, 7] -> [1, -1, 2, 4, 7]

[1, -1, 2, 4, 7] -> [1, -1, 2, 4, 7]

Third iteration:

[1, -1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Fourth iteration:

[-1, 1, 2, 4, 7] -> [-1, 1, 2, 4, 7]

Thus, on each iteration the biggest element from the unsorted part relocates to its position in sorted subarray.

Time complexity of the algorithm is O(N^2), because there are two nested loops of length up to N.

Space complexity: O(1). We can swap elements in-place, so no additional memory is needed.

Завдання

Edit code to create the Bubble Sort Algorithm. Follow the comments in code.

Make function call for given array arr and print the sorted array.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 1. Розділ 6
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt