Sorting/Bubble Sort
comparisonin-placestable
Press play to start
1void bubbleSort(int* A, int n) {
2 int i, j, tmp;
3 for (i = 0; i < n; i++) {
4 for (j = 0; j < n-1; j++) {
5 if (A[j] > A[j+1]) {
6 tmp = A[j];
7 A[j] = A[j+1];
8 A[j+1] = tmp;
9 }
10 }
11 }
12}
Step 1/0

Practice

LeetCode·#912 Sort an ArrayMediumHackerRank·Bubble SortMediumNeetCode·Sort an ArrayMedium
BestO(n²)
AverageO(n²)
WorstO(n²)
SpaceO(1)