-
Notifications
You must be signed in to change notification settings - Fork 368
/
bingo_Sort.cpp
57 lines (46 loc) · 1.05 KB
/
bingo_Sort.cpp
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
#include <iostream>
#include <vector>
using namespace std;
void bingoSort(vector<int> &arr)
{
int min = arr[0], max = arr[0];
for (int i = 1; i < arr.size(); i++)
{
if (arr[i] < min)
min = arr[i];
else if (arr[i] > max)
max = arr[i];
}
vector<int> flags(max - min + 1, 0);
for (int i = 0; i < arr.size(); i++)
flags[arr[i] - min] = 1;
int index = 0;
for (int i = 0; i < max - min + 1; i++)
{
if (flags[i])
{
arr[index] = i + min;
index++;
}
}
}
int main()
{
cout << "Enter the length of the array: ";
int n;
cin >> n;
vector<int> a(n);
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "Original array:" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
bingoSort(a);
cout << "Sorted array:" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}