-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortAlgorithm.cpp
More file actions
65 lines (56 loc) · 1.26 KB
/
Copy pathSortAlgorithm.cpp
File metadata and controls
65 lines (56 loc) · 1.26 KB
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
63
64
65
#include <iostream>
using namespace std;
void bubleSort(string &sort_string)
{
unsigned int i{};
unsigned int x{};
for (i = 0; i < sort_string.length(); i++)
{
for (x = i + 1; x < sort_string.length(); x++)
{
if (sort_string[i] > sort_string[x])
{
char temp = sort_string[x];
sort_string[x] = sort_string[i];
sort_string[i] = temp;
}
}
}
}
void insertionSort(string &sort_string)
{
int i = 1;
while (i < sort_string.length())
{
int j = i;
while (j > 0 and sort_string[j - 1] > sort_string[j])
{
char temp = sort_string[j];
sort_string[j] = sort_string[j - 1];
sort_string[j - 1] = temp;
j = j - 1;
}
i = i + 1;
}
}
int main()
{
string username = "";
string sort_string;
char sort_type;
int comparison_count{};
cout << "Enter your name: " << endl;
cin >> username;
cout << "Hello " << username << "!" << endl;
cout << "Enter characters to sort: " << endl;
cin >> sort_string;
cout << "What sort algorthm you want, buble(b) or insertion(i) sort?" << endl;
cin >> sort_type;
if (sort_type == 'b')
{
bubleSort(sort_string);
} else {
insertionSort(sort_string);
}
cout << "Sorted Characters: " << sort_string << endl;
}