27/02/2023

C Program to Sort an Array in Ascending And Descending Order

Write a C Program to Sort an Array in Ascending And Descending Order Using For Loop. Arrays are a kind of data structure that can store a fixed-size sequential collection of elements of the same type. Program to Sort float array in C. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Float array in C Programming language.

C Program to Sort an Array in Ascending And Descending Order

C Program to Sort an Array in Ascending And Descending Order


#include <stdio.h>

main()
{
	int i, n = 11, j;
	float temp = 0.0 f;

	float a[11] = { 1.01, 6.66, 3.3, 7.5, 2.2, 4.1, 7.9, 9.7, 0.01, 9.14, 0.69 };

	printf("\nC Program to Sort an Array\n");

	printf("\n\nElement Before Sorting \n\n");

	for (i = 0; i <= 10; i++)
	{
		printf("%.2f  ", a[i]);
	}

	printf("\n");

	printf("\n\nElement After Sorting \n\n");

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

	//printing output of the program
	for (i = 0; i < 11; i++)
	{
		printf("%.2f  ", a[i]);
	}

	printf("\n\n");
}

The Output of C Program to Sort an Array


The Output of C Program to Sort an Array

Similar to Sort an Array


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: