如何用Mathematica簡潔快速地實現氣泡排序?

時間 2021-05-09 07:21:56

1樓:

簡潔和快速不容易兼得,寫了兩個版本,較簡潔的速度慢一些bsort1

=Compile

[},Module

[,Do[If

[a[[j

]]>a[[

j+1]],=],

,];a],

CompilationTarget

->"C",

RuntimeOptions

->"Speed"

];bsort2

=Compile

[},Module

[,Do[If

[a[[j

]]>a[[

j+1]],

tmp=a[[

j+1]];a[[

j+1]]

=a[[j

]];a[[j

]]=tmp],,];

a],CompilationTarget

->"C",

RuntimeOptions

->"Speed"

];data

=RandomReal[1

,10^4

];bsort1

[data]==

Sort

[data]//

AbsoluteTiming

bsort2

[data]==

Sort

[data]//

AbsoluteTiming

如何用mathematica證明下述方程只有唯一實根,且該實根為無理根?

用軟體證明是無理根還是奇怪了點。不過其實你可以直接證明該多項式在Q上面不可約,從而根不可能是有理根。對於這種級數截斷,當你截斷的地方是個質數的時候,可以輕鬆使用Eisenstein判別法判斷。當你截斷的地方不是個質數的時候,就稍微要用到一點點代數知識。利用準素分解就能得到結果,這個東西應該是叫Sch...

Mathematica如何生成MeshShading的顏色張量?

cvgmt r 5 n 10 meshshading Array RGBColor RandomReal,RandomReal,RandomReal Table 2,n normals Normalize RandomReal 1,dists RandomReal n ContourPlot3D x...

Mathematica 中如何定義 f x y f x f y ?

Attributes f HoldAll f a Plus f Unevaluated a 寫完了才想起來上面這種複雜的寫法其實毫無必要。最簡單的就足夠了 ClearAll f Attributes f HoldAll f x y f x f y f a b c f a f b f c 因為Plus...