given an array of 6 elements sort first 3 in ascending and another 3 in descending order

0 votes
asked Oct 3 by (120 points)

5 Answers

+1 vote
answered Oct 3 by (160 points)
#include <iostream>
using namespace std;
 
int main()
{
    int arr[100];
    int size, i, j,k ,l ,temp;
 
    // Reading the size of the array
    cout<<"Enter size of array: ";
    cin>>size;
 
    //Reading elements of array
    cout<<"Enter elements in array: ";
    for(i=0; i<size; i++)
    {
        cin>>arr[i];
    }
    //Sorting an array in ascending order
    for(i=0; i<size/2; i++)
    {
        for(j=i+1; j<size/2; j++)
        {
            //If there is a smaller element found on right of the array then swap it.
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
       
    }
     //Sorting an array in descending order
     for(k=size/2; k<size; k++)
    {
        
        for(l=(size/2)+1; l<size; l++)
        {
            //If there is a smaller element found on right of the array then swap it.
            if(arr[l] > arr[k])
            {
                temp = arr[k];
                arr[k] = arr[l];
                arr[l] = temp;
            }
        }
    }
    //Printing the sorted array in ascending order
    cout<<"Elements of array in sorted order:"<<endl;
    for(i=0; i<size; i++)
    {
        cout<<arr[i]<<endl;
    }
 
    return 0;
}
0 votes
answered Oct 4 by anonymous
you can apply burble sort algorithm separated on each part using diferent critery, like

#include <stdio.h>

int main()
{
    int i, j;
    int vet[6];
    for(i = 0; i < 6; i++)
        scanf("%d", &vet[i]);
    //print the original array for comparasion
    printf("array:");
    for(i = 0; i < 6; i++)
        printf(" %d ", vet[i]);
    printf("\n");
    //sort the elements
    for(j = 2; j > 0; j--)
        for(i = 0; i < j; i++)
            if(vet[i] > vet[i + 1]){
                int aux = vet[i];
                vet[i] = vet[i + 1];
                vet[i + 1] = aux;
            }
    for(j = 5; j > 3; j--)
        for(i = 3; i < j; i++)
            if(vet[i] < vet[i + 1]){
                int aux = vet[i];
                vet[i] = vet[i + 1];
                vet[i + 1] = aux;
            }
    //print the resultant array for comparasion
    printf("array:");
    for(i = 0; i < 6; i++)
        printf(" %d ", vet[i]);
    printf("\n");

    return 0;
}
0 votes
answered Oct 4 by anonymous
a = []
b = []
for i in range(6):
    nos = int(input())
    if i > 2:
        b.append(nos)
    else:
        a.append(nos)
a.sort()
b.sort(reverse = True)
a +=  b
print(a)
0 votes
answered 3 days ago by (280 points)
#include<stdio.h>

int main()
{    int a[]={90,30,20,10,20,30};
    
       int i,j,t;
       for(i=0;i<=5;i++)
         printf(" %d",a[i]);

       printf("\n");
      for(i=0;i<3;i++)
      for(j=i+1;j<3;j++)

   {
       if(a[i]>a[j])
      t=a[i],a[i]=a[j],a[j]=t;
  }
  

  for(;i<=5;i++)
  for(j=i+1;j<=5;j++)
  {
       if(a[i]<a[j])
     t=a[i],a[i]=a[j],a[j]=t;
  }
 

    for(i=0;i<=5;i++)
    printf(" %d",a[i]);
        return (0);
}
0 votes
answered 3 days ago by (280 points)
#include <stdio.h>

int main()
{
    
    
           int a[]={90,30,20,10,20,30},  i,j,t;
    
             
                  for(i=0;i<=5;i++)
                  printf(" %d",a[i]);

                       printf("\n");

//main logic start from here
                 for(i=0;i<3;i++)
              for(j=i+1;j<3;j++)
  {
      if(a[i]>a[j])
      
      a[i]=a[i]+a[j]-(a[j]=a[i]);
  }
  
  
  
  for(;i<=5;i++)
  for(j=i+1;j<=5;j++)
  {
      if(a[i]<a[j])
        a[i]=a[i]+a[j]-(a[j]=a[i]);
  
  }
  

    for(i=0;i<=5;i++)
    printf(" %d",a[i]);
        return (0);
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...