# Sort Array such that smallest is at 0th index and next smallest it at last index and so on

Given an array, arr[] of N integers, the task is to rearrange the array elements such that the smallest element is at the 0th position, the second smallest element is at the (N-1)th position, the third smallest element at 1st position, 4th smallest element is at the (N-2)th position, and so on for all integers in arr[].

Examples:

Input: arr[] = {10, 23, 12, 17, 9}
Output: 9 12 23 17 10
Explanation:
The smallest element is 9 which is put in the index 0.
Then the second smallest element is 10 which is put in the last position.
The third smallest element is put in the second position from the start.
The fourth smallest in the second position from the last and so on.
Input: arr[] = {1, 3, 3, 4, 5}
Output: 1 3 5 4 3

Approach: We will be using the Two Pointer Technique. The idea is to iterate from the start marked by variable i to end marked by variable j of the array alternatively until they meet in the middle and keep updating the minimum values at these indices. Below are the steps:

1. Set the element at index i as the minimum value. Iterate over [i, j] and compare each value in this range with arr[i], and if the value in the range is less than arr[i]  then swap the value between arr[i] and current element. Increment the value of i.
2. Set the element at index j as the minimum value. Iterate over [i, j] and compare each value in this range with arr[j], and if the value in the range is less than arr[j]  then swap the value between arr[j] and the current element. Decrement the value of j.
3. Place the smallest element in the ith position at the first iteration and the next smallest element in the jth position. Do this until both i and j points to the same position.

