Home > Backend Development > C++ > Explain the concept of sorting in C language

Explain the concept of sorting in C language

PHPz
Release: 2023-08-26 18:33:08
forward
866 people have browsed it

Explain the concept of sorting in C language

Question

Why does sorting in C make searching easier? How to judge the efficiency of sorting in C language?

Solution

Sort is the process of arranging elements in ascending (or descending) order.

  • The word sorting appeared when people realized the importance of fast search.

  • There are many things in life that need to be searched, such as specific records in the database, student numbers in the list, phone numbers in the phone book, specific page numbers in the book, etc.

  • If the data is saved in an unordered and unsorted form, searching for something specific becomes difficult. But luckily, the concept of sorting came along, making it easier for everyone to put data in order.

  • Sort arranges data into a sequence, making searching easier.

Sort Efficiency

  • If we want to sort a deck of cards in order, we check each card one by one and accordingly Adjust the deck.

  • It takes a long time to put the deck in order, but we still do it the same way. But, that's not how computers work.

  • Since the beginning of the programming era, scientists have been solving sorting problems through different algorithms.

The criteria for judging which algorithm is better than another are as follows:

  • The time required to sort the given data.
  • Required memory space.

Example

The following is a C program for sorting data:

#include<stdio.h>
int main(){
   int a[50], i,j,n,t,sm;
   printf("enter the No: of elements in the list:</p><p>");
   scanf("%d", &n);
   printf("enter the elements:</p><p>");
   for(i=0; i<n; i++){
      scanf ("%d", &a[i]);
   }
   for (i=0; i<n-1; i++){
      sm=i;
      for (j=i+1; j<n; j++){
         if (a[j] < a[sm]){
            sm=j;
         }
      }
      t=a[i];
      a[i]=a[sm];
      a[sm]=t;
   }
   printf ("after selection sorting the elements are:</p><p>");
   for (i=0; i<n; i++)
      printf("%d\t", a[i]);
   return 0;
}
Copy after login

Output

When the above program is executed, It produces the following result −

output

enter the No: of elements in the list:
4
enter the elements:
34
12
56
7
after selection sorting the elements are:
7 12 34 56
Copy after login

The above is the detailed content of Explain the concept of sorting in C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template