You are on page 1of 6

Sort array in ascending descending order?

Charan315
Answered Last

#include<stdio.h> 
#include<conio.h> 

void main() 

int a[10],i,n,j,t; 
clrscr(); 
printf("enter size of array: "); 
scanf("%d",&n); 
printf(" enter array elements: "); 
for(i=0;i<n;i++) 

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

printf(" array before sorting\n"); 
for(i=0;i<n;i++) 

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

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

for(j=i+1;j<n;j++) 

if(a[i]>a[j]) 

t=a[i]; 
a[i]=a[j]; 
a[j]=t; 



printf("\n after sorting\n"); 
for(i=0;i<n;i++) 

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

getch(); 
}

#include<iostream>
#include<conio.h>

using namespace std;

template <class T>


void s_sort(T a[],int n)
{
     int i,j,t;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i]) //for descending order use if(a[j]>a[i]) 
                                       {
                                                    t=a[i];
                                                    a[i]=a[j];
                                                    a[j]=t;
                                       }
                     }
     }
}

int main()
{
    int a[100],i,n;
    cout<<"Enter The number of Element:\n";
    cin>>n;
    cout<<"\nEnter Elements:\n";
    for(i=0;i<n;i++)
    {

                      cout<<"\nEnter:";
                    cin>>a[i];
    }
    s_sort(a,n);
    cout<<"\nAfter Sorting\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\t";
    }
    getch();
    return 0;
}

Screenshot:

I need help with writing a function called swap that sorts the elements of an array in order from
highest to lowest values where they descend and ascend. A particular thing about this function is
that swap does NOT re-arrange elements in the array so it just uses a second array of indexes for
the elements in the original array and then swap sorts the second array based on the values in the
original array. A sorted version of the original array can then be produced with these sorted
indexes. You can declare and initialize the original array without any user input. For example, int
arr[10] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 };
Header of the function swap must be as shown below: void swap(int array[],int swapedIndexes
[], int size, char mode) When mode is 'a', the function sorts the array in the ascending order, and
when mode is 'd', the function sorts it in the descending order. int
swappedIndexes[5];swap(array,swappedInde… 5, 'a');
So far I have this code that randomly generates arrays of 10 elements and I found the max and
min, but I am confused on what to do about the swapping part? Also I cant figure out how to
make the numbers ascend or descend in numerical order?? Can somebody please please help me
to finish with writing this code?
Here is what i have done so far:

#include<ctime>
#include <iostream>
using namespace std;
int min(int [],int);
void max(int [],int , int &);
void main()
{ srand(time(0));
//1-declare
int g[10];
int res=0;
//2-init
for(int i=0;i<10;i++)
{ g[i]=rand()%250;
cout <<g[i] << "\t";
}

//3- processing
res=min(g,10);
cout << "\nthe minimum is " << res<<endl;
max(g,10,res);
cout << "the maximum is " << res<<endl;
//printing

system("pause");
}
int min(int a[],int n)
{ int m=a[0];

for(int i=1;i<n;i++)
if( a[i] <m)
m=a[i];
return m;
}
void max(int a[],int n, int &m)
{ m=a[0];

for(int i=1;i<n;i++)
if( a[i] > m)
m=a[i];

}
 Jul 6, 2013 at 7:11pm
mokhi64 (19)
dear friend !
if you want to write it by yourself i can help you by :
1 #include <iostream> Edit & Run
2 using namespace std;
3
4 int main() {
5 int a[10], swap;
6 for ( int i = 0; i < 10; ++i )
7 cin >> a[i];
8 for ( int i = 0; i < 10; ++i ) {
9 for ( int j = i; j < 10; ++j ){
10 if( a[j] > a[i] ){
11 swap = a[i];
12 a[i] = a[j];
13 a[j] = swap;
14 }
15 }
16 }
17
18 for ( int i = 0; i < 10; ++i )
19 cout << a[i] << " ";
20 return 0;
21 }

===========================================

but i suggest you to use sort function in <algorithm> & <vector> header files

1 vector<double> a;
2 //filling vector by a.push_back(/*something from STDIN or a file*/);
3 sort(a.begin(), a.end(), /*a function class like greater_than or
less_than*/);

You might also like