Sorting an integer array in C

By: Ignatius Emailed: 1728 times Printed: 2329 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This simple C program sorts the elements of an integer array and prints them.

#include <stdio.h>
#define  SIZE   10
int main()
       int a[SIZE] = {34,6,41,58,0,12,89,-2,45,25};
       int i, pass, hold;
       printf("Data items in original order\n\n");
       // displaying the original array...
       for(i=0; i<=SIZE - 1; i++)
              printf("%d  ", a[i]);
       // ------do the sorting...ascending-------------
       // for every array elements do this...
       for(pass = 1; pass <= (SIZE-1); pass++)
            // for every 2 array elements comparison do
            // the comparison and swap...
            for(i = 0; i <= (SIZE-2); i++)
              // set the condition...
              if(a[i] > a[i + 1])
                     // put the a[i] in temporary variable hold...
                     hold = a[i];
                     // put the a[i + 1] in a[i]
                     a[i] = a[i + 1];
                     // put the hold in a[i + 1], one swapping is
                     // completed...and repeat for other elements...
                     a[i + 1] = hold;
       printf("\n\nData items in ascending order\n\n");
       // display the new ordered list...
       for (i=0; i <= (SIZE-1); i++)
            printf("%4d", a[i]);
       return 0;

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Ignatius
Sorting an array of Strings in C++
Sorting an integer array in C
Printing a simple histogram in C
Sum of the elements of an array in C
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
goto and labels in C
Find square and square root for a given number in C
Open, Creat, Close, Unlink system calls sample program in C
Using calloc() Function in C
perror() Function - example program in C
Sorting an integer array in C
Character Arrays in C
scanf and sscanf sample program in C
lseek() sample program in C
Arrays sample program in C
register Variables in C
Arrays of Structures example program in C
union example program in C
fgets(), fputs() - Line Input and Output - sample program in C
Most Emailed Articles (in C)
Listing Files and Directories sample program in C
Arithmetic Operators in C
Relational and Logical Operators in C
Increment and Decrement Operators in C
register Variables in C
File Inclusion in C
Pointers and Function Arguments in C
Pointers and Arrays in C
Character Pointers and Functions in C
Pointer Arrays and Pointers to Pointers in C
Multi-dimensional Arrays in C (Explained using date conversion program)
Initialization of Pointer Arrays in C
The Birth and history of C Programming Language
Basics of C
Getting Started with C