Below is the implementation of the above approach:

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std;`   `// Function to perform the rearrangement ` `void` `rearrange(``int` `a[], ``int` `N) ` `{ ` `    `  `    ``// Initialize variables ` `    ``int` `i = 0, j = N - 1; ` `    ``int` `min = 0, k, x = 0, temp; `   `    ``// Loop until i crosses j ` `    ``while` `(i < j) ` `    ``{ `   `        ``// This check is to find the ` `        ``// minimum values in the ` `        ``// ascending order ` `        ``for``(k = i; k <= j; k++) ` `        ``{ ` `            ``if` `(a[k] < a[min]) ` `                ``min = k; ` `        ``} `   `        ``// Condition to alternatively ` `        ``// iterate variable i and j ` `        ``if` `(x % 2 == 0)` `        ``{ `   `            ``// Perform swap operation ` `            ``temp = a[i]; ` `            ``a[i] = a[min]; ` `            ``a[min] = temp; `   `            ``// Increment i ` `            ``i++; `   `            ``// Assign the value of min ` `            ``min = i; ` `        ``} ` `        ``else` `        ``{ `   `            ``// Perform swap ` `            ``temp = a[j]; ` `            ``a[j] = a[min]; ` `            ``a[min] = temp; `   `            ``// Decrement i ` `            ``j--; `   `            ``// Assign the value of min ` `            ``min = j; ` `        ``} ` `        ``x++; ` `    ``} `   `    ``// Print the array ` `    ``for``(i = 0; i < N; i++) ` `        ``cout << a[i] << ``" "``; ` `}`   `// Driver Code` `int` `main()` `{` `    `  `    ``// Given Array arr[] ` `    ``int` `arr[] = { 1, 3, 3, 4, 5 }; ` `    `  `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    `  `    ``// Function call ` `    ``rearrange(arr, N);`   `    ``return` `0;` `}`   `// This code is contributed by divyeshrabadiya07`

 `// Java program for the above approach ` `import` `java.io.*; ` `import` `java.util.*; `   `public` `class` `GFG { `   `    ``// Function to perform the rearrangement ` `    ``static` `void` `rearrange(``int``[] a) ` `    ``{ ` `        ``// Initialize variables ` `        ``int` `i = ``0``, j = a.length - ``1``; ` `        ``int` `min = ``0``, k, x = ``0``, temp; `   `        ``// Loop until i crosses j ` `        ``while` `(i < j) { `   `            ``// This check is to find the ` `            ``// minimum values in the ` `            ``// ascending order `   `            ``for` `(k = i; k <= j; k++) { ` `                ``if` `(a[k] < a[min]) ` `                    ``min = k; ` `            ``} `   `            ``// Condition to alternatively ` `            ``// iterate variable i and j ` `            ``if` `(x % ``2` `== ``0``) { `   `                ``// Perform swap operation ` `                ``temp = a[i]; ` `                ``a[i] = a[min]; ` `                ``a[min] = temp; `   `                ``// Increment i ` `                ``i++; `   `                ``// Assign the value of min ` `                ``min = i; ` `            ``} ` `            ``else` `{ `   `                ``// Perform swap ` `                ``temp = a[j]; ` `                ``a[j] = a[min]; ` `                ``a[min] = temp; `   `                ``// Decrement i ` `                ``j--; `   `                ``// Assign the value of min ` `                ``min = j; ` `            ``} ` `            ``x++; ` `        ``} `   `        ``// Print the array ` `        ``for` `(i = ``0``; i < a.length; i++) ` `            ``System.out.print(a[i] + ``" "``); ` `    ``} `   `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Given Array arr[] ` `        ``int` `arr[] = { ``1``, ``3``, ``3``, ``4``, ``5` `}; `   `        ``// Function Call ` `        ``rearrange(arr); ` `    ``} ` `}`

 `# Python3 program for ` `# the above approach ` `# Function to perform ` `# the rearrangement ` `def` `rearrange(a, N):`   `    ``# Initialize variables ` `    ``i ``=` `0` `    ``j ``=` `N ``-` `1` `    ``min` `=` `0` `    ``x ``=` `0`   `    ``# Loop until i crosses j ` `    ``while` `(i < j):` `    `  `        ``# This check is to find the ` `        ``# minimum values in the ` `        ``# ascending order ` `        ``for` `k ``in` `range` `(i, j ``+` `1``):` `            ``if` `(a[k] < a[``min``]):` `                ``min` `=` `k`   `        ``# Condition to alternatively ` `        ``# iterate variable i and j ` `        ``if` `(x ``%` `2` `=``=` `0``):` `        `  `            ``# Perform swap operation ` `            ``temp ``=` `a[i]` `            ``a[i] ``=` `a[``min``]` `            ``a[``min``] ``=` `temp`   `            ``# Increment i ` `            ``i ``+``=` `1`   `            ``# Assign the value of min ` `            ``min` `=` `i` `        `  `        ``else``:` `        `  `            ``# Perform swap ` `            ``temp ``=` `a[j]` `            ``a[j] ``=` `a[``min``]` `            ``a[``min``] ``=` `temp`   `            ``# Decrement i ` `            ``j ``-``=` `1`   `            ``# Assign the value of min ` `            ``min` `=` `j` `        `  `        ``x ``+``=` `1` `    `  `    ``# Print the array ` `    ``for` `i ``in` `range` `(N):` `        ``print` `(a[i] ,end ``=` `" "``)`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `  `  `    ``# Given Array arr[] ` `    ``arr ``=` `[``1``, ``3``, ``3``, ``4``, ``5``] ` `    `  `    ``N ``=` `len``(arr)` `    `  `    ``# Function call ` `    ``rearrange(arr, N)` `    `  `# This code is contributed by Chitranayal`

 `// C# program for the above approach` `using` `System;`   `class` `GFG{`   `// Function to perform the rearrangement` `static` `void` `rearrange(``int``[] a)` `{` `    `  `    ``// Initialize variables` `    ``int` `i = 0, j = a.Length - 1;` `    ``int` `min = 0, k, x = 0, temp;`   `    ``// Loop until i crosses j` `    ``while` `(i < j)` `    ``{` `        `  `        ``// This check is to find the` `        ``// minimum values in the` `        ``// ascending order`   `        ``for``(k = i; k <= j; k++)` `        ``{` `            ``if` `(a[k] < a[min])` `                ``min = k;` `        ``}`   `        ``// Condition to alternatively` `        ``// iterate variable i and j` `        ``if` `(x % 2 == 0) ` `        ``{` `            `  `            ``// Perform swap operation` `            ``temp = a[i];` `            ``a[i] = a[min];` `            ``a[min] = temp;`   `            ``// Increment i` `            ``i++;`   `            ``// Assign the value of min` `            ``min = i;` `        ``}` `        ``else` `        ``{` `            `  `            ``// Perform swap` `            ``temp = a[j];` `            ``a[j] = a[min];` `            ``a[min] = temp;`   `            ``// Decrement i` `            ``j--;`   `            ``// Assign the value of min` `            ``min = j;` `        ``}` `        ``x++;` `    ``}` `    `  `    ``// Print the array` `    ``for``(i = 0; i < a.Length; i++)` `        ``Console.Write(a[i] + ``" "``);` `}`   `// Driver Code` `public` `static` `void` `Main(``string``[] args)` `{` `    `  `    ``// Given array arr[]` `    ``int` `[]arr = { 1, 3, 3, 4, 5 };`   `    ``// Function call` `    ``rearrange(arr);` `}` `}`   `// This code is contributed by rutvik_56`

Output:
```1 3 5 4 3

```

Time Complexity: O(N2)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :