Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created November 27, 2024 09:08
Show Gist options
  • Select an option

  • Save vlaleli/de8d0afc22cbbc2b2cd3f06647911000 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/de8d0afc22cbbc2b2cd3f06647911000 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main() {
const int size = 15;
int arr[size];
srand(time(0));
for (int i = 0; i < size; ++i) {
arr[i] = rand() % 61 - 30;
}
cout << "Initial array: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
bool sorted = true;
for (int i = 0; i < size - 1; ++i) {
if (arr[i] < arr[i + 1]) {
sorted = false;
break;
}
}
if (!sorted) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] < arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
cout << "Sorted array: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
} else {
cout << "The array is already sorted." << endl;
}
int maxElement = arr[0];
int minElement = arr[size - 1];
cout << "Maximum element: " << maxElement << endl;
cout << "Minimum element: " << minElement << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment