Hi There,
To troubleshoot your following issue, to detect any malware or unwanted software on your computer you can use a reputed malware remover software like malwarebytes. If no results are found in terms of any suspicuious software, you still have an option.
Reinstalling macOS. (Which will help remove any stubborn unwanted search engines taking over safari)
Reinstalling macOS necessary does not mean erasing all of your data and you can try reinstalling macOS without erasing your data for this issue although it is recommended that you do try a fresh install of macOS (because that would positively remove any suspicious software and adware) after making a backup* of what's important to you.
You can follow this article to reinstall macOS --> https://support.apple.com/en-us/HT204904
You should remember to learn more about how to erase your disk from the provided article if you're willing to fresh reinstall macOS (which is suggested in this case)
*You can make a backup of all your important files, photos and documents with the help of iCloud or with an external drive you can make an entire backup of your mac with the help of time machine (You can easily restore your time machine backup when you're setting up macOS after a fresh reinstall)
For help with backup methods, you can assist yourself with this article --> https://support.apple.com/mac-backup