-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
62 lines (47 loc) · 1014 Bytes
/
bubbleSort.cpp
File metadata and controls
62 lines (47 loc) · 1014 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// bubbleSort.cpp
// DataStructures
//
// Created by Suraj on 07/06/20.
// Copyright © 2020 Suraj. All rights reserved.
//
#include <iostream>
using namespace std;
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}
void display(int A[], int size)
{
for (int i=0; i < size; i++)
{
cout << A[i] << " ";
}
}
int main()
{
int size;
cout << "Enter the number of elements: ";
cin >> size;
int arr[size];
cout << "Enter elements:" << endl;
for(int i = 0; i<size; i++)
{
cin >> arr[i];
}
cout << "Array before Sorting: " << endl;
display(arr, size);
bubbleSort(arr, size);
cout <<"\n""Array after sorting: " <<endl;
display(arr, size);
}