Sorting of Array in C

Sorting of Array in C Language

 

In this page we will look into a coding question where we will learn how to sort the array in C programming language. There are many sorting techniques to sort the array like quick sort, merge sort, bubble sort, insertion sort of them is scripted below.
There might be different approach to solve this question, one you will find here. If your approach is bit different post it onto the comment section.

sorting of array in C

Problem Statement

Write a program in C programming language where you need to find the sort the elements of array in ascending order.

Input Format
    -First line will contain the number of elements present in the array.
    -Second line will contain the elements of array

Output Format
   
-Output contain the space separated sorted elements in ascending order

Sample Test Case : 1

Input
9
4 2 1 6 8 2 9 3 5 7

Output
1 2 3 4 5 6 7 8 9

Sample Test Case : 2

Input
8
414 55 6 745 -215 663 -54 68

Output
-215 -54 6 55 68 414 663 745

Sorting of Array in C

Program for sorting the elements of c in ascending order

#include <stdio.h>
int main()
{
    int a[100];
    int nsumtemp;
    int ij;   
    scanf("%d",&n);
    for(i=0i<ni++)
    {
        scanf("%d",&a[i]);   
    }
    for(i=0i<ni++)
    {
        for(j=i+1j<nj++)
        {
            if(a[i]>a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    for(i=0i<ni++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}

Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures