Sorting all array elements except one
Last Updated :
29 Mar, 2024
Given an array, a positive integer, sort the array in ascending order such that the element at index K in the 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 a given array to another array. Then sort the other array using a sorting algorithm. Finally, again copy the sorted array to the original array. While copying, skip k-th element.
Below is an efficient solution.
- Swap k-th element with the last element.
- Sort all elements except the last.
- For every element from (k+1)-th to last, move them one position ahead.1
- Copy k-th element back to position k.
C++
// 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] << " ";
}
Java
// 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.
Python3
# Python3 program to sort all elements except
# element at index k.
def sortExcept(arr, k, n):
# Move k-th element to end
arr[k], arr[-1] = arr[-1], arr[k]
# Sort all elements except last
arr = sorted(arr, key = lambda i: (i is arr[-1], i))
# Store last element (originally k-th)
last = arr[-1]
# Move all elements from k-th to one
# position ahead.
i = n - 1
while i > k:
arr[i] = arr[i - 1]
i -= 1
# Restore k-th element
arr[k] = last
return arr
# Driver code
if __name__ == '__main__':
a = [10, 4, 11, 7, 6, 20]
k = 2
n = len(a)
a = sortExcept(a, k, n)
print(" ".join(list(map(str, a))))
# This code is contributed by Shivam Singh.
C#
// 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
PHP
<?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)
?>
JavaScript
<script>
// Javascript program to sort all elements except
// element at index k.
function sortExceptK(arr, k, n)
{
// Move k-th element to end
let temp = arr[k];
arr[k] = arr[n-1];
arr[n-1] = temp;
// Sort all elements except last
arr.sort(function(a, b){
return a - b});
// Store last element (originally k-th)
let last = arr[n-1];
// Move all elements from k-th to one
// position ahead.
for (let i = n-1; i > k; i--)
arr[i] = arr[i-1];
// Restore k-th element
arr[k] = last;
// Move k-th element to end
temp = arr[k];
arr[k] = arr[n-1];
arr[n-1] = temp;
return 0;
}
let a = [10, 4, 11, 7, 6, 20 ];
let k = 2;
let n = a.length;
sortExceptK(a, k, n);
for(let i = 0; i < n; i++)
document.write(a[i] + " ");
</script>
Time Complexity: O(n*log(n)) where n is the number of elements.
Auxiliary Space: O(1)
Similar Reads
Sorting array except elements in a subarray Given an array A positive integers, sort the array in ascending order such that element in given subarray (start and end indexes are input) in unsorted array stay unmoved and all other elements are sorted.Examples : Input : arr[] = {10, 4, 11, 7, 6, 20} l = 1, u = 3 Output : arr[] = {6, 4, 11, 7, 10
7 min read
Single Element in a Sorted Array Given a sorted array in which all elements appear twice and one element appears only once, the task is to find the element that appears once.Examples: Input: arr[] = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8}Output: 4Explanation: All numbers except 4 occur twice in the array.Input: arr[] = {1, 1, 3, 3, 4, 4,
10 min read
Remove elements to make array sorted Given an array of integers, the task is to remove elements from the array to make the array sorted. That is, remove the elements which do not follow an increasing order. Examples: Input: arr[] = {1, 2, 4, 3, 5, 7, 8, 6, 9, 10} Output: 1 2 4 5 7 8 9 10 Input: arr[] = {10, 12, 9, 5, 2, 13, 14} Output:
8 min read
Array Sorting - Practice Problems Sorting an array means arranging the elements of the array in a certain order. Generally sorting in an array is done to arrange the elements in increasing or decreasing order. Problem statement: Given an array of integers arr, the task is to sort the array in ascending order and return it, without u
9 min read
Sort an array by left shifting digits of array elements Given an array arr[] consisting of N positive integers, the task is to left-shift the digits of array elements such that the array modifies to a sorted form. If multiple solutions exist, then print any one of them. Otherwise, print -1. Examples: Input: arr[] = { 511, 321, 323, 432 } Output: { 115, 1
8 min read