C++
x
43
1
2
using namespace std;
3
void Exchange(int* a, int* b) {
4
int var;
5
var = *a; //For swapping or exchanging values. O_o
6
*a = *b;
7
*b = var;
8
}
9
void Algorithm(int array[], int nerd) {
10
int i, j, k; //This is the actual algorithm which is required. ;)
11
i = 0; while (i < nerd) {
12
for (j = i + 1; j < nerd; j++) {
13
if (array[j] < array[j - 1])
14
Exchange(&array[j], &array[j - 1]);
15
}
16
nerd--;
17
for (k = nerd - 1; k > i; k--) {
18
if (array[k] < array[k - 1])
19
Exchange(&array[k], &array[k - 1]);
20
21
}
22
i++;
23
24
}
25
26
27
}
28
int main() {
29
int n, i;
30
cout << "\nEnter the count of numbers for the algorithm: ";
31
cin >> n;
32
int arr[10];
33
for (i = 0; i < n; i++) {
34
cout << "Enter number " << i + 1 << ": ";
35
cin >> arr[i];
36
}
37
Algorithm(arr, n);
38
cout << "\nFinal Data you entered (after the algorithm performed)";
39
for (i = 0; i < n; i++)
40
cout << " -> " << arr[i];
41
return 0;
42
}
43
$ g++ prog.cc -Wall -Wextra -std=c++11 -pedantic
Stdin
5 1 4 2 9 3
Start
Enter the count of numbers for the algorithm: Enter number 1: Enter number 2: Enter number 3: Enter number 4: Enter number 5: Final Data you entered (after the algorithm performed) -> 1 -> 2 -> 3 -> 4 -> 9
0
Finish