快速排序中的分区中的最小部分是否有某些条件?

时间:2016-02-14 17:51:10

标签: sorting math probability quicksort partitioning

0 < a < 0.5保持不变。我们有一个n-element数组作为输入。随机快速排序从阵列中随机选择一个元素作为轴和分区。最小部分的概率大于an。我的简短回答用概率1-2a说。谁能说出这个概率是如何计算的?

1 个答案:

答案 0 :(得分:1)

较小分区的大小均匀分布在[0,n / 2]范围内,因此它小于an / (n/2)的概率为a。因此,它大于1 - an / (n/2)的概率为an/(n/2)2a正是1 - 2a,因此概率为 Pivot positions a·n n/2 a·n+n/2 n v v v v <---------------------|||||||·---------------------|||||||> /---^---\ /---^---\ smaller partition on left smaller partition on right bigger than a·n bigger than a·n size: n/2 - a·n size: n - (a·n + n/2) Total size: n - 2a·n Probability: 1 - 2a

这是一个图表,如果它有帮助:

@model CommonLayer.ORDER
    @{
    ViewBag.Title = "Update";
    Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h4>Update Order Status</h4>
    <br />

    @if (Model.OrderStatus != "Shipped")
    {
    using (Html.BeginForm("Update","Order",FormMethod.Post))
    {
        <div class="form-horizontal">
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.UserId)
            @Html.HiddenFor(model => model.OrderId)
            <div class="form-group">
                @Html.LabelFor(model => model.OrderId, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select class="form-control" name="OrderStatus">
                        <option value="Pending" id="pending">Pending</option>
                        <option value="Shipped" id="shipped">Shipped</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Update" class="btn btn-primary" />
                    </div>
                </div>
        </div>
    }
    }
    else
    {
    <h4>Order Has Been Shipped!</h4>
    }