# Sort half array in ascending and half in descending

Here given code implementation process.

``````import java.util.Arrays;
// Java program
// Sort half array in ascending and half in descending
public class Sorting
{
// Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print("  " + arr[i]);
}
System.out.print("\n");
}
public void sortElement(int[] arr, int n)
{
// Sort first half
Arrays.sort(arr, 0, n / 2);
// Sort second half
Arrays.sort(arr, n / 2, n);
int temp = 0;
int i = n / 2;
int j = n - 1;
// Reverse the elements of second half
while (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
public static void main(String[] args)
{
// Define array elements
int[] arr1 = {
1 , 8 , 2 , 5 , 17 , 7 , 9 , 4 , 3 , 11
};
int[] arr2 = {
7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31
};
// Test A
int n = arr1.length;
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
n = arr2.length;
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]-> [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31] -> [31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
}
}``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````
``````// Include header file
#include <iostream>
#include <algorithm>

using namespace std;
// C++ program
// Sort half array in ascending and half in descending
class Sorting
{
public:
// Function which is display array elements
void display(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
void sortElement(int arr[], int n)
{
// Sort first half
sort(arr, arr + (n/2));
// Sort second half
sort(arr + (n/2), arr + n);
int temp = 0;
int i = n / 2;
int j = n - 1;
// Reverse the elements of second half
while (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
};
int main()
{
// Define array elements
int arr1[] = {
1 , 8 , 2 , 5 , 17 , 7 , 9 , 4 , 3 , 11
};
int arr2[] = {
7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31
};
// Test A
int n = sizeof(arr1) / sizeof(arr1[0]);
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11].[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
n = sizeof(arr2) / sizeof(arr2[0]);
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]. [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31].[31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
return 0;
}``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````
``````package main
import "sort"
import "fmt"
// Go program
// Sort half array in ascending and half in descending
type Sorting struct {}
func getSorting() * Sorting {
var me *Sorting = &Sorting {}
return me
}
// Function which is display array elements
func(this Sorting) display(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print("  ", arr[i])
}
fmt.Print("\n")
}
func(this Sorting) sortElement(arr[] int, n int) {

a := arr[0 : (n / 2)]
b := arr[(n / 2): n]
temp := 0
// Sort first half
sort.Ints(a)
// Sort second half
sort.Ints(b)

// Reverse the elements of second half
for i := 0; i < len(b) / 2; i++ {
temp = b[i]
b[i] = b[len(b)-1-i]
b[len(b)-1-i] = temp
}

for i := 0; i < len(a); i++ {

arr[i] = a[i]
}
for i := 0; i < len(b); i++ {

arr[len(a) + i] = b[i]
}
}
func main() {
var task * Sorting = getSorting()
// Define array elements
var arr1 = [] int {
1,
8,
2,
5,
17,
7,
9,
4,
3,
11,
}
var arr2 = [] int {
7,
1,
8,
3,
12,
13,
-3,
6,
31,
}
// Test A
var n int = len(arr1)
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
n = len(arr2)
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]-> [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31]->[31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
}``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````
``````// Include namespace system
using System;
// Csharp program
// Sort half array in ascending and half in descending
public class Sorting
{
// Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
public void sortElement(int[] arr, int n)
{
// Sort first half
Array.Sort(arr,0, n / 2);
// Sort second half
Array.Sort(arr,(n / 2), n-(n / 2));
int temp = 0;
int i = n / 2;
int j = n - 1;
// Reverse the elements of second half
while (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
public static void Main(String[] args)
{
// Define array elements
int[] arr1 = {
1 , 8 , 2 , 5 , 17 , 7 , 9 , 4 , 3 , 11
};
int[] arr2 = {
7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31
};
// Test A
int n = arr1.Length;
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
n = arr2.Length;
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]-> [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31] -> [31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
}
}``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````
``````<?php
// Php program
// Sort half array in ascending and half in descending
class Sorting
{
// Function which is display array elements
public  function display(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo("  ".\$arr[\$i]);
}
echo("\n");
}
public  function sortElement(&\$arr, \$n)
{
if(\$n <= 1)
{
return;
}
\$a = array_slice(\$arr,0,(int)(\$n / 2));
\$b = array_slice(\$arr,(int)(\$n / 2),\$n - (int)(\$n / 2));

// Sort first half
sort(\$a);
// Sort second half
rsort(\$b);
for (\$index = 0; \$index < count(\$a) ; \$index++) {
\$arr[\$index] = \$a[\$index];
}
for (\$index = 0 ; \$index < count(\$b) ; \$index++) {
\$arr[count(\$a) + \$index] = \$b[\$index];
}
}
}

function main()
{
// Define array elements
\$arr1 = array(1, 8, 2, 5, 17, 7, 9, 4, 3, 11);
\$arr2 = array(7, 1, 8, 3, 12, 13, -3, 6, 31);
// Test A
\$n = count(\$arr1);
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
\$n = count(\$arr2);
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]-> [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31]->[31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
}
main();``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````
``````// Node JS program
// Sort half array in ascending and half in descending
class Sorting
{
// Function which is display array elements
display(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
sortElement(arr, n)
{
if(n <= 0)
{
return ;
}
var h1 = arr.slice(0,parseInt(n/2));
var h2 = arr.slice(parseInt(n/2),n);
// Sort first half
h1.sort(function(a, b)
{
return a - b;
});
// Sort second half
h2.sort(function(a, b)
{
return a - b;
});

h2 = h2.reverse()  ;

for( var i = 0; i < h1.length; ++i)
{
arr[i] = h1[i];
}
for( var i = 0; i < h2.length; ++i)
{
arr[h1.length + i] = h2[i];
}
}
}

function main()
{
// Define array elements
var arr1 = [1, 8, 2, 5, 17, 7, 9, 4, 3, 11];
var arr2 = [7, 1, 8, 3, 12, 13, -3, 6, 31];
// Test A
var n = arr1.length;
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
n = arr2.length;
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]-> [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31]->[31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
}
main();``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````
``````#  Python 3 program
#  Sort half array in ascending and half in descending
class Sorting :
#  Function which is display list elements
def display(self, arr, n) :
i = 0
while (i < n) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

def reverseData(self,arr) :
return arr.reverse()

def sortElement(self, arr, n) :
if(n <= 0) :
return

# Sort first and second half
data = (sorted(arr[0:int(n/2)])) + sorted(arr[int(n/2):n]);
i = 0
j = n - 1
# Collect element
while(i < n) :
if(i < n//2) :
arr[i] = data[i]
else :
arr[i] = data[j]
j -= 1
i+=1

def main() :
#  Define list elements
arr1 = [1, 8, 2, 5, 17, 7, 9, 4, 3, 11]
arr2 = [7, 1, 8, 3, 12, 13, -3, 6, 31]
#  Test A
n = len(arr1)
#  arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
#  first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
#  second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
#  Result =  [ 1  2  5  8  17  11  9  7  4  3]
#  Test B
n = len(arr2)
#  arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
#  first half [7 , 1 , 8 , 3]-> [1  3  7  8]
#  second half [12 , 13 , -3 , 6 , 31] -> [31  13  12  6  -3]
#  result = [1  3  7  8  31  13  12  6  -3]

if __name__ == "__main__": main()``````

#### Output

``````   1   2   5   8   17   11   9   7   4   3
1   3   7   8   31   13   12   6   -3``````
``````#  Ruby program
#  Sort half array in ascending and half in descending
class Sorting
def sortElement(arr, n)
if(n <= 0)
return
end
# Sort first half
# Sort second half and reverse
temp = arr[0..(n/2 -1)].sort! + (arr[(n/2)..(n)].sort!).reverse
i = 0
while(i < n)
arr[i] = temp[i]
i += 1
end
end
end

def main()
#  Define array elements
arr1 = [1, 8, 2, 5, 17, 7, 9, 4, 3, 11]
arr2 = [7, 1, 8, 3, 12, 13, -3, 6, 31]
#  Test A
n = arr1.length
#  arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
#  first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
#  second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
#  Result =  [ 1  2  5  8  17  11  9  7  4  3]
print(arr1,"\n")
#  Test B
n = arr2.length
#  arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
#  first half [7 , 1 , 8 , 3]-> [1  3  7  8]
#  second half [12 , 13 , -3 , 6 , 31] -> [31  13  12  6  -3]
#  result = [1  3  7  8  31  13  12  6  -3]
print(arr2,"\n")
end

main()``````

#### Output

``````[1, 2, 5, 8, 17, 11, 9, 7, 4, 3]
[1, 3, 7, 8, 31, 13, 12, 6, -3]
``````
``````import scala.collection.mutable._;
// Scala program
// Sort half array in ascending and half in descending
class Sorting()
{
// Function which is display array elements
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
def sortElement(arr: Array[Int], n: Int): Unit = {
if (n <= 0)
{
return;
}
var a = arr.slice(0, n/2);
var b = arr.slice(n/2, n);
// Sort first half
a = a.sorted;
// Sort second half
b = b.sorted(Ordering.Int.reverse);

var i: Int = 0;
var j: Int = 0;

while(i < n)
{
if(i < n/2)
{
arr(i) = a(i);
}
else{
arr(i) = b(j);
j += 1;
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Sorting = new Sorting();
// Define array elements
var arr1: Array[Int] = Array(1, 8, 2, 5, 17, 7, 9, 4, 3, 11);
var arr2: Array[Int] = Array(7, 1, 8, 3, 12, 13, -3, 6, 31);
// Test A
var n: Int = arr1.length;
// arr = [1 , 8 , 2 , 5 ,17, 7 , 9 , 4 , 3 , 11]
// first half  [1 , 8 , 2 , 5 ,17] ->[1  2  5  8  17]
// second half [7 , 9 , 4 , 3 , 11]->[11  9  7  4  3]
// Result =  [ 1  2  5  8  17  11  9  7  4  3]
// Test B
n = arr2.length;
// arr = [7 , 1 , 8 , 3 , 12 , 13 , -3 , 6 , 31]
// first half [7 , 1 , 8 , 3]-> [1  3  7  8]
// second half [12 , 13 , -3 , 6 , 31] -> [31  13  12  6  -3]
// result = [1  3  7  8  31  13  12  6  -3]
}
}``````

#### Output

``````  1  2  5  8  17  11  9  7  4  3
1  3  7  8  31  13  12  6  -3``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.