Question

How to write this in c++?

1. Sort the following list using the selection sort algorithm as discussed in this chapter. Show the list after each iteratio

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

void print(int arr[], int size) {
    for (int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

void selection_sort(int arr[], int size) {
    int minIndex, temp;
    for (int i = 0; i < size - 1; ++i) {
        minIndex = i;
        for (int j = i + 1; j < size; ++j) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
        cout << "Array after iteration " << (i + 1) << ": ";
        print(arr, size);
    }
}

int main() {
    int array[] = {36, 55, 17, 35, 63, 85, 12, 48, 3, 66};
    int size = sizeof(array) / sizeof(int);
    print(array, size);
    selection_sort(array, size);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
How to write this in c++? 1. Sort the following list using the selection sort algorithm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT