Implement Selection sort algorithm in C++
Code
// selection sort#include <iostream>using namespace std;int main(){int n, i, j, temp;int arr[50];cout << "Enter the size of array: ";cin >> n;cout << "Enter the elements of array: " << endl;for (i = 0; i < n; i++){cin >> arr[i];}for (i = 0; i < n; i++){for (j = i + 1; j < n; j++){if (arr[i] > arr[j]){temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}cout << "Sorted array: ";for (i = 0; i < n; i++){cout << arr[i] << " ";}cout << endl;return 0;}
Output
Enter the size of array: 5
Enter the elements of array:
4
3
1
5
4
Sorted array: 1 3 4 4 5
0 Comments
Post a Comment