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:
#include <iostream> using namespace std;
// function to sort list2 according to list1 void sortArrays( char *list1, int *list2, int n) {
// array to store frequency of list2
int freqList2[n] = {};
for ( int i = 0; i < n; i ++) {
freqList2[list2[i]] += 1;
}
// prefix sum calculation
for ( int i = 1; i < n; i ++) {
freqList2[i] += freqList2[i-1];
}
// temporary array to store the sorted list
char temp[n] = {};
for ( int i = n - 1; i >= 0; i --) {
char x = list1[i];
int pos = list2[i];
temp[--freqList2[pos]] = x;
}
// copy the sorted list back to list1
for ( int i = 0; i < n; i ++) {
list1[i] = temp[i];
}
} int main() {
// input
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};
// function call to sort list2 according to list1
sortArrays(list1, list2, n);
for ( int i = 0; i < n; i ++) {
cout << list1[i] << " " ;
}
return 0;
} |
import java.util.Arrays;
public class Solution {
// Function to sort list2 according to list1
static void sortArrays( char [] list1, int [] list2, int n) {
// Array to store frequency of list2
int [] freqList2 = new int [n];
for ( int i = 0 ; i < n; i++) {
freqList2[list2[i]] += 1 ;
}
// Prefix sum calculation
for ( int i = 1 ; i < n; i++) {
freqList2[i] += freqList2[i - 1 ];
}
// Temporary array to store the sorted list
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;
}
// Copy the sorted list back to list1
System.arraycopy(temp, 0 , list1, 0 , n);
}
public static void main(String[] args) {
// Input
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 };
// Function call to sort list2 according to list1
sortArrays(list1, list2, n);
// Output
for ( char c : list1) {
System.out.print(c + " " );
}
}
} // This code is contributed by shivamgupta310570 |
def sortArrays(list1, list2, n):
# Dictionary to store the frequency of elements in list2
freqList2 = {}
# Count the frequency of elements in list2
for i in range (n):
freqList2[list2[i]] = freqList2.get(list2[i], 0 ) + 1
# Calculate prefix sum
for i in range ( 1 , n):
freqList2[i] = freqList2.get(i, 0 ) + freqList2.get(i - 1 , 0 )
# Temporary array to store the sorted list
temp = [''] * n
# Traverse list1 in reverse order
for i in range (n - 1 , - 1 , - 1 ):
x = list1[i]
pos = list2[i]
temp[freqList2[pos] - 1 ] = x
freqList2[pos] - = 1
# Copy the sorted list back to list1
for i in range (n):
list1[i] = temp[i]
# Main function def main():
# Input
n = 9
list1 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ]
list2 = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
# Function call to sort list2 according to list1
sortArrays(list1, list2, n)
# Print the sorted list
for item in list1:
print item,
# Call the main function main() |
using System;
class MainClass {
// function to sort list2 according to list1
static void SortArrays( char [] list1, int [] list2, int n) {
// array to store frequency of list2
int [] freqList2 = new int [n];
for ( int i = 0; i < n; i++) {
freqList2[list2[i]] += 1;
}
// prefix sum calculation
for ( int i = 1; i < n; i++) {
freqList2[i] += freqList2[i - 1];
}
// temporary array to store the sorted list
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;
}
// copy the sorted list back to list1
for ( int i = 0; i < n; i++) {
list1[i] = temp[i];
}
}
public static void Main( string [] args) {
// input
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 };
// function call to sort list2 according to list1
SortArrays(list1, list2, n);
for ( int i = 0; i < n; i++) {
Console.Write(list1[i] + " " );
}
Console.WriteLine();
}
} // This code is contributed by shivamgupta0987654321 |
// Function to sort list2 according to list1 function sortArrays(list1, list2, n) {
// Array to store frequency of list2
let freqList2 = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
freqList2[list2[i]] += 1;
}
// Prefix sum calculation
for (let i = 1; i < n; i++) {
freqList2[i] += freqList2[i - 1];
}
// Temporary array to store the sorted list
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;
}
// Copy the sorted list back to list1
for (let i = 0; i < n; i++) {
list1[i] = temp[i];
}
} // Main function function main() {
// Input
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];
// Function call to sort list2 according to list1
sortArrays(list1, list2, n);
// Output the sorted list
console.log(list1.join( " " ));
} // Call the main function main(); |
a d h b c e i f g
Time Complexity: O(N), where N is the length of list.
Auxiliary Space: O(N)