Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
- Time complexity is O(n^2) , and in best case it is O(n), if the array is already sorted.
- */
- #include <stdio.h>
- int main()
- {
- int n, arr[100];
- printf("Enter the number and elements of array with spaces ");
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- scanf("%d", &arr[i]);
- for (int i = 0; i < n - 1; i++)
- {
- int flag = 0;
- for (int j = 0; j < n - i - 1; j++)
- {
- if (arr[j] > arr[j + 1])
- {
- int temp = arr[j + 1];
- arr[j + 1] = arr[j];
- arr[j] = temp;
- flag = 1;
- }
- }
- if (!flag)
- break;
- }
- for (int i = 0; i < n; i++)
- {
- printf("%d ", arr[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement