Re: [問題] 請問個排序的問題

看板Perl作者 (衝了啊)時間19年前 (2006/06/11 03:44), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《cutecpu (可愛中央處理器)》之銘言: : #!/usr/bin/perl : sub mysort{#這邊用bubble sort : for($i=1;$i<=$#_;$i++){ : for($j=0;$j<=$#_-$i;$j++){ : ($_[$j],$_[$j+1])=($_[$j+1],$_[$j]) if($_[$j]>$_[$j+1]); : } : } : } : print "請輸入你要排序的數字,輸入完成請按ctrl+d結束\n"; : chomp (@data = <STDIN>); : &mysort(@data); : print "@data\n"; : ※ 引述《liwei0321 (ㄚ笠伯)》之銘言: : : 就是今天我有個排序的題目 可是我不用 sort ($a <=> $b) 下去做 : : 我要把我給的幾個數給排序 : : 想法是(假如有四個) 兩兩相比 找出第一個最小的 然後刪掉 : : 剩下三個在繼續比較.....一直到結束 : : 這重複過程就用sub來做 : : =================================================== : : #!/usr/bin/perl : : print "請輸入你要排序的數字,輸入完成請按ctrl+d結束\n"; : : chomp (@data = <STDIN>); : : open(FILE,">data\n"); : : print FILE "@data"; : : close(FILE); : : my @input = @data; : : my @out = sort { $a <=> $b } @input; : : open(FILE,">output"); : : print FILE "@out\n"; : : close(FILE); : : 這些是我sort寫出來的 : : 希望知道的高手可以教一下 !!!!感謝! 我不會比較有效率的演算法,orz... @ 提供我自己的笨蛋排序法...大家笑笑XD (大致是順著原po的邏輯) sub stupid_sort{ ゥfif ($_[0] < $_[1]){ $_[0]; } else{ $_[1]; } } chomp (@data = <STDIN>); foreach (@data){ $hash{$_} = 1; } while (%hash){ @tmp = keys %hash; $a = shift @tmp; foreach (@tmp){ $a = &stupid_sort($a, $_); } push @final, $a; delete $hash{$a}; } print "@final\n"; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.58.171.124

06/11 16:09, , 1F
感謝!!!! 推!
06/11 16:09, 1F
文章代碼(AID): #14Yo4bNT (Perl)
討論串 (同標題文章)
文章代碼(AID): #14Yo4bNT (Perl)