0% found this document useful (0 votes)
4 views1 page

Selection Sort

The document contains a C++ program that implements the Selection Sort algorithm. It prompts the user to enter the size and values of an array, sorts the array using Selection Sort, and then displays the original and sorted arrays. The program demonstrates the process of finding the minimum element and swapping it with the first unsorted element in the array.

Uploaded by

tehreemwaqas391
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Selection Sort

The document contains a C++ program that implements the Selection Sort algorithm. It prompts the user to enter the size and values of an array, sorts the array using Selection Sort, and then displays the original and sorted arrays. The program demonstrates the process of finding the minimum element and swapping it with the first unsorted element in the array.

Uploaded by

tehreemwaqas391
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Selection Sort:

#include <iostream>
using namespace std;
int main() {
int size;
cout<<"enter size of array";
cin>>size;
int arr[size];
cout<<"enter values of array"<<endl;
for (int i = 0; i < size; i++) {
cin>> arr[i];
}
cout << endl;
cout << "Original array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Selection sort
for (int i = 0; i < size - 1; ++i) {
int minIndex = i;
// Find the minimum element in the unsorted part of the array
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
cout << "Sorted array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout <<endl;

return 0;}

You might also like