C Program to Sort Array in Ascending Order

C program to sort array in ascending order; Through this tutorial, we will learn how to sort array in ascending order using the standard method, function, and pointer in c programs.

Programs to Sort Array in Ascending Order in C

Use the following programs to sort array in ascending order using the standard method, function, and pointer in c:

  • C Program to Sort Array in Ascending Order using Standard Method
  • C Program to Sort Array in Ascending Order using Function
  • C Program to Sort Array in Ascending Order using Pointer

C Program to Sort Array in Ascending Order using Standard Method

#include <stdio.h>

int main()
{
    int a[10000],i,n,j,temp;

    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }


    for(i=0; i<n-1; i++)
    {

        for(j=0; j<n-i-1; j++)
        {
           if(a[j]>a[j+1])
           {
           	temp=a[j];
           	a[j]=a[j+1];
           	a[j+1]=temp;
		   }

        }

    }
    printf("\narray elements in ascending order:\n ");

    for(i=0; i<n; i++)
    {
       printf("%d ",a[i]);
    }

 }

The output of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 1 8 7 2 6
array elements in ascending order:
 1 2 6 7 8 

C Program to Sort Array in Ascending Order using Function

#include <stdio.h>

int sort(int *a,int n)
{
    int i,j,temp;
     for(i=0; i<n-1; i++)
    {

        for(j=0; j<n-i-1; j++)
        {
           if(a[j]>a[j+1])
           {
           	temp=a[j];
           	a[j]=a[j+1];
           	a[j+1]=temp;
		   }

        }

    }



 }
 print(int *a,int n)
{

    int i;
    for(i=0; i<n; i++)
    {
       printf("%d ",a[i]);
    }


 }


int main()
{
    int a[10000],i,n,key;

    printf("Enter size of the  array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }

   sort(a,n);
      print(a,n);



}

The output of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 5 4 3 2 1
1 2 3 4 5 

C Program to Sort Array in Ascending Order using Pointer

#include <stdio.h>

void SortArray(int Size, int* parr)
{
	int i, j, temp;

	for (i = 0; i < Size; i++)
	{
		for (j = i + 1; j < Size; j++)
		{
			if(*(parr + j) < *(parr + i))
			{
				temp = *(parr + i);
				*(parr + i) = *(parr + j);
				*(parr + j) = temp;
			}
		}
	}
	printf("\nSorted Array Elements using Pointer = ");
	for(i = 0; i < Size; i++)
	{
		printf("%d  ", *(parr + i));
	}
}

int main()
{
	int Size;

	printf("\nEnter Array Size to Sort using Pointers = ");
	scanf("%d", &Size);

	int arr[Size];

	printf("\nPlease Enter %d elements of an Array = ", Size);
	for (int i = 0; i < Size; i++)
	{
		scanf("%d", &arr[i]);
    }
	SortArray(Size, arr);
	printf("\n");
}

The output of the above c program; as follows:

Enter Array Size to Sort using Pointers = 5
Please Enter 5 elements of an Array =  4 3 2 1 0
4 3 2 1 0

Sorted Array Elements using Pointer = 0  1  2  3  4  

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *