Java Program for Sorting all array elements except one
Last Updated :
23 Mar, 2023
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.
Java
// Java code for the approach
import java.util.Arrays;
public class GFG {
// Driver code
public static void main(String[] args) {
int[] arr = {10, 4, 11, 7, 6, 20 };
int k = 2;
int n = arr.length;
// Function Call
sortExceptK(arr, k);
// Print final array
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Function to sort an array in ascending order
// except for the element at index k
public static void sortExceptK(int[] arr, int k) {
int[] temp = new int[arr.length - 1];
int index = 0;
// Copy all elements except k-th to temp array
for (int i = 0; i < arr.length; i++) {
if (i != k) {
temp[index++] = arr[i];
}
}
// Sort the temp array
Arrays.sort(temp);
// Copy the sorted array back to original array
index = 0;
for (int i = 0; i < arr.length; i++) {
if (i != k) {
arr[i] = temp[index++];
}
}
}
}
Time Complexity: O(n*log2n) as sorting takes n*log2n time.
Space Complexity: O(n) as temp array has been created.
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.
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.
Time Complexity: O(n*log(n)) where n is the number of elements.
Auxiliary Space: O(1)
Please refer complete article on Sorting all array elements except one for more details!
Similar Reads
Java Program to Sort an ArrayList ArrayList is the class provided in the Collection framework. In Java, the collection framework is defined in java.util package. ArrayList is used to dynamically stores the elements. It is more flexible than an array because there is no size limit in ArrayList. ArrayList stores the data in an unorder
6 min read
Java Program for Pancake sorting Write a Java program for a given unsorted array, the task is to sort the given array. You are allowed to do only the following operation on the array. flip(arr, i): Reverse array from 0 to i.Examples: Input: arr[] = { 23, 10, 20, 11, 12, 6, 7 }Output: { 6, 7, 10, 11, 12, 20, 23} Input: arr[] = { 0,
5 min read
Remove all elements from the ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove all elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 3, 4] Output: ArrayList = [] Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: ArrayList = [] Using clear() method: Syntax: collection_name.clear
2 min read
Remove all elements from the ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove all elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 3, 4] Output: ArrayList = [] Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: ArrayList = [] Using clear() method: Syntax: collection_name.clear
2 min read
Sort an Array in Java using Comparator A Comparator is an object that can be used to compare two objects and determine their order. We can use a Comparator to sort a list of objects in any order we can choose, not just in ascending order.Examples:Array(Ascending Order): Input: arr = (4, 2, 5, 1, 3) Output: [1, 2, 3, 4, 5] Array(Descendin
4 min read
Sort elements by frequency | Set 5 (using Java Map) Given an integer array, sort the array according to the frequency of elements in decreasing order, if the frequency of two elements are same then sort in increasing order Examples: Input: arr[] = {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12} Output: 3 3 3 3 2 2 2 12 12 4 5 Explanation : No. Freq 2 : 3 3 : 4 4
3 min read