My issue
i cant able to get correct output
My code
// C++ program to implement Selection Sort
#include <bits/stdc++.h>
using namespace std;
void selectionSort(vector<int> &arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
// Assume the current position holds
// the minimum element
int min_idx = i;
// Iterate through the unsorted portion
// to find the actual minimum
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_idx]) {
// Update min_idx if a smaller
// element is found
min_idx = j;
}
}
// Move minimum element to its
// correct position
swap(arr[i], arr[min_idx]);
}
}
void printArray(vector<int> &arr) {
for (int &val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector<int> arr = { 4, 5, 2, 10};
cout << "Original array: ";
printArray(arr);
selectionSort(arr);
cout << "Sorted array: ";
printArray(arr);
return 0;
}
Learning course: Design and analysis of Algorithms
Problem Link: https://www.codechef.com/learn/course/abesit-daa/ABESITDA19/problems/SESO36