Write a C++ program to find the second and third smallest elements in a given array of
integers
#include <iostream>
using namespace std;
void bubbleSort(int array[], int size) {
// run loops two times: one for walking throught the array
// and the other for comparison
for (int step = 0; step < size - 1; ++step) {
for (int i = 0; i < size - step - 1; ++i) {
if (array[i] < array[i + 1]) {
// swap if greater is at the rear position
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
// function to print the array
void printArray() {
cout << " second largest =" << array[1]<<endl;
cout << " Third largest= " << array[2]<<endl;
}
int main() {
int data[] = {12, 45, 87, 88, 79};
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printArray();
Return 0;
}
Comments
Leave a comment