Sharp Tutorial
Sorting the array elements

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

Introduction

  • C Home
  • How To Install Software
  • First Program in C
  • Syntax and Rules
  • Data Types in C Language
  • Operators in C Language

C Programming

  • If - Else
  • Switch - Break
  • while loop
  • do while loop
  • for loop
  • Array in C
  • Multidimentional Array
  • String in C
  • Functions in C

C Advanced

  • Pointers
  • Structure
  • Sorting
  • Searching
  • File Handling
  • Stack

Important C Programs

  • Telephone bill
  • Factorial
  • Fibonacci series
  • Palindrome program
  • Armstrong No.
  • Prime No.
  • Pattern Programs
  • Maximum number from Array
  • Matrix addition& Multiplication
  • String reverse program
  • Student database
  • All Programs in C [must visit]

Sorting

Sorting is the process of arranging the elements either in ascending or descending order.
Many algorithm may be implemented to arrange the items ;

  1. selection sort
  2. bubble sort
  3. insertion sort
  4. heap sort
  5. merge sort
  6. shell sort etc.


How to sort array elements in ascending order.

#include <stdio.h>
#include <conio.h>
void main()
{
    int a[10], i, j, temp;
    printf("enter 10 unsorted array elements");
    for(i=0; i<10; i++)
      {
          scanf("%d", &a[i]);
       }
       
// logic to sort the elements in ascending order.     for(i=0; i<9; i++)       {           for(j=i+1; j<10; j++)           {                 if(a[j]<a[i])   // swap the elements if next elements are smaller than                   { // previous elements.                       temp=a[j];                       a[j]=a[i];                       a[i]=temp;                   }              }        }        
printf("sorted array elements are \n");     for(i=0; i<10; i++)     printf("%d ", a[i]); }

When we run above program we get the following result.

Selection Sort Program in C

#include <stdio.h>
#include <conio.h>
void main()
{
    int a[10], i, j, temp,minindex;
    printf("enter 10 unsorted array elements");
    for(i=0; i<10; i++)
      {
          scanf("%d", &a[i]);
       }
       
//selection sort logic to sort the elements in ascending order.     for(i=0; i<9; i++)       {
minindex=i;           for(j=i+1; j<10; j++)           {                 if(a[j]<a[minindex])   // swap the position if next element is smaller                   {                       minindex=j;                    }   
            }      temp=a[i];
           a[i]=a[minindex];
a[minindex]=temp;        }        printf("sorted elements are as follows\n");       for(i=0; i<10; i++)
{
printf("%d ", a[i]);
}
}

When we run above program we get the following result.

Bubble Sort Program in C

#include <stdio.h>
#include <conio.h>
void main()
{
    int a[10],n=10, i, j, temp;
    printf("enter 10 unsorted array elements");
    for(i=0; i<n; i++)
      {
          scanf("%d", &a[i]);
       } 
// bubble sort logic to sort the elements in ascending order.     for(i=0; i<n-1; i++)       {           for(j=0; j<n-1-i; j++)           {                 if(a[j]>a[j+1])                      {                       temp=a[j];                     a[j]=a[j+1]; 
                a[j+1]=temp; }
}        }        printf("sorted elements are as follows\n");       for(i=0; i<10; i++)
{
printf("%d ", a[i]);
}
}

When we run above program we get the following result.

Insertion Sort Program in C

#include <stdio.h>
#include <conio.h>
void main()
{
    int a[10],n=10, i, j, key;
    printf("enter 10 unsorted array elements");
    for(i=0; i<n; i++)
      {
          scanf("%d", &a[i]);
       } 
// insertion sort logic to sort the elements in ascending order.     for(i=1; i<n; i++)       { key=a[i]; j=i-1;           while(j>=0 && a[j]>key)           {                 if(a[j]>a[j+1])                      {                      a[j+1]=a[j];                     j=j-1; 
               } a[j+1]=key;
}        }        printf("sorted elements are as follows\n");       for(i=0; i<10; i++)
{
printf("%d ", a[i]);
}
}

When we run above program we get the following result.

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial