Important alert: (current site time 8/23/2014 1:00:28 PM EDT)
 

VB icon

Sort an integer array in ascending order

Email
Submitted on: 10/29/2002 12:22:35 PM
By: Zohair Ali 
Level: Beginner
User Rating: By 2 Users
Compatibility: C
Views: 7221
author picture
(About the author)
 
     This code will read n integers (n can have a maximum value of 100 but you can alter the code to support a larger value) from the keyboard. Next it will sort them in ascending order and it will finally print the sorted list on the screen. (P.S Note that by changing ">" operator to "<" (in sorting algo.) you can also print the array in descending order.)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Sort an integer array in ascending order
// Description:This code will read n integers (n can have a maximum value of 100 but you can alter the code to support a larger value) from the keyboard. Next it will sort them in ascending order and it will finally print the sorted list on the screen.
(P.S Note that by changing ">" operator to "<" (in sorting algo.) you can also print the array in descending order.)
// By: Zohair Ali
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5064&lngWId=3//for details.//**************************************

//To sort an array in ascending order. (By Zohair Ali)
//zohairali1@hotmail.com
#include <stdio.h>
#include <conio.h>
void main(void)
{
int n;//holds the total no. of vals.
int a[100];//Holds the numbers to be sorted
int i,i2,i3,temp,z;//i,i2,i3 & z are loop counters
clrscr();
printf ("Enter the no. of values you want to enter(100 max)>");
scanf ("%d",&n);
for (i=0;i<n;i++)//reads nos
{
scanf ("%d",&a[i]);
}
for (i2=0;i2<n;i2++)//These 2 nested loops sort the list.
{
	for (i3=0;i3<n-1;i3++)//i3 is kept <n-1 so that a[i3+1] may not become a[n+1](out of bounds) at the end of execution of final step of the loop
	{
	if (a[i3]>a[i3+1])
		{
		temp=a[i3];//temp is used to swap a[i3] and a[i3+1]
		a[i3]=a[i3+1];
		a[i3+1]=temp;
		}
	}
}
clrscr();//this part prints the sorted array.
printf ("Printing sorted array\n");
for (z=0;z<n;z++)
printf ("%d\n",a[z]);
getch();
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/19/2002 10:03:46 AM

thank you,, no error, this code is working....
(If this comment was disrespectful, please report it.)

 
2/17/2003 11:11:17 PM

How would I write code for a series of inputs to be printed in an lowest/highest acsending order? eg. 12,50,13,52,20,55,28,60,40...
(If this comment was disrespectful, please report it.)

 
2/21/2003 4:51:58 AMZohair Ali

I dont understand your Question. Do you want to supply input in a series with out pressing Enter key after each value?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.