Sorting all array elements except one

Given an array A positive integers, sort the array in ascending order such that element at index K in unsorted array stays unmoved and all other elements are sorted.

Examples:

Input : arr[] = {10, 4, 11, 7, 6, 20}
            k = 2;
Output : arr[] = {4, 6, 11, 7, 10, 20}

Input : arr[] = {30, 20, 10}
         k = 0
Output : arr[] = {30, 10, 20}

A simple solution is to copy all elements except k-th of given array to another array. Then sort the other array using a sorting algorithm. Finally again copy the sorted array to original array. While copying, skip k-th element.



Below is an efficient solution.

  1. Swap k-th element with last element.
  2. Sort all elements except last.
  3. For every element from (k+1)-th to last, move them one position ahead.1
  4. Copy k-th element back to position k.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to sort all elements except
// element at index k.
#include <bits/stdc++.h>
using namespace std;
  
int sortExceptK(int arr[], int k, int n)
{
    // Move k-th element to end
    swap(arr[k], arr[n-1]);
  
    // Sort all elements except last
    sort(arr, arr + n - 1);
  
    // Store last element (originally k-th)
    int last = arr[n-1];
  
    // Move all elements from k-th to one
    // position ahead.
    for (int i=n-1; i>k; i--)
       arr[i] = arr[i-1];
  
    // Restore k-th element
    arr[k] = last;
}
  
// Driver code
int main()
{
    int a[] = {10, 4, 11, 7, 6, 20 };
    int k = 2;
    int n = sizeof(a) / sizeof(a[0]);
    sortExceptK(a, k, n);
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to sort all elements except
// element at index k.
import java.util.Arrays;
  
class GFG {
      
    static int sortExceptK(int arr[], int k, int n)
    {
          
        // Move k-th element to end
        int temp = arr[k];
        arr[k] = arr[n-1];
        arr[n-1] = temp;
      
        // Sort all elements except last
        Arrays.sort(arr, 0, n-1);
      
        // Store last element (originally k-th)
        int last = arr[n-1];
      
        // Move all elements from k-th to one
        // position ahead.
        for (int i = n-1; i > k; i--)
        arr[i] = arr[i-1];
      
        // Restore k-th element
        arr[k] = last;
        return 0;
    }
      
    //Driver code
    public static void main (String[] args)
    {
        int a[] = {10, 4, 11, 7, 6, 20 };
        int k = 2;
        int n = a.length;
          
        sortExceptK(a, k, n);
          
        for (int i = 0; i < n; i++)
            System.out.print(a[i] + " ");
    }
}
  
//This code is contributed by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to sort all elements except
// element at index k.
using System;
  
public class GFG {
      
    static int sortExceptK(int[] arr, int k, int n)
    {
        // Move k-th element to end
        int temp = arr[k];
        arr[k] = arr[n - 1];
        arr[n - 1] = temp;
  
        // Sort all elements except last
        Array.Sort(arr, 0, n - 1);
  
        // Store last element (originally k-th)
        int last = arr[n - 1];
  
        // Move all elements from k-th to one
        // position ahead.
        for (int i = n - 1; i > k; i--)
            arr[i] = arr[i - 1];
  
        // Restore k-th element
        arr[k] = last;
          
        return 0;
    }
      
    // Driver code
    public static void Main()
    {
        int[] a = { 10, 4, 11, 7, 6, 20 };
        int k = 2;
        int n = a.Length;
  
        sortExceptK(a, k, n);
  
        for (int i = 0; i < n; i++)
        Console.Write(a[i] + " ");
    }
}
  
// This article is contributed by shiv_bhakt

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to sort all 
// elements except element
// at index k.
function sortExceptK(&$arr, $k, $n)
{
    // Move k-th element to end
    $t = $arr[$k];
    $arr[$k] = $arr[$n - 1];
    $arr[$n - 1] = $t;
  
    // Sort all elements 
    // except last
    $t = $arr[count($arr) - 1];
    $arr = array_slice($arr, 0, -1);
    sort($arr);
    array_push($arr, $t);
  
    // Store last element
    // (originally k-th)
    $last = $arr[$n - 1];
  
    // Move all elements from 
    // k-th to one position ahead.
    for ($i = $n - 1; $i > $k; $i--)
    $arr[$i] = $arr[$i - 1];
  
    // Restore k-th element
    $arr[$k] = $last;
}
  
// Driver code
$a = array(10, 4, 11, 
            7, 6, 20 );
$k = 2;
$n = count($a);
sortExceptK($a, $k, $n);
  
for ($i = 0; $i < $n; $i++)
    echo ($a[$i]." ");
      
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

chevron_right


Output:

4 6 11 7 10 20 


My Personal Notes arrow_drop_up

Sometimes you never realize the value of a moment until it becomes a memory

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.



Improved By : manishshaw1



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.