Life Hacks


      

Write a program of the Selection Sort.



#include<iostream.h>

#include<conio.h>

void main( ) /* main start */

{

clrscr( );

int array[10], n, i, j, temp; /* local variable */

cout << " Enter the number of elements in an array is : ”;

cin >> n; // inserting no. of elements in the array.

cout << “ Enter “ << “ “ << n << “ “ << ” number \n ” :;

for(i = 0; i < n; i++) // inserting elements in the array.

{

cin>>array[i];

} /* End of for loop*/

for(i = 0; i < n; i++) // sorting elements of the array by using selection sort technique.

{

for(j = i + 1; j < n; j++)

{

if(array[i] > array[j])

{

temp = array[i];

array[i] = array[j];

array[j] = temp;

}

} /* End of for loop*/

} /* End of for loop*/

cout << " \n Now sorted array is : ";

for(i = 0; i < n; i++) // Displays elements of the sorted array.

{

cout << array[i] <<" ";

} /* End of for loop*/

getch( );

}

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­Output:-

clip_image002

Leave a Reply