-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathbubblesort.cpp
38 lines (38 loc) · 1.01 KB
/
bubblesort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// bubble sort
// sorting algorithms can be used for collection of numbers, strings characters or a structure of any of theese types
// bubble sort is based on the idea of repeatedly comparing pairs of adjacent elements and then swapping their positions if they exist in the wrong order.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int bubble_sort(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
if (arr[i] > arr[i + 1])
{
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
int main()
{
// take array as input
int n; // n is the size of the array
printf("Enter the size of the array : ");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter the %d element of the array : ", i + 1);
scanf("%d", &arr[i]);
}
bubble_sort(arr, n);
printf("Sorted array is : ");
for (int i = 0; i < n; i++)
{
printf("%d", arr[i]);
}
return 0;
}