Given an array, the task is to create a new sorted array in ascending order from the elements of the given array.
Examples:
Input : arr[] = {2, 5, 4, 9, 8}
Output : 2 4 5 8 9
Input : arr[] = {10, 45, 98, 35, 45}
Output : 10 35 45 45 98
The above problem can be solved efficiently using Binary Search. We create a new array and insert the first element if it’s empty. Now for every new element, we find the correct position for the element in the new array using binary search and then insert that element at the corresponding index in the new array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void createSorted( int a[], int n)
{
vector< int > b;
for ( int j = 0; j < n; j++) {
if (b.empty())
b.push_back(a[j]);
else {
int start = 0, end = b.size() - 1;
int pos = 0;
while (start <= end) {
int mid = start + (end - start) / 2;
if (b[mid] == a[j]) {
b.emplace(b.begin() + max(0, mid + 1), a[j]);
break ;
}
else if (b[mid] > a[j])
pos = end = mid - 1;
else
pos = start = mid + 1;
if (start > end) {
pos = start;
b.emplace(b.begin() + max(0, pos), a[j]);
break ;
}
}
}
}
for ( int i = 0; i < n; i++)
cout << b[i] << " " ;
}
int main()
{
int a[] = { 2, 5, 4, 9, 8 };
int n = sizeof (a) / sizeof (a[0]);
createSorted(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void createSorted( int a[], int n)
{
Vector<Integer> b = new Vector<>();
for ( int j = 0 ; j < n; j++)
{
if (b.isEmpty())
b.add(a[j]);
else
{
int start = 0 , end = b.size() - 1 ;
int pos = 0 ;
while (start <= end)
{
int mid = start + (end - start) / 2 ;
if (b.get(mid) == a[j])
{
b.add((Math.max( 0 , mid + 1 )), a[j]);
break ;
}
else if (b.get(mid) > a[j])
pos = end = mid - 1 ;
else
pos = start = mid + 1 ;
if (start > end)
{
pos = start;
b.add(Math.max( 0 , pos), a[j]);
break ;
}
}
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(b.get(i) + " " );
}
public static void main(String args[])
{
int a[] = { 2 , 5 , 4 , 9 , 8 };
int n = a.length;
createSorted(a, n);
}
}
|
Python3
def createSorted(a: list , n: int ):
b = []
for j in range (n):
if len (b) = = 0 :
b.append(a[j])
else :
start = 0
end = len (b) - 1
pos = 0
while start < = end:
mid = start + (end - start) / / 2
if b[mid] = = a[j]:
b.insert( max ( 0 , mid + 1 ), a[j])
break
elif b[mid] > a[j]:
pos = end = mid - 1
else :
pos = start = mid + 1
if start > end:
pos = start
b.insert( max ( 0 , pos), a[j])
break
for i in range (n):
print (b[i], end = " " )
if __name__ = = "__main__" :
a = [ 2 , 5 , 4 , 9 , 8 ]
n = len (a)
createSorted(a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void createSorted( int []a, int n)
{
List< int > b = new List< int >();
for ( int j = 0; j < n; j++)
{
if (b.Count == 0)
b.Add(a[j]);
else
{
int start = 0, end = b.Count - 1;
int pos = 0;
while (start <= end)
{
int mid = start + (end - start) / 2;
if (b[mid] == a[j])
{
b.Insert((Math.Max(0, mid + 1)), a[j]);
break ;
}
else if (b[mid] > a[j])
pos = end = mid - 1;
else
pos = start = mid + 1;
if (start > end)
{
pos = start;
b.Insert(Math.Max(0, pos), a[j]);
break ;
}
}
}
}
for ( int i = 0; i < n; i++)
Console.Write(b[i] + " " );
}
public static void Main(String []args)
{
int []a = { 2, 5, 4, 9, 8 };
int n = a.Length;
createSorted(a, n);
}
}
|
Javascript
<script>
function createSorted(a, n) {
var b = [];
for ( var j = 0; j < n; j++) {
if (b.length == 0) b.push(a[j]);
else {
var start = 0,
end = b.length - 1;
var pos = 0;
while (start <= end) {
var mid = start + parseInt((end - start) / 2);
if (b[mid] === a[j]) {
b.insert(Math.max(0, mid + 1), a[j]);
break ;
}
else if (b[mid] > a[j])
pos = end = mid - 1;
else pos = start = mid + 1;
if (start > end) {
pos = start;
b.insert(Math.max(0, pos), a[j]);
break ;
}
}
}
}
for ( var i = 0; i < n; i++) document.write(b[i] + " " );
}
Array.prototype.insert = function (index, item) {
this .splice(index, 0, item);
};
var a = [2, 5, 4, 9, 8];
var n = a.length;
createSorted(a, n);
</script>
|
Time Complexity: O(N*N). Although binary search is being used, the list insert calls run in O(N) time on average
Auxiliary Space: O(N)