-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6.BubbleSort.cpp
37 lines (32 loc) · 865 Bytes
/
6.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
/*
* This program takes an array from the user
* sorts it using bubble sort
* Prints the sorted array
* For more information about Bubble Sort Algorithm: https://en.wikipedia.org/wiki/Bubble_sort
*
* Coded by: Abdurrezak EFE
*
* */
#include <iostream>
#include <algorithm>
using namespace std;
void bubble_sort(int arr[],int k) //bubble sort function
{
int i, j,temp;
for (i = 0; i < k-1; i++)
for (j = 0; j < k-i-1; j++)
if (arr[j] > arr[j+1])
temp=arr[j],arr[j]=arr[j+1],arr[j+1]=temp; //swapping
}
int main()
{
int k;
cout << "Enter the number of the integers you want to construct the array from: ";
cin >> k;
int arr[k];
for(int i=0;i<k;i++)
cin >> arr[i];
bubble_sort(arr,k); //sorting
for(int i=0;i<k;i++) //printing the array
cout << arr[i] << " ";
}