C++ program to sort an array in ascending and descending order
Sort First half in Ascending and Second half in descending order in C++
Here, in this page we will discuss the program to sort first half in ascending and second half in descending order in C++ programming language. We are given with an array and need to print the required sorted array in the desired way.
Here, in this page we will discuss two different methods to sort the given array such that it’s first half is in ascending order and second half in descending order.
- Method 1 : Using bubble sort
- Method 2 : Sort the entire array then, print first half in ascending and second half in descending.
Example
Input : arr[6] = [1, 90, 34, 89, 7, 9]Output : 1 7 9 90 89 34
Method 1 :
This program takes a lot of inspiration from the bubble sort.
Apart from the fact that we divide the array into two halves. In the first half we sort in ascending order and in the second we sort in descending order.
Method 1 : Code in C++
Run
#include<bits/stdc++.h> using namespace std; void ascDecFunc(int a[], int n) { int temp; for(int i=0;i < n-1;i++) { for(int j = 0;j < n/2; j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } for(int j = n/2;j < n-1; j++) { if(a[j] < a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } for(int i = 0; i < n; i++) cout<<a[i]<<" "; } int main() { int arr[] = {3, 2, 4, 1, 10, 30, 40, 20}; int len = sizeof(arr) / sizeof(arr[0]); ascDecFunc(arr, len); return 0; }
Output
1 2 3 4 40 30 20 10
Method 2 :
- Sort the given array.
- Run a loop up to half the length of the array and print the elements of the sorted array.
- Run a loop from the last index of the array to the middle of the array and print the elements in reverse order.
Method 2 : Code in C++
Run
#include<bits/stdc++.h> using namespace std; void ascDecFunc(int a[], int n) { sort(a, a+n); // printing first half in ascending order for (int i = 0; i < n / 2; i++) cout<<a[i]<<" "; // printing second half in descending order for (int j = n - 1; j >= n / 2; j--) cout<<a[j]<<" "; } int main() { int arr[] = {3, 2, 4, 1, 10, 30, 40, 20}; int len = sizeof(arr) / sizeof(arr[0]); ascDecFunc(arr, len); return 0; }
Output
1 2 3 4 40 30 20 10
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
#include
using namespace std;
int main()
{
int arr[]={3, 2, 4, 1, 10, 30, 40, 20};
int size = sizeof(arr)/sizeof(arr[0]);
sort(arr,arr+size);
int mid = size/2;
int s = mid;
int e = size-1;
while(s<e)
{
swap(arr[s],arr[e]);
s++;
e–;
}
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Hey, Our mentors would be happy to help you with this technical queries on Discord
program to sort an array in ascending and descending order by taking input from the user without using temp variable
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main (){
int n;
cout << "enter the no of element in an array" <> n;
vector arr;
int val;
for (int i = 0; i > val;
arr.push_back(val);
}
int size = arr.size();
int start = 0;
int end = arr.size() – 1;
int mid = size/2;
for(int i = start;i<=end;i++){
for (int j = 0; j arr[j+1]){
swap(arr[j] , arr[j+1]);
}
}
for (int k = mid; k<=end; k++)
{
if (arr[k] < arr[k + 1])
{
swap(arr[k], arr[k+1]);
}
}
}
for (int k = 0; k < arr.size(); k++)
{
cout << arr[k] << " ";
}
cout << endl;
}
import java.util.*;
public class Main
{
public static void main (String[]args)
{
int[] arr= {3, 2, 4, 1, 10, 30, 40, 20};
Arrays.sort(arr);
for(int i=0;i=arr.length/2;i–){
System.out.print(arr[i]+” “);
}
}
}
#include
using namespace std;
void shorted(int arr[], int n){
int temp, i , j;
for(int i=0; i<n; i++){
for(int j = i +1 ; j arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void halfarray(int arr[], int n){
int first = n/2;
int last = n-1, temp;
while(first<last){
temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
++first;
–last;
}
}
int main(){
cout<>len;
int arr[len];
cout<>n;
cout<<"enter your array elemant : ";
for(int i = 0 ; i>arr[i];
}
shorted(arr, n);
halfarray(arr,n);
cout<<"Sort First half in Ascending and Second half in descending order in C++ : ";
for(int i = 0 ; i<n; i++){
cout<<arr[i]<<" ";
}
return 0;
}
//PROGRAM TO PRINT FIRST HALF IN ASCENDING AND SECOND HALF IN DESCENDING
#include
using namespace std;
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
int n = sizeof(a)/sizeof(a[0]);
int temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<=n/2;j++)
{
if(a[i]<a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
for(int j=n/2;ja[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
#include
using namespace std;
void ascending(int a[],int n){
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[j]<a[i]){
int temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
void descending(int a[],int n){
for(int i=0;i<n;i++){
for(int j=i+1;ja[i]){
int temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
for(int i=0;i<n;i++){
cout<<a[i]<>n;
int a[n];
for(int i=0;i>a[i];
}
cout<<"Ascending :";
ascending(a,n);
cout<<endl;
cout<<"Descending :";
descending(a,n);
return 0;
}