1/24/2024
Relative Sort Array
This is my solution for the Relative Sort Array problem.
If you know the selection sort, it should be easy. Basically, you do selection sort twice. The first time, you do it to sort arr1 in the order dictated by arr2. Then, if some elements haven't been sorted, you do selection sort again, but this time, you sort it in ascending order.
To do selection sort on the first portion, you go over each item in the arr2 and find which items in arr1 that are equal to it, then put them one by one back to the arr1. How can you put them back?
Basically, you keep a pointer that initially points to the first element of the arr1; when you find an element that needs to be put back, you swap the element that the pointer currently points to with that element and move the pointer to the next item (similar to what you do in selection sort).