Computer Science, asked by rahulkumar9572, 1 year ago

Write a program to sort the elements by using selection sort

Answers

Answered by Nitish0001
4
Selection sort in C

#include <stdio.h>

int main()

{

int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

Answered by Anonymous
2

import java.util.*;

class selection_sort

{

   public static void main(String args[])

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter the number of elements in an array");

       int n=sc.nextInt();

       int a[]=new int[n];

       System.out.println("Enter the array values randomly ! I will sort it ! I know magic");

       for(int i=0;i<n;i++)

       {

           a[i]=sc.nextInt();

       }

       int min=0;

       int temp=0;

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

       {

           min=i;

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

           {

               if(a[j]<a[min])

               {

                   min=j;

               }

           }

           temp=a[i];

           a[i]=a[min];

           a[min]=temp;

       }

       System.out.println("Sorted array in ascending order gives all negative first then positive hehe");

       for(int i=0;i<n;i++)

       {

           System.out.println(a[i]);

       }

   }

}

Similar questions