Computer Science, asked by Arslanbazaz2902, 1 year ago

write a program in c++ that will input 10 number from the users in 1 dimensional array and then short the array in decending order using selectyion sort


CyberAkay: atleast you can thank me for answering your question

Answers

Answered by CyberAkay
3
Dear user,
Kindly mark the answer as brainliest if you find it useful.

Selection Sort in C++

To sort an array in ascending order using selection sort technique in C++ programming, then you have to ask to the user to enter the array size and array elements, now start comparing the array elements and start placing the smaller elements before bigger to arrange all the array elements in ascending order.


/* C++ Program - Selection Sort */ #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size; cout<<"Enter Array Elements : "; for(i=0; i<size; i++)
{ cin>>arr[i]; }
cout<<"Sorting array using selection sort...\n"; for(i=0; i<size; i++)
{ for(j=i+1; j<size; j++)
{ if(arr[i]>arr[j])
{ temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n"; for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}


Answered by samarthkrv
0

Answer:

#include <iostream>

void selection_sort(int arr[] , int len){

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

       int min = i;

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

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

               min = j;

           }

       }

       int temp = arr[i];

       arr[i] = arr[min];

       arr[min] = temp;

   }

}

int main() {

   int arr[] = {9,8,4,5,2,7,1,3,0,2};

   int len = sizeof(arr)/sizeof(arr[0]);

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

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

   }

   std::cout << "\n";

   selection_sort(arr , len);

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

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

   }

   return 0;

}

Explanation:

Similar questions