Skip to content

Commit 519d37f

Browse files
fix: remove memory issues (#2533)
Co-authored-by: realstealthninja <[email protected]>
1 parent 9e44b68 commit 519d37f

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

sorting/counting_sort.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ int *Counting_Sort(int Arr[], int N) {
2727
int *Sorted_Arr = new int[N];
2828

2929
int *Count = new int[max - min + 1];
30+
for (int i = 0; i < max - min + 1; ++i) {
31+
Count[i] = 0;
32+
}
3033

3134
for (int i = 0; i < N; i++) Count[Arr[i] - min]++;
3235

@@ -37,6 +40,7 @@ int *Counting_Sort(int Arr[], int N) {
3740
Count[Arr[i] - min]--;
3841
}
3942

43+
delete[] Count;
4044
return Sorted_Arr;
4145
}
4246

@@ -51,6 +55,7 @@ int main() {
5155
Sorted_Arr = Counting_Sort(Arr, N);
5256
cout << "\n\t Sorted Array = ";
5357
Print(Sorted_Arr, N);
58+
delete[] Sorted_Arr;
5459
cout << endl;
5560

5661
return 0;

0 commit comments

Comments
 (0)