Monday, September 17, 2018

Sort by Select

  • Problem Description

    There were group of friends standing in a straight line. But the class teacher asked them to stand in a ascending order. Can you write a program for this scenario using arrays
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int a,i,j,temp,arr[100];
      scanf("%d",&a);
      for(i=0;i<a;i++)
      {
        scanf("%d",&arr[i]);
      }
      for(i=0;i<a;i++)
      {
        for(j=i;j<a;j++)
        {
          if(arr[i]>arr[j])
          {
            temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
          }
        }
      }
      for(i=0;i<a;i++)
      {
        printf("%d ",arr[i]);
      }

    return 0;
    }
  • Test Case 1

    Input (stdin)
    9
    
    9 8 7 6 5 4 3 2 1
    
    
    Expected Output
    1 2 3 4 5 6 7 8 9
  • Test Case 2

    Input (stdin)
    4
    
    9 19 7 99
    
    
    Expected Output
    7 9 19 99

1 comment: