-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0912-Sort_an_Array.cpp
More file actions
125 lines (112 loc) · 3.1 KB
/
0912-Sort_an_Array.cpp
File metadata and controls
125 lines (112 loc) · 3.1 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
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************************************************
* 0912-Sort_an_Array.cpp
* Billy.Ljm
* 01 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/sort-an-array/description/
* 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.
*
* ===========
* My Approach
* ===========
* The problem is asking for Heapsort. It has a time complexity of O(nlog(n)),
* albeit it is typically slow in practice, and the space complexity is O(1)
* since it is sort-in-place. Thus, it satisfies the required scaling of time
* complexity and minimises the space complexity as requested.
******************************************************************************/
#include <iostream>
#include <vector>
class Solution {
public:
/**
* Heapsorts an array
*
* @param nums The array to be converted into a Max-Heap.
*
* @return the sorted array; the array is sorted in-place but also returned.
*/
std::vector<int> sortArray(std::vector<int>& nums) {
// creates Max-Heap from (unordered) array
int n = int(nums.size());
buildHeap(nums, int(n));
for (int i = n - 1; i >= 0; i--) {
// swaps the largest root element with the end of the array
std::swap(nums[0], nums[i]);
// heapifies the remainder of the array
// the left and right subtrees are individually max-heaped already
heapify(nums, i, 0);
}
return nums;
}
private:
/**
* Helps create a Max-Heap from the given array.
*
* @param nums The array to be converted into a Max-Heap.
* @param n The size of the array.
* @param i The root of the subtree to heapify
*/
void heapify(std::vector<int>& nums, int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && nums[l] > nums[largest]) {
largest = l;
}
if (r < n && nums[r] > nums[largest]) {
largest = r;
}
if (largest != i) {
std::swap(nums[i], nums[largest]);
heapify(nums, n, largest);
}
}
/**
* Creates a Max-Heap from the given array.
* Iterates through the binary tree from bottom to top, and heapifies
*
* @params nums The array to be converted into a Max-Heap.
* @params n The size of the array.
*/
void buildHeap(std::vector<int>& nums, int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(nums, n, i);
}
}
};
/**
* Test Cases
*/
int main() {
Solution solution;
// test case 1
std::vector<int> nums = {5, 2, 3, 1};
std::cout << "unsorted: ";
for (int i = 0; i < nums.size(); i++) {
std::cout << nums[i] << " ";
}
std::cout << "\nsorted: ";
solution.sortArray(nums);
for (int i = 0; i < nums.size(); i++) {
std::cout << nums[i] << " ";
}
std::cout << std::endl;
// test case 2
nums = {5, 1, 1, 2, 0, 0};
std::cout << "unsorted: ";
for (int i = 0; i < nums.size(); i++) {
std::cout << nums[i] << " ";
}
std::cout << "\nsorted: ";
solution.sortArray(nums);
for (int i = 0; i < nums.size(); i++) {
std::cout << nums[i] << " ";
}
std::cout << std::endl;
return 0;
}