Murphy is on point. Look for local Japanese "crate engines", longblocks sell for for around a grand or less, get your engine pulled, accessories swapped over and installed. Many crate engine dealers offer installation, too, but more geared to the DIY set. Happy hunting.
Junk yard engine is cheapest, but its always buyer beware on those engines. A rebuilt one from say "Jasper Engines" will be more expensive, but you get a decent warranty. Installation labor will be around 700 dollars or so. This does not include the engine cost.
Get a motor from Japan. Japan has a huge smog problem and most cars wont pass emissions after 50k miles. So they just pull them and drop in a new one. You can buy a guaranteed motor for $1200. I know because I've done it!
Get real , a new engine would be MUCH more expensive than a used engine. You need to make some calls to auto salvage yards to get a few prices on used motors, it will vary by year and mileage. Then a few thousand to install the engine, Expect at least $2,000 - $3,000 .
I just bought a 2005 Mazda3 sedan and it still runs but I need a new engine, how much would it cost all in all to get a new engine put in? New or used engine whatever's cheapest