-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_array_modification.cpp
More file actions
88 lines (73 loc) · 1.84 KB
/
Copy path2_array_modification.cpp
File metadata and controls
88 lines (73 loc) · 1.84 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// #include <bits/stdc++.h>
// #include <iostream>
// using namespace std;
// // Function to perform one pass of Bubble Sort
// void bubblePass(int target[], int n, int i)
// {
// if (i == n - 1)
// return;
// if (target[i] > target[i + 1])
// {
// int temp = target[i];
// target[i] = target[i + 1];
// target[i + 1] = temp;
// }
// bubblePass(target, n, i + 1);
// }
// void recursiveBubbleSort(int target[], int n)
// {
// if (n <= 1)
// return;
// // Perform one pass of Bubble Sort
// bubblePass(target, n, 0);
// // Recursively call for remaining elements
// recursiveBubbleSort(target, n - 1);
// }
// int main()
// {
// int target[] = {9, 3, 5};
// int n = sizeof(target) / sizeof(target[0]);
// // Sort the array recursively
// recursiveBubbleSort(target, n);
// // Print the sorted array
// for (int i = 0; i < n; i++)
// {
// cout << target[i] << " ";
// }
// cout << endl;
// return 0;
// }
#include <iostream>
using namespace std;
// Function to perform Bubble Sort
void bubbleSort(int target[], int n)
{
for (int i = 0; i < n - 1; i++)
{
// Last i elements are already sorted
for (int j = 0; j < n - i - 1; j++)
{
// Swap if the element found is greater than the next element
if (target[j] > target[j + 1])
{
int temp = target[j];
target[j] = target[j + 1];
target[j + 1] = temp;
}
}
}
}
int main()
{
int target[] = {9, 3, 5};
int n = sizeof(target) / sizeof(target[0]);
// Sort the array using Bubble Sort
bubbleSort(target, n);
// Print the sorted array
for (int i = 0; i < n; i++)
{
cout << target[i] << " ";
}
cout << endl;
return 0;
}