SOFIA, BULGARIA, AUG. 1 -- Bulgaria's fragile new multi-party parliament narrowly averted disolution today as it elected a new president from the opposition to the ruling Socialists after three weeks of paralyzing debate.

The bipartisan choice of Zhelyu Zhelev, leader of an alliance of 16 opposition parties, came after his supporters threatened a permanent walkout if the Socialists -- the former Communist Party -- did not accept their candidate by the end of the day. That would have meant dissolving parliament and staging new general elections.

But the confrontation was defused when Socialist Premier Andrei Lukanov declared that his party -- which won a 53-percent majority in parliment this spring in the country's first free elections in more than 40 years -- would agree to back Zhelev "for the good of Bulgaria and the Bulgarian people."

Lukanov added the party took the step even though many members would find an opposition president hard to swallow after winning the elections. Opposition legislator Pavel Mazolov agreed, saying: "They think that if Zhelev is made president it is the beginning of the end for the Communist party. And they are right."

The leadership dispute erupted in the midst of a deepening economic crisis that has led the government to halt payments on its $10.6 billion foreign debt and take other urgent steps to ease the situation. The price of gasoline has doubled, while the scarcity of fuel has forced cancellation of some domestic airline flights. Lines outside food and merchandise shops are lengthening, and some basic foodstuffs have been rationed.

Zhelev replaces Petar Mladenov, the reform Communist who led last November's palace coup against longtime dictator Todor Zhivkov. Mladenov was forced to resign last month over charges that he had threatened to use tanks against a public demonstration.

Today's opposition ultimatum clearly rankled some of the more conservative Socialists. "Don't think I don't understand what's going on here," said Velko Vulkanov, the Socialist candidate for vice president: "130 people tried to coerce 240. Either Zhelev or else . . . . That is no way to solve problems."

Today's result did not necessarily signal future cooperation between the Socialists and the opposition. More than 100 Socialist legislators declined to vote, giving the unopposed Zhelev votes from 284 of the 392 members present, 24 more than the required two-thirds majority.

Nor did the opposition, which has repeatedly refused to join the Socialists in a governing coalition, foreswear its unspoken policy of allowing them to take full responsibility for the painful measures needed to improve the struggling economy.

Zhelev, 55, is Bulgaria's best-known philosopher. He was exiled to his native village and expelled from the Communist Party in 1965 because of his doctoral dissertation, which disputed Lenin's theory on the nature of matter. But he gained nationwide fame in 1981 after publishing an analysis of totalitarianism titled "Fascism," which Zhivkov soon banned.

Mazolov described Zhelev as "Bulgaria's Vaclav Havel," but whatever his personal qualities, it is unlikely the new president will enjoy the Czechoslovak leader's near-universal support. In his acceptance speech, Zhelev nominated as vice president Atanas Semerdjiev, who resigned as interior minister last week during the rancorous debate over whether Zhivkov should be allowed to address parliament with live television coverage. Semerdjiev was elected by acclamation.