We are given an array of n-elements you have to find k smallest elements from the array but they must be in the same order as they are in the given array and we are allowed to use only O(1) extra space.
Examples:
Input : arr[] = {4, 2, 6, 1, 5},
k = 3
Output : 4 2 1
Explanation : 1, 2 and 4 are three smallest
numbers and 4 2 1 is their order in given array
Input : arr[] = {4, 12, 16, 21, 25},
k = 3
Output : 4 12 16
Explanation : 4, 12 and 16 are 3 smallest numbers
and 4 12 16 is their order in given array
We have discussed an efficient solution to find n smallest elements of the above problem by using extra space of O(n). To solve it without using any extra space we will use the concept of insertion sort.
The idea is to move k minimum elements to begin in the same order. To do this, we start from the (k+1)-th element and move to the end. For every array element, we replace the largest element of the first k elements with the current element if the current element is smaller than the largest. To keep the order, we use the insertion sort idea.
The flowchart is as follows:

Flowchart- printsmall
Implementation:
C++
#include <algorithm>
#include <iostream>
using namespace std;
void printSmall( int arr[], int n, int k)
{
for ( int i = k; i < n; ++i) {
int max_var = arr[k - 1];
int pos = k - 1;
for ( int j = k - 2; j >= 0; j--) {
if (arr[j] > max_var) {
max_var = arr[j];
pos = j;
}
}
if (max_var > arr[i]) {
int j = pos;
while (j < k - 1) {
arr[j] = arr[j + 1];
j++;
}
arr[k - 1] = arr[i];
}
}
for ( int i = 0; i < k; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 5;
printSmall(arr, n, k);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
public class GfG {
public static void printSmall( int arr[], int n, int k)
{
for ( int i = k; i < n; ++i) {
int max_var = arr[k - 1 ];
int pos = k - 1 ;
for ( int j = k - 2 ; j >= 0 ; j--) {
if (arr[j] > max_var) {
max_var = arr[j];
pos = j;
}
}
if (max_var > arr[i]) {
int j = pos;
while (j < k - 1 ) {
arr[j] = arr[j + 1 ];
j++;
}
arr[k - 1 ] = arr[i];
}
}
for ( int i = 0 ; i < k; i++)
System.out.print(arr[i] + " " );
}
public static void main(String argc[])
{
int [] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 };
int n = 10 ;
int k = 5 ;
printSmall(arr, n, k);
}
}
|
Python3
def printSmall(arr, n, k):
for i in range (k, n):
max_var = arr[k - 1 ]
pos = k - 1
for j in range (k - 2 , - 1 , - 1 ):
if (arr[j] > max_var):
max_var = arr[j]
pos = j
if (max_var > arr[i]):
j = pos
while (j < k - 1 ):
arr[j] = arr[j + 1 ]
j + = 1
arr[k - 1 ] = arr[i]
for i in range ( 0 , k):
print (arr[i], end = " " )
arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ]
n = len (arr)
k = 5
printSmall(arr, n, k)
|
C#
using System;
public class GfG {
public static void printSmall( int [] arr, int n, int k)
{
for ( int i = k; i < n; ++i) {
int max_var = arr[k - 1];
int pos = k - 1;
for ( int j = k - 2; j >= 0; j--) {
if (arr[j] > max_var) {
max_var = arr[j];
pos = j;
}
}
if (max_var > arr[i]) {
int j = pos;
while (j < k - 1) {
arr[j] = arr[j + 1];
j++;
}
arr[k - 1] = arr[i];
}
}
for ( int i = 0; i < k; i++)
Console.Write(arr[i] + " " );
}
public static void Main()
{
int [] arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = 10;
int k = 5;
printSmall(arr, n, k);
}
}
|
PHP
<?php
function printSmall( $arr , $n , $k )
{
for ( $i = $k ; $i < $n ; ++ $i )
{
$max_var = $arr [ $k - 1];
$pos = $k - 1;
for ( $j = $k - 2; $j >= 0; $j --)
{
if ( $arr [ $j ] > $max_var )
{
$max_var = $arr [ $j ];
$pos = $j ;
}
}
if ( $max_var > $arr [ $i ])
{
$j = $pos ;
while ( $j < $k - 1)
{
$arr [ $j ] = $arr [ $j + 1];
$j ++;
}
$arr [ $k - 1] = $arr [ $i ];
}
}
for ( $i = 0; $i < $k ; $i ++)
echo $arr [ $i ] , " " ;
}
$arr = array (1, 5, 8, 9, 6, 7, 3, 4, 2, 0);
$n = count ( $arr );
$k = 5;
printSmall( $arr , $n , $k );
?>
|
Javascript
<script>
function printSmall(arr, n, k)
{
for (let i = k; i < n; ++i) {
let max_var = arr[k - 1];
let pos = k - 1;
for (let j = k - 2; j >= 0; j--) {
if (arr[j] > max_var) {
max_var = arr[j];
pos = j;
}
}
if (max_var > arr[i]) {
let j = pos;
while (j < k - 1) {
arr[j] = arr[j + 1];
j++;
}
arr[k - 1] = arr[i];
}
}
for (let i = 0; i < k; i++)
document.write(arr[i] + " " );
}
let arr = [ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 ];
let n = 10;
let k = 5;
printSmall(arr, n, k);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)