-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cc
More file actions
58 lines (52 loc) · 1.21 KB
/
BubbleSort.cc
File metadata and controls
58 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
/*
* I wrote bubble sort because I need to implement a
* multi-process bubble sort program. This program
* sorts numbers in descending way.
*/
#include <iostream>
#include <vector>
using namespace std;
int bubbleSort(std::vector<long long> &nums,int begin, int end)
{
bool change = true;
long long swap;
for(int j=0; j < nums.size(); j++)
{
change = false;
for(int i=0; i < end-1-j; i++)
{
if(nums[i] < nums[i+1])
{
swap = nums[i];
nums[i] = nums[i+1];
nums[i+1] = swap;
change = true;
}
}
if(!change)
{
break;
}
}
return 0;
}
int main(int argc, char **argv) {
std::vector<long long> nums;
long long temp;
FILE *file;
file = fopen(argv[1], "r");
if(file == NULL)
{
fprintf(stderr, "Can't open this file.\n");
}
while((fscanf(file, "%lld\n", &temp)) != EOF)
{
nums.push_back(temp);
}
bubbleSort(nums, 0, nums.size());
for(int i=0; i < nums.size(); i++)
{
printf("%lld\n", nums[i]);
}
return 0;
}