Sort the values of first list using second list
Last Updated :
07 Mar, 2024
Given two lists, sort the values of one list using the second list.
Examples:
Input: list1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’], list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
Output: [‘a’, ‘d’, ‘h’, ‘b’, ‘c’, ‘e’, ‘i’, ‘f’, ‘g’]
Explanation: Since ‘a’, ‘d’ and ‘h’ have value = 0 in list 2, therefore they occur first, then ‘b’, ‘c’, ‘e’ and ‘i’ have values = 1 so they occur next and at last occurs ‘f’ and ‘g’ which have values = 2.
Input: list1 = [‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’], list2 = [ 0, 1, 1, 0, 1, 2, 2, 2, 0, 1, 1, 0, 1]
Output: [‘g’, ‘k’, ‘g’, ‘k’, ‘e’, ‘e’, ‘s’, ‘e’, ‘e’, ‘s’, ‘f’, ‘o’, ‘r’]
Explanation: Since ‘g’ and ‘k’ have value = 0 in list2, therefore they occur first, then ‘e’ and ‘s’ have value = 1 in list2 so they occur next and at last occurs ‘f’, ‘o’ and ‘r’ which have values = 2.
Approach: To solve the problem, follow the below idea:
The problem can be solved using the same idea as Counting Sort. Maintain a map to store the frequency of list2. After that we can take the prefix sum of the array to get the ending index for every position. We can maintain a new list to store all the sorted elements. So, we traverse the list2 from right to left and find the respective position from list2. Now, we check the prefix sum array to find the index and put the character at that place. Similarly, we can traverse the whole array and finally copy it back to list2.
Step-by-step algorithm:
- Maintain a frequency array for list2, say freqList2[] to store the frequency of positions in list2.
- Now, calculate the prefix sum of all the elements in freqList2[] to get the last position where an element with position = list2[i], will be stored.
- Maintain a temp array to store the sorted list of characters.
- Iterate from i = N-1 to 0, and for every position find the character to be put from list1 and the exact position to be put at from freqList2[].
- Copy the sorted list temp[] back to list1[].
Below is the implementation of the algorithm:
C++
#include <iostream>
using namespace std;
void sortArrays( char *list1, int *list2, int n) {
int freqList2[n] = {};
for ( int i = 0; i < n; i ++) {
freqList2[list2[i]] += 1;
}
for ( int i = 1; i < n; i ++) {
freqList2[i] += freqList2[i-1];
}
char temp[n] = {};
for ( int i = n - 1; i >= 0; i --) {
char x = list1[i];
int pos = list2[i];
temp[--freqList2[pos]] = x;
}
for ( int i = 0; i < n; i ++) {
list1[i] = temp[i];
}
}
int main() {
int n = 9;
char list1[] = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' };
int list2[] = { 0, 1, 1, 0, 1, 2, 2, 0, 1};
sortArrays(list1, list2, n);
for ( int i = 0; i < n; i ++) {
cout << list1[i] << " " ;
}
return 0;
}
|
Java
import java.util.Arrays;
public class Solution {
static void sortArrays( char [] list1, int [] list2, int n) {
int [] freqList2 = new int [n];
for ( int i = 0 ; i < n; i++) {
freqList2[list2[i]] += 1 ;
}
for ( int i = 1 ; i < n; i++) {
freqList2[i] += freqList2[i - 1 ];
}
char [] temp = new char [n];
for ( int i = n - 1 ; i >= 0 ; i--) {
char x = list1[i];
int pos = list2[i];
temp[--freqList2[pos]] = x;
}
System.arraycopy(temp, 0 , list1, 0 , n);
}
public static void main(String[] args) {
int n = 9 ;
char [] list1 = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' };
int [] list2 = { 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 };
sortArrays(list1, list2, n);
for ( char c : list1) {
System.out.print(c + " " );
}
}
}
|
Python
def sortArrays(list1, list2, n):
freqList2 = {}
for i in range (n):
freqList2[list2[i]] = freqList2.get(list2[i], 0 ) + 1
for i in range ( 1 , n):
freqList2[i] = freqList2.get(i, 0 ) + freqList2.get(i - 1 , 0 )
temp = [''] * n
for i in range (n - 1 , - 1 , - 1 ):
x = list1[i]
pos = list2[i]
temp[freqList2[pos] - 1 ] = x
freqList2[pos] - = 1
for i in range (n):
list1[i] = temp[i]
def main():
n = 9
list1 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ]
list2 = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
sortArrays(list1, list2, n)
for item in list1:
print item,
main()
|
C#
using System;
class MainClass {
static void SortArrays( char [] list1, int [] list2, int n) {
int [] freqList2 = new int [n];
for ( int i = 0; i < n; i++) {
freqList2[list2[i]] += 1;
}
for ( int i = 1; i < n; i++) {
freqList2[i] += freqList2[i - 1];
}
char [] temp = new char [n];
for ( int i = n - 1; i >= 0; i--) {
char x = list1[i];
int pos = list2[i];
temp[--freqList2[pos]] = x;
}
for ( int i = 0; i < n; i++) {
list1[i] = temp[i];
}
}
public static void Main( string [] args) {
int n = 9;
char [] list1 = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' };
int [] list2 = { 0, 1, 1, 0, 1, 2, 2, 0, 1 };
SortArrays(list1, list2, n);
for ( int i = 0; i < n; i++) {
Console.Write(list1[i] + " " );
}
Console.WriteLine();
}
}
|
Javascript
function sortArrays(list1, list2, n) {
let freqList2 = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
freqList2[list2[i]] += 1;
}
for (let i = 1; i < n; i++) {
freqList2[i] += freqList2[i - 1];
}
let temp = new Array(n).fill( null );
for (let i = n - 1; i >= 0; i--) {
let x = list1[i];
let pos = list2[i];
temp[--freqList2[pos]] = x;
}
for (let i = 0; i < n; i++) {
list1[i] = temp[i];
}
}
function main() {
let n = 9;
let list1 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ];
let list2 = [0, 1, 1, 0, 1, 2, 2, 0, 1];
sortArrays(list1, list2, n);
console.log(list1.join( " " ));
}
main();
|
Time Complexity: O(N), where N is the length of list.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...