-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounting_sort.cpp
74 lines (62 loc) · 1.51 KB
/
counting_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int* number;
string* strings;
int size;
cin>>size;
string str;
number=new int[size];
strings= new string[size];
for(int i =0;i<size;i++){
cin>>number[i];
cin>>str;
strings[i]=str;
}
int* counted_;
counted_ = new int[size];
for(int i =0;i<size;i++){
counted_[number[i]]++;
}
int* shifted_;
shifted_ = new int[size];
cout<<"printing shifted";
for(int i =1;i<size;i++){
shifted_[i] = shifted_[i-1]+counted_[i-1];
// cout<<shifted_[i]<<" ";
}
shifted_[0] = 0;
string* sorted= new string[size];
int* sorted_num = new int[size];
bool* filled = new bool[size];
for(int i =0;i<size;i++)
filled[i]=false;
int k, pos;
for(int i =0;i<size;i++){
pos = shifted_[number[i]];
if(filled[pos]){
for( k = pos+1;k<size;k++){
if(!filled[k])
break;
}
pos = k;
}
filled[pos] = true;
sorted[pos] = strings[i];
sorted_num[pos] = i;
}
int half = size/2;
for(int i =0;i<size;i++){
if(sorted_num[i]>=half)
cout<<sorted[i];
else
cout<<"-";
cout<<" ";
}
}