Skip to content

Commit

Permalink
fix: remove memory issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Feb 12, 2024
1 parent 2dadbf7 commit 9c29933
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sorting/counting_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ int *Counting_Sort(int Arr[], int N) {
int *Sorted_Arr = new int[N];

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

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

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

delete[] Count;
return Sorted_Arr;
}

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

return 0;
Expand Down

0 comments on commit 9c29933

Please sign in to comment.