# Sort array in ascending order

Write a program in C language which accepts integer values from user, sorts it in ascending order and prints the integers in ascending order.

#include#include void main() { int arr[100],i,j,element,no,temp; clrscr(); printf("\nEnter the no of Elements: "); scanf("%d", &no); for(i=0;i arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } printf("\nSorted array:"); for(i=0;i

Output:Enter the no of Elements: 5 Enter Element 1: 2 Enter Element 2: 5 Enter Element 3: 2 Enter Element 4: 5 Enter Element 5: 3 Sorted array: 2 2 3 5 5

what is the logic for this example?

Logic here is very simple.

Take the input in an array.

then compare the first array element with others. If the first array element is greater than any of the other array element then swap it. The swapping will cause the smallest element to be placed at 1st position.

Now, compare the second array element with others and not the previous one (1st Array element). If second array element is greater than any of the other array element then swap. If its value is less then no need to swap.

This swapping will again cause second smallest element to be placed at position 2.

If you still have any query, then kindly reply. 🙂

Plz tel me hw to program ascending and descending order using array in LabView

can u explain me further briefly…?

This is probably the easiest and the best method of doing it…..

why have you taken another variable j??

cant it be solved only with i??

why is this in DESCENDING??

what if the input is -5, 98, 23, 91, 34 ??

98 and 23 will be swapped

98 and 91 will be swapped

34 and 98 will be swapped

and in the end the result will be

-5,23,91,34,91

which is not in order

am i right ??

urgently need help.

when i am running the above code, i get this error

Task 10.c:2:18: fatal error: conio.h: No such file or directory

how do i solve it?

Tnx for this example

I think the code mentioned below is more efficient.e.g.—If there are 5 no’s, we need only 4 pass in each of the for loop.But in your code there are 5 passes..i.e–one extra pass .we dont need that .

I may be wrong.whats your opinion, admin?

code–

for( i=0;i{{

for( j=i+1;j

#include<stdio.h>

#include<conio.h>

void main()

{

int a[100], i, n, s, j;

printf(“Enter size of array\t”);

scanf(“%d”, &n);

printf(“Enter the elements of array\t”);

for(i=0;ia[i+1])

{

s=a[i];

a[i]=a[i+1];

a[i+1]=s;

}

}

}

printf(“Array in ascending order\n”);

for(i=0;i

y is clrscr; function is used when not using functions…

The logic is simple….here he used arr[i] and arr[j] for an array itself…in it he compared two different elements of an array through i and j…..

Sir please said starting to end of program flow.

sir tell me full flow of program

sir i don’t know how can you used two for loops again you used one for loop

what if we want to sort the 2 d array?

hi. This code is really helpful but can you help me. I need to print first the original array content before it prints the sorted array. Thanks

Hi Iyle,

It is great to hear that this website helped you. If you liked it, share it. 🙂

Please find below the program that will first print the original array content and then the sorted array.

this is really helpful….thanks!

this is code for acs and des both

#include

#include

void main()

{clrscr();

int array[5],des[5],acs[5];

int temp;

int i;

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

{

printf("enter number: ");

scanf("%d",&array[i]);

}

for(int l=0;l<5;l++)

{

des[l]=array[l];

acs[l]=array[l];

}

printf("\n\ndes order:");

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

{

for(int j=i+1;j<5;j++)

{if(des[i]<des[j])

{temp=des[i];

des[i]=des[j];

des[j]=temp;}

}

}

for(int a=0;a<5;a++)

{printf("\n%d",des[a]);}

printf("\n\nacs order:");

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

{

for(int j=i+1;jacs[j])

{temp=acs[i];

acs[i]=acs[j];

acs[j]=temp;}

}

}

for(a=0;a<5;a++)

{

printf("\n%d",acs[a]);

}

getch();

}

input=90,19,-78,83,-31,-44-11,84,-93,66

output=-78,90,-31,19,-44,83,-11,84,-93,66

1. Write a complete C program that allows the user to do the following (in sequence)

i. Step 1 : Choose type one of the following Option :

a. Option 1 : Sorting The Number in Ascending Order

b. Option 2 : Sorting The Number in Descending Order

c. Option 3 : Find Biggest No

d. Option 4 : Find Smallest No

e. Option 5 : Calculate The Average of All Number

f. Option 6 : Calculate The Median Number

g. Option 7 : Exit System

ii. Step 2 : If user choose Option 1

i accept all those cammands ..bt the doubt is whn we comepare the last 2 numbers means i will not wrk…so am totaly confused abt that ..any one can xplain me plz..

pls explaim swapping in asecending order c program..

can anyone write program to sort 2 dimensional array plzz……