0% found this document useful (0 votes)
11 views2 pages

Practical 5

The document contains a C++ program that implements the selection sort algorithm for sorting arrays of integers and floats. It includes functions to sort the arrays and display their contents before and after sorting. The program demonstrates sorting an integer array and a float array, outputting the results to the console.

Uploaded by

purvapalankar123
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)
11 views2 pages

Practical 5

The document contains a C++ program that implements the selection sort algorithm for sorting arrays of integers and floats. It includes functions to sort the arrays and display their contents before and after sorting. The program demonstrates sorting an integer array and a float array, outputting the results to the console.

Uploaded by

purvapalankar123
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

#include <iostream>

template <typename T>

void selectionSort(T arr[], int size) {

for (int i = 0; i < size - 1; ++i) {

int minIndex = i;

for (int j = i + 1; j < size; ++j) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

if (minIndex != i) {

std::swap(arr[i], arr[minIndex]);

template <typename T>

void displayArray(const T arr[], int size) {

for (int i = 0; i < size; ++i) {

std::cout << arr[i] << " ";

std::cout << std::endl;

int main() {

const int intSize = 5;


const float floatSize = 5;

int intArray[intSize] = {5, 2, 9, 1, 5};

std::cout << "Original integer array: ";

displayArray(intArray, intSize);

selectionSort(intArray, intSize);

std::cout << "Sorted integer array: ";

displayArray(intArray, intSize);

float floatArray[floatSize] = {3.5, 1.2, 9.8, 2.1, 5.4};

std::cout << "\nOriginal float array: ";

displayArray(floatArray, floatSize);

selectionSort(floatArray, floatSize);

std::cout << "Sorted float array: ";

displayArray(floatArray, floatSize);

return 0;

/*Original integer array: 5 2 9 1 5

Sorted integer array: 1 2 5 5 9

Original float array: 3.5 1.2 9.8 2.1 5.4

Sorted float array: 1.2 2.1 3.5 5.4 9.8

*/

You might also like