SORTING PROGRAM
#include <iostream>
using namespace std;
int main() {
int n[5], i, num_elements;
cout << "Enter the number of elements (up to 3): ";
cin >> num_elements;
if (num_elements > 3) {
cout << "Number of elements exceeds the limit of 3." << end;
return 1;
}
cout << "Enter " << num_elements << " elements:" << endl;
for (i = 0; i < num_elements; i++) {
cin >> n[i];
}
int temp, j;
for (i = 1; i < num_elements; i++) {
j = i;
while (j > 0 && n[j] < n[j - 1]) {
temp = n[j];
n[j] = n[j - 1];
n[j - 1] = temp;
j--;
}
}
cout << "Sorted elements: ";
for (i = 0; i < num_elements; i++) {
cout << n[i] << " ";
}
cout << endl;
return 0;
}
Comments
Post a Comment