Given an array of integer numbers, we need to sort this array in a minimum number of steps where in one step we can insert any array element from its position to any other position.
Examples :
Input : arr[] = [2, 3, 5, 1, 4, 7, 6]
Output : 3
We can sort above array in 3 insertion
steps as shown below,
1 before array value 2
4 before array value 5
6 before array value 7
Input : arr[] = {4, 6, 5, 1}
Output : 2
We can solve this problem using dynamic programming. The main thing to observe is that moving an element doesn’t change the relative order of elements other than the element which is being moved. Now consider longest increasing subsequence (LIS) in which equal element are also taken as part of the increasing sequence, now if keep the element of this increasing sequence as it is and move all other elements then it will take the least number of steps because we have taken longest subsequence which does not need to be moved. Finally, the answer will be the size of the array minus the size of the longest increasing subsequence.
As LIS problem can be solved in O(N^2) with O(N) extra space using Dynamic Programming.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int minInsertionStepToSortArray( int arr[], int N)
{
int lis[N];
for ( int i = 0; i < N; i++)
lis[i] = 1;
for ( int i = 1; i < N; i++)
for ( int j = 0; j < i; j++)
if (arr[i] >= arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
int max = 0;
for ( int i = 0; i < N; i++)
if (max < lis[i])
max = lis[i];
return (N - max);
}
int main()
{
int arr[] = {2, 3, 5, 1, 4, 7, 6};
int N = sizeof (arr) / sizeof (arr[0]);
cout << minInsertionStepToSortArray(arr, N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class Main
{
static int minInsertionStepToSortArray( int arr[], int N)
{
int [] lis = new int [N];
for ( int i = 0 ; i < N; i++)
lis[i] = 1 ;
for ( int i = 1 ; i < N; i++)
for ( int j = 0 ; j < i; j++)
if (arr[i] >= arr[j] && lis[i] < lis[j] + 1 )
lis[i] = lis[j] + 1 ;
int max = 0 ;
for ( int i = 0 ; i < N; i++)
if (max < lis[i])
max = lis[i];
return (N - max);
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 , 5 , 1 , 4 , 7 , 6 };
int N = arr.length;
System.out.println(minInsertionStepToSortArray(arr, N));
}
}
|
Python 3
def minInsertionStepToSortArray(arr, N):
lis = [ 0 ] * N
for i in range (N):
lis[i] = 1
for i in range ( 1 , N):
for j in range (i):
if (arr[i] > = arr[j] and
lis[i] < lis[j] + 1 ):
lis[i] = lis[j] + 1
max = 0
for i in range (N):
if ( max < lis[i]):
max = lis[i]
return (N - max )
if __name__ = = "__main__" :
arr = [ 2 , 3 , 5 , 1 , 4 , 7 , 6 ]
N = len (arr)
print (minInsertionStepToSortArray(arr, N))
|
C#
using System;
class GfG {
static int minInsertionStepToSortArray(
int []arr, int N)
{
int [] lis = new int [N];
for ( int i = 0; i < N; i++)
lis[i] = 1;
for ( int i = 1; i < N; i++)
for ( int j = 0; j < i; j++)
if (arr[i] >= arr[j] &&
lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
int max = 0;
for ( int i = 0; i < N; i++)
if (max < lis[i])
max = lis[i];
return (N - max);
}
public static void Main (String[] args)
{
int []arr = {2, 3, 5, 1, 4, 7, 6};
int N = arr.Length;
Console.Write(
minInsertionStepToSortArray(arr, N));
}
}
|
PHP
<?php
function minInsertionStepToSortArray( $arr , $N )
{
$lis [ $N ] = 0;
for ( $i = 0; $i < $N ; $i ++)
$lis [ $i ] = 1;
for ( $i = 1; $i < $N ; $i ++)
for ( $j = 0; $j < $i ; $j ++)
if ( $arr [ $i ] >= $arr [ $j ] &&
$lis [ $i ] < $lis [ $j ] + 1)
$lis [ $i ] = $lis [ $j ] + 1;
$max = 0;
for ( $i = 0; $i < $N ; $i ++)
if ( $max < $lis [ $i ])
$max = $lis [ $i ];
return ( $N - $max );
}
$arr = array (2, 3, 5, 1, 4, 7, 6);
$N = sizeof( $arr ) / sizeof( $arr [0]);
echo minInsertionStepToSortArray( $arr , $N );
?>
|
Javascript
<script>
function minInsertionStepToSortArray(arr,N)
{
let lis = new Array(N);
for (let i = 0; i < N; i++)
{
lis[i] = 1;
}
for (let i = 1; i < N; i++)
{
for (let j = 0; j < i; j++)
{
if (arr[i] >= arr[j] && lis[i] < lis[j] + 1)
{
lis[i] = lis[j] + 1;
}
}
}
let max = 0;
for (let i = 0; i < N; i++)
{
if (max < lis[i])
{
max = lis[i];
}
}
return (N - max);
}
let arr = new Array(2, 3, 5, 1, 4, 7, 6);
let N = arr.length;
document.write(minInsertionStepToSortArray(arr, N));
</script>
|
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Dec, 2022
Like Article
Save Article