Match makers – Sorting

QUESTION

Sort the given set of numbers using Selection Sort. The first line of the input contains the number of elements, the second line of the input contains the numbers to be sorted. In the output print the status of the array at the 3rd iteration and the final sorted array in the given format\n\nExample Input:\n8\n14 83 25 47 9 77 1 0\n\nOutput 1:\n0 1 25 47 9 77 83 14 \nSorted Array:0 1 9 14 25 47 77 83\n\nExplanation:\n14 83 25 47 9 77 1 0 \n0 83 25 47 9 77 1 14 \n0 1 25 47 9 77 83 14 \n0 1 9 47 25 77 83 14 \n0 1 9 14 25 77 83 47 \n0 1 9 14 25 77 83 47 \n0 1 9 14 25 47 83 77 \n0 1 9 14 25 47 77 83 \nSorted Array:0 1 9 14 25 47 77 83 \n\nThe Third Iteration: 0 1 25 47 9 77 83 14 \n.

“TESTCASE_1”: “5\n25 47 11 65 1\n###—###SEPERATOR—###—\n1 11 47 65 25 \nSorted Array:1 11 25 47 65”, “TESTCASE_2”: “7\n14 83 25 47 9 77 1\n###—###SEPERATOR—###—\n1 9 25 47 83 77 14 \nSorted Array:1 9 14 25 47 77 83”, “TESTCASE_3”: “8\n14 83 25 47 9 77 1 0\n###—###SEPERATOR—###—\n0 1 25 47 9 77 83 14 \nSorted Array:0 1 9 14 25 47 77 83”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
int main()
{
int n,i;
  scanf("%d",&n);
  int a[n];
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  int pos=0,j,temp=0;
  for(i=0;i<n;i++)
  {pos=i;
    for(j=i+1;j<n;j++)
    {
      if(a[pos]>a[j])
      {
        pos=j;
      }
    }
   if(pos!=i)
   {
     temp=a[i];
     a[i]=a[pos];
     a[pos]=temp;
   }
   if(i==1)
   {
     for(j=0;j<n;j++)
       printf("%d ",a[j]);
   }
  }  
  printf("\nSorted Array:");
  for(i=0;i<n;i++)
  {
    printf("%d ",a[i]);
  }
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro