New Zealand
Under Section 150 of the New Zealand Crimes Act 1961, it is an offence for there to be "misconduct in respect to human remains". Subsection (b) elaborates that this applies if someone "improperly or indecently interferes with or offers indignity to any dead human body or human remains, whether buried or not". This statute is therefore applicable to sex with corpses and carries a potential two-year prison sentence, although there is no relevant case law. South Africa
Section 14 of the Criminal Law (Sexual Offences and Related Matters) Amendment Act, 2007 prohibits the commission of a sexual act with a corpse. Until codified by the act it was a common law offence. Sweden
Section 16, § 10 of the Swedish Penal Code criminalises necrophilia, but it is not explicitly mentioned. Necrophilia falls under the regulations against abusing a corpse or grave (Brott mot griftefrid), which carries a maximum sentence of two years in prison. One person has been convicted of necrophilia. He was sentenced to psychiatric care for that and other crimes, including arson. United Kingdom
Sexual penetration of a corpse was made illegal under the Sexual Offences Act 2003, carrying a maximum sentence of two years imprisonment. Prior to 2003, necrophilia was not illegal; however, exposing a naked corpse in public was classed as a public nuisance (R v. Clark [1883] 15 Cox 171). United States
There is no federal legislation specifically barring sex with a corpse.
When his attempts failed, and his male victim died, he would keep the corpse until it decomposed beyond recognition,  masturbating and performing sexual intercourse on the body. He would perform sexual activities before and after murdering his victims. Dahmer explained that he only killed his victims because he did not want them to leave. More modern necrophiliacs include Scottish serial killer Dennis Nilsen, and English David Fuller, who is considered the worst offender of this kind in English legal history.Havelock Ellis, in his 1903 volume of Studies of the Psychology of Sex, believed that necrophilia was related to algolagnia, in that both involve the transformation of a supposed negative emotion, such as anger, fear, disgust, or grief, into sexual desire. Classification
In the Diagnostic and Statistical Manual of Mental Disorders, fifth edition (DSM-5), recurrent, intense sexual interest in corpses can be diagnosed under Other Specified Paraphilic Disorder (necrophilia) when it causes marked distress or impairment in important areas of functioning.Forensic Psychologist Anil Aggrawal introduced a ten tier classification of necrophiliacs based on increasing severity of disorder. Additionally, criminologist Lee Mellors typology of homicidal necrophiliacs consists of eight categories (A–H) and is based on the combination of two behavioural axes: destructive (offender mutilates the corpse for sexual reasons) – preservative (offender does not), and cold (offender used the corpse sexually two hours or more after death) – warm (offender used the corpse sexually less than two hours after death). This renders four categories (A-D) to which Mellor adds an additional four (E-H):

Category A (cold/destructive), e.g.