Sorting an integer array in C

By: Ignatius Emailed: 1598 times Printed: 2044 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]);
       printf("\n\n");
       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

Comments(0)


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
Constants and escape sequences in C
UNIX read and write system calls sample program in C
A Storage Allocator sample program in C
ERRNO.H Header File in C
Printing a simple histogram in C
Getting Started with C
File Copying in C
Pointers and Arrays in C
Table Lookup - hashtab - example program in C
fgets(), fputs() - Line Input and Output - sample program in C
Using realloc() Function in C
Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C
The C Character Set
Writing The First C program
Most Emailed Articles (in C)
Conditional Expressions in C
fgets(), fputs() - Line Input and Output - sample program in C
Symbolic Constants using #define in C
Address Arithmetic and pointers in C
Character Pointers and Functions in C
Initialization of Pointer Arrays in C
Pointers to Structures example program in C
Using printf function in C
A Storage Allocator sample program in C
Using free() Function in C
The C Character Set
Writing The First C program
Formatting with printf in C
while, do while and for loops in C
The Birth and history of C Programming Language