-
Notifications
You must be signed in to change notification settings - Fork 0
/
customSort.cpp
56 lines (38 loc) · 1 KB
/
customSort.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
#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
void customSort(int arr1[], int arr2[], int m, int n)
{
unordered_map<int, int> freq;
for (int i = 0; i < m; i++)
freq[arr1[i]]++;
int index = 0;
for (int i = 0; i < n; i++)
{
while (freq[arr2[i]])
{
arr1[index++] = arr2[i];
freq[arr2[i]]--;
}
freq.erase(arr2[i]);
}
int i = index;
for (auto it = freq.begin(); it != freq.end(); ++it)
while (it->second--)
arr1[index++] = (it->first);
sort(arr1 + i, arr1 + index);
}
int main()
{
int arr1[] = { 5, 8, 9, 3, 5, 7, 1, 3, 4, 9, 3, 5, 1, 8, 4 };
int arr2[] = { 3, 5, 7, 2 };
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
customSort(arr1, arr2, m, n);
cout << "After sorting the array is : ";
for (int i = 0; i < m; i++)
cout << arr1[i] << " ";
cout << endl;
return 0;
}