Answers for "sorting program 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
0

sorting in structure with c

#include <stdio.h>

struct student
{
    int rollno;
    char name[80];
    int marks;
};

void accept(struct student list[], int s);
void display(struct student list[], int s);
void bsortDesc(struct student list[], int s);

int main()
{
    struct student data[20];
    int n;

    printf("Number of records you want to enter? : ");
    scanf("%d", &n);

    accept(data, n);
    printf("\nBefore sorting");
    display(data, n);
    bsortDesc(data, n);
    printf("\nAfter sorting");
    display(data, n);

    return 0;
} 

void accept(struct student list[80], int s)
{
    int i;
    for (i = 0; i < s; i++)
    {
        printf("\n\nEnter data for Record #%d", i + 1);
        
        printf("\nEnter rollno : ");
        scanf("%d", &list[i].rollno);

        printf("Enter name : ");
        gets(list[i].name);

        printf("Enter marks : ");
        scanf("%d", &list[i].marks);
    } 
}

void display(struct student list[80], int s)
{
    int i;
    
    printf("\n\nRollno\tName\tMarks\n");
    for (i = 0; i < s; i++)
    {
        printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
    } 
}

void bsortDesc(struct student list[80], int s)
{
    int i, j;
    struct student temp;
    
    for (i = 0; i < s - 1; i++)
    {
        for (j = 0; j < (s - 1-i); j++)
        {
            if (list[j].marks < list[j + 1].marks)
            {
                temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            } 
        }
    }
}
Posted by: Guest on June-16-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
-2

using sort function in c

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
Posted by: Guest on March-02-2021

Code answers related to "C"

Browse Popular Code Answers by Language