Answers for "sorting array in c"

C
6

sorting program in c

#include<stdio.h>
int main(){
   /* Here i & j for loop counters, temp for swapping,
    * count for total number of elements, number[] to
    * store the input numbers in array. You can increase
    * or decrease the size of number array as per requirement
    */
   int i, j, count, temp, number[25];

   printf("How many numbers u are going to enter?: ");
   scanf("%d",&count);

   printf("Enter %d elements: ", count);
   // Loop to get the elements stored in array
   for(i=0;i<count;i++)
      scanf("%d",&number[i]);
 
   // Logic of selection sort algorithm
   for(i=0;i<count;i++){
      for(j=i+1;j<count;j++){
         if(number[i]>number[j]){
            temp=number[i];
            number[i]=number[j];
            number[j]=temp;
         }
      }
   }

   printf("Sorted elements: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
}
Posted by: Guest on October-08-2020
2

sorting in c

#include <stdio.h>
int main()
{

     int ara[1000];
     int high;
     printf("How many numbers are you going to enter ? (max:1000) : ");
     scanf("%d", &high);
     int i, j, min;
     printf("\n");
     for (i = 0; i < high; i++)
     {
          printf("Type number : ");
          scanf("%d", &ara[i]);
     }
     printf("\n The numbers arranged in ascending order are : \n");
     for (i = 0; i < high; i++)
     {
          for (j = i; j < high; j++)
          {
               if (ara[i] > ara[j])
               {
                    min = ara[i];
                    ara[i] = ara[j];
                    ara[j] = min;
               }
          }
          // The numbers arranged in ascending order are
          printf("%d\n", ara[i]);
     }
     return 0;
}
Posted by: Guest on July-05-2021
0

sorting array in c

#include <stdio.h>

int main()
{
    int ara[10]={4,45,23,53,2,64,56,34,77,98};
    int i,j,temp;
    for(i = 0; i < 10; i++){
        
        for(j = 0; j< 10; j++){
            if(ara[i] < ara[j]){ //use getter than (>) symbol for sorting from getter than to less than
                
                temp = ara[i];
                ara[i] = ara[j];
                ara[j] = temp;
                
            }
        }
        
    }
    
    for(i = 0; i < 10; i++){ //printing array sorted array elements
        
        printf("%d\t",ara[i]);
    }
    return 0;
}
Posted by: Guest on July-13-2021
-1

sorting array in c

#include <stdio.h>
    void main()
    {
 
        int i, j, a, n, number[30];
        printf("Enter the value of N \n");
        scanf("%d", &n);
 
        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);
 
        for (i = 0; i < n; ++i) 
        {
 
            for (j = i + 1; j < n; ++j)
            {
 
                if (number[i] > number[j]) 
                {
 
                    a =  number[i];
                    number[i] = number[j];
                    number[j] = a;
 
                }
 
            }
 
        }
 
        printf("The numbers arranged in ascending order are given below \n");
        for (i = 0; i < n; ++i)
            printf("%d\n", number[i]);
 }
Posted by: Guest on September-19-2020

Code answers related to "C"

Browse Popular Code Answers by Language