-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path912_Sort_an_Array.cpp
More file actions
113 lines (112 loc) · 2.7 KB
/
Copy path912_Sort_an_Array.cpp
File metadata and controls
113 lines (112 loc) · 2.7 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
912. Sort an Array
Given an array of integers nums, sort the array in ascending order and return it.
You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Explanation: Note that the values of nums are not necessairly unique.
Constraints:
1 <= nums.length <= 5 * 104
-5 * 104 <= nums[i] <= 5 * 104
*/
/*
USING HASHMAP
Time Complexity O(n);
Space Complexity O(n);
*/
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
map<int,int> map;
for(int i:nums) map[i]++;
nums.clear();
for(auto i:map){
int f = i.first;
while(map[f]){
nums.push_back(f);
map[f]--;
// if(map[f]==0) map.erase(f);
}
}
return nums;
}
};
/*
USING MERGE SORT
Time Complexity O(n);
Space Complexity O(n);
*/
class Solution {
public:
vector<int> mergesorted(vector<int>&arr)
{
if(arr.size()==1)
{
return arr;
}
int dividesize=arr.size()/2;
vector<int>merge1,merge2;
for(int i=0;i<dividesize;i++)
{
merge1.push_back(arr[i]);
}
for(int i=dividesize;i<arr.size();i++)
{
merge2.push_back(arr[i]);
}
merge1=mergesorted(merge1);
merge2=mergesorted(merge2);
int ptr1=0,ptr2=0,ptr3=0;
while(ptr3<arr.size())
{
if(ptr1==merge1.size())
{
arr[ptr3]=merge2[ptr2];
ptr2++;
ptr3++;
continue;
}
if(ptr2==merge2.size())
{
arr[ptr3]=merge1[ptr1];
ptr1++;
ptr3++;
continue;
}
if(merge1[ptr1]<merge2[ptr2])
{
arr[ptr3]=merge1[ptr1];
ptr1++;
ptr3++;
}
else
{
arr[ptr3]=merge2[ptr2];
ptr2++;
ptr3++;
}
}
return arr;
}
vector<int> sortArray(vector<int>&nums)
{
return mergesorted(nums);
}
};
/*
USING In-Built Sort Function
Time Complexity O(n);
Space Complexity O(n);
*/
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
sort(nums.begin(),nums.end());
return nums;
}
};