Sorting an integer array in C

By: Ignatius Emailed: 1700 times Printed: 2205 times

 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;
}``````
``` ```

Be the first one to add a comment

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

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 Sorting an integer array in C Line Counting sample program in C External Variables and Scope in C Relational and Logical Operators in C Bitwise Operators in C File Inclusion in C union example program in C scanf and sscanf sample program in C Fopen and Getc implementation program in C Bitwise Logical Operators in C Using the qsort() and bsearch() functions with values - example program in C Unicode and UTF-8 in C Infix to Prefix And Postfix in C Calculator in C
Most Emailed Articles (in C)
 Functions returning non-integer values in C File Copying in C Type Conversions in C (String to Integer, isdigit() etc) Conditional Expressions in C A C program similar to grep command in UNIX Initialization of Pointer Arrays in C Standard Input and Output in C Bitwise Logical Operators in C Printing a simple histogram in C Find square and square root for a given number in C The Birth and history of C Programming Language Basics of C Getting Started with C Variables and Arithmetic Expressions in C The for statement in C