forked from Adityaranjanpatra/Btecky2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.cpp
38 lines (31 loc) · 962 Bytes
/
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
#include <iostream>
using namespace std;
void swap(char &a, char &b) {
char temp = a;
a = b;
b = temp;
}
void bubbleSort(char arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
int main() {
const int size = 26; // Number of alphabets
char alphabets[size] = {'z', 'a', 'g', 'b', 'm', 'x', 'c', 'p', 'd', 'y', 'h', 'n', 'q', 'e', 'f', 'r', 'i', 'j', 'k', 'l', 's', 'o', 't', 'u', 'v', 'w'};
cout << "Original array of alphabets:" << endl;
for (int i = 0; i < size; ++i) {
cout << alphabets[i] << " ";
}
// Sorting the array using bubble sort
bubbleSort(alphabets, size);
cout << "\n\nArray of alphabets after sorting in ascending order:" << endl;
for (int i = 0; i < size; ++i) {
cout << alphabets[i] << " ";
}
return 0;
}