* PPP, which led last government, could face major setback
* Coalition expected, unstable government possible
* Pakistan's first transition between civilian governments (Recasts with Sharif ahead)
Pakistan's Nawaz Sharif, seeking to become prime minister for a third time, appeared way ahead of his rivals in Saturday's general election, according to a partial count of votes cast.
The election, in which 86 million people were eligible to vote, will bring the first transition between civilian governments in a country that has been ruled by the military for more than half of its turbulent history.
Despite pre-election violence and attacks on Saturday which killed at least 17 people, millions turned out to cast their ballot.
The partial count showed that, so far, Sharif's Pakistan Muslim League - Nawaz (PML-N) was almost certain to win 40 of the 272 National Assembly seats that were contested.
"Let the election results come and you will see that we will have enough votes to form the government with a simple majority. It's already quite clear," said Rana Sanaullah, a PML-N leader.
Whoever wins the vote will have to contend with Taliban militancy, endemic corruption, chronic power cuts and crumbling infrastructure in the nuclear-armed country of 180 million people. One of the first likely tasks will be to negotiate with the International Monetary Fund for a multi-billion-dollar bailout.
Sharif's strong performance appeared to have dealt a blow to the Pakistan People's Party (PPP), which led the government for the last five years. Nearly five hours after polling stations closed, it could count on only 20 seats going in its favour.
The party of cricketer-turned-politician Imran Khan, whose popularity among urban youth has broken a tradition of dominance by the PML-N and PPP, appeared to be close behind in third place.
Khan, Pakistan's best-known sportsman who led a playboy lifestyle in his younger days, is seen by many as a refreshing change from the dynastic politicians who long relied on a patronage system to win votes.
His Tehrik-i-Insaf (PTI) has become a political force because of his calls for an end to corruption, a new political landscape and a halt to U.S. drone strikes against suspected militants on Pakistani soil.
Khan could deprive the PML-N of a parliamentary majority, complicating Sharif's hopes of a return to power 14 years after he was ousted in a military coup, jailed and later exiled.
Sharif may have to form a coalition, raising the possibility of a weak government that would struggle to bring reforms desperately needed to revive the failing economy.
BLOODY ELECTION DAY
Pakistan's Taliban, which is close to al Qaeda, has killed more than 125 people in election-related violence since April. The group, which is fighting to topple the U.S.-backed government, regards the election as un-Islamic.
More bloodshed marred election day. A bomb attack on the office of the Awami National Party (ANP) in Karachi killed 11 people and wounded about 40.
In Baluchistan, four died in a gunbattle and, in another incident, gunmen on a motorcycle opened fire near a polling station, killing two people, police said. A separate attack on a convoy of voters killed at least four people in the restive province.
But despite the violence and the searing heat, many went to the polls excited about the prospect of change.
"The team that we elect today will determine whether the rot will be stemmed or whether we will slide further into the abyss," prominent lawyer Babar Sattar wrote in The News daily.
Despite Pakistan's history of coups, the army stayed out of politics during the five years of the last government and threw its support behind Saturday's election. It still sets foreign and security policy and will steer the thorny relationship with Washington as NATO troops withdraw from neighbouring Afghanistan in 2014.
However, some fear the military could step back in if there is a repeat of the incompetence and corruption that frustrated many Pakistanis during the last government.
On top of the 272 contested seats, a further 70 - most reserved for women and members of non-Muslim minorities - are allocated to parties on the basis of their performance in the constituencies. To have a majority of the total of 342, a party would need 172 seats.