forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppSortingAscending.cpp
More file actions
61 lines (52 loc) · 1.21 KB
/
cppSortingAscending.cpp
File metadata and controls
61 lines (52 loc) · 1.21 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
#include <bits/stdc++.h>
using namespace std;
void sort(int num[], int len);
void swapNums(int nums[],
int first, int second);
// Driver code
int main()
{
// Initializing arrya
int nums[] = {1, 12, 6, 8, 10};
int size_nums = (sizeof(nums) /
sizeof(nums[0]));
cout << "Before sorting the array is: \n";
for (int i = 0; i < size_nums; i++)
cout << nums[i] << " ";
cout << "\n\n";
sort(nums, size_nums);
cout << "After sorting the array is: \n";
for (int i = 0; i < size_nums; i++)
cout << nums[i] << " ";
cout << "\n";
return 0;
}
// Sort function
void sort(int num[], int len)
{
bool isSwapped;
for (int i = 0; i < len; i++)
{
isSwapped = false;
for (int j = 1; j < len - i; j++)
{
if (num[j] < num[j - 1])
{
swapNums(num, j, (j - 1));
isSwapped = true;
}
}
if (!isSwapped)
{
break;
}
}
}
// Swaps two numbers in array
void swapNums(int nums[],
int first, int second)
{
int curr = nums[first];
nums[first] = nums[second];
nums[second] = curr;
}