Sie sind auf Seite 1von 5

« Prev Page Next Page »

C Program to Sort the Array in an Ascending Order


This is a C Program to sort an array in ascending order.

Problem Description
This program will implement a one-dimentional array of some fixed size, filled with
some random numbers, then will sort all the filled elements of the array.

Problem Solution
1. Create an array of fixed size (maximum capacity), lets say 10.
2. Take n, a variable which stores the number of elements of the array, less
than maximum capacity of array.
3. Iterate via for loop to take array elements as input, and print them.
4. The array elements are in unsorted fashion, to sort them, make a nested loop.
5. In the nested loop, the each element will be compared to all the elements
below it.
6. In case the element is greater than the element present below it, then they
are interchanged
7. After executing the nested loop, we will obtain an array in ascending order
arranged elements.

advertisement

Program/Source Code
Here is source code of the C program to sort the array in an ascending order. The
program is successfully compiled and tested using Turbo C compiler in windows
environment. The program output is also shown below.

/*
* C program to accept N numbers and arrange them in an ascending order
*/

#include <stdio.h>
void main()
{

int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);

printf("Enter the numbers \n");


for (i = 0; i < n; ++i)
scanf("%d", &number[i]);

for (i = 0; i < n; ++i)


{

for (j = i + 1; j < n; ++j)


{

if (number[i] > number[j])


{

a = number[i];
number[i] = number[j];
number[j] = a;

printf("The numbers arranged in ascending order are given below \n");


for (i = 0; i < n; ++i)
printf("%d\n", number[i]);

}
Program Explanation
1. Declare an array of some fixed capacity, lets say 30.
2. From users, take a number N as input, which will indicate the number of
elements in the array (N <= maximum capacity)
3. Iterating through for loops (from [0 to N) ), take integers as input from
user and print them. These input are the elements of the array.
4. Now, create a nested for loop with i and j as iterators.
5. Start the sorting in ascending order by extracting each element at position i
of outer loop.
6. This element is being compared to every element from position i+1 to size-1
(means all elements present below this extracted element)
7. In case any of the extracted element is greater than the element below it,
then these two interchange their position, else the loop continues.
8. After this nested loop gets executed, we get all the elements of the array
sorted in ascending order.

advertisement

Runtime Test Cases

Enter the value of N


6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data Structures and
Algorithms.

« Prev Page - C Program to Print the Factorial of a given Number


» Next Page - C Program to Illustrate the Operations of Singly Linked List

Deep Dive @ Sanfoundry:


Python Programming Examples
C++ Programming Examples on Combinatorial Problems & Algorithms
Java Programming Examples on Combinatorial Problems & Algorithms
C Programming Examples using Recursion
C Programming Examples on Combinatorial Problems & Algorithms
C Programming Examples on Matrix
C# Programming Examples on Arrays
C Programming Examples on Searching and Sorting
C# Programming Examples on Sorting
C Programming Examples on Arrays

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder
and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is
passionate about competency developments in these areas. He lives in Bangalore and
delivers focused training sessions to IT professionals in Linux Kernel, Linux
Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster
Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals
and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+
Subscribe Sanfoundry Newsletter and Posts

Name*

Email*
Subscribe
Best Careers
Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos


Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel
Programming. Our Founder has trained employees of almost all Top Companies in India
such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft,
Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-
Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL,
Mindtree, Cognizant and Startups.
Best Trainings
SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers
Best Reference Books
Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books
Questions and Answers
1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers
India Internships
Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships
About Sanfoundry
About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011-2018 Sanfoundry. All Rights Reserved.
Did You Know?
1
Motorola designed and made a pill that when swallowed, IDs your digestive juices
and makes your body able to unlock your electronic devices by touch.
2
Since 2012, almost all mobile phones in Japan were made waterproof because women
liked to use their phone in the shower.
Sponsored
Advertisement
3
By typing “about: robots” into the Mozilla Firefox URL bar, it will load and show a
message from our "future robotic overlords."
Sponsored
Advertisement
4
In 2012, a French woman received a phone bill for $15 quadrillion dollars of
cancellation fees. The company only admitted the error after offering her a payment
plan twice.
5
In 1998, Sony had to recall 700,000 camcorders with night vision technology after
they realized it could see through people’s clothes.
6
__________ ______ __________ _____ ___ _______ __________ ______ __________ _____
___ _______ __________ ______ _____
7
__________ ______ __________ _____ ___ _______ __________ ______ __________ _____
___ _______ __________ ______ _____
8
__________ ______ __________ _____ ___ _______ __________ ______ __________ _____
___ _______ __________ ______ _____
9
__________ ______ __________ _____ ___ _______ __________ ______ __________ _____
___ _______ __________ ______ _____
10
__________ ______ __________ _____ ___ _______ __________ ______ __________ _____
___ _______ __________ ______ _____
Sponsored
Advertisement

Das könnte Ihnen auch gefallen