fix dynamic array issues in sorting folder

(cherry picked from commit 01b69fcb24)
This commit is contained in:
Krishna Vedala
2020-05-26 13:02:10 -04:00
parent 09f0733065
commit 231c99f880
9 changed files with 256 additions and 311 deletions

View File

@@ -1,40 +1,36 @@
//Insertion Sort
// Insertion Sort
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "\nEnter the length of your array : ";
cin >> n;
int Array[n];
cout << "\nEnter any " << n << " Numbers for Unsorted Array : ";
int main() {
int n;
std::cout << "\nEnter the length of your array : ";
std::cin >> n;
int *Array = new int[n];
std::cout << "\nEnter any " << n << " Numbers for Unsorted Array : ";
//Input
for (int i = 0; i < n; i++)
{
cin >> Array[i];
}
// Input
for (int i = 0; i < n; i++) {
std::cin >> Array[i];
}
//Sorting
for (int i = 1; i < n; i++)
{
int temp = Array[i];
int j = i - 1;
while (j >= 0 && temp < Array[j])
{
Array[j + 1] = Array[j];
j--;
}
Array[j + 1] = temp;
}
// Sorting
for (int i = 1; i < n; i++) {
int temp = Array[i];
int j = i - 1;
while (j >= 0 && temp < Array[j]) {
Array[j + 1] = Array[j];
j--;
}
Array[j + 1] = temp;
}
//Output
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++)
{
cout << Array[i] << "\t";
}
return 0;
// Output
std::cout << "\nSorted Array : ";
for (int i = 0; i < n; i++) {
std::cout << Array[i] << "\t";
}
delete[] Array;
return 0;
}