Question

Write a c++ program that sorts one – dimensional array’s value into ascending order using the...

Write a c++ program that sorts one – dimensional array’s value into ascending order using the selection sort method

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

using namespace std;

void selectionSort(int arr[], int n){
    int i, j, k, temp;
 
    for (i = 0; i < n-1; i++){
        k = i;
        for (j = i+1; j < n; j++){
           if (arr[j] < arr[k]){
               k = j;
           }
        }
 
        temp = arr[i];
        arr[i] = arr[k];
        arr[k] = temp;
    }
}

int main()
{
       int arr[3];
       int n;
       cout<<"Enter number of elements in the array: ";
       cin>>n;
       cout<<"Enter "<<n<<" numbers for the array: ";
       for(int i = 0;i<n;i++){
          cin>>arr[i];
   }
    selectionSort(arr,n);
    for(int i = 0;i<n;i++){
       cout<<arr[i]<<" ";
   }
       return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a c++ program that sorts one – dimensional array’s value into ascending order using the...
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
Active Questions
ADVERTISEMENT