stack_quick_sort

<?php

/*function quick_sort(&$arr, $start, $end)
{
    if ($start < $end) {
        $pivot_index = partition($arr, $start, $end);
        quick_sort($arr, $start, $pivot_index - 1);
        quick_sort($arr, $pivot_index + 1, $end);
    }
}*/

function quick_sort(&$arr, $start, $end)
{
    $border_stack = [];
    $border_stack[] = ['start' => $start, 'end' => $end];
    while (!empty($border_stack)) {
        $border = array_pop($border_stack); // 出栈
        $pivot_index = partition($arr, $border['start'], $border['end']);
        if ($pivot_index - 1 > $border['start']) {
            $border_stack[] = ['start' => $border['start'], 'end' => $pivot_index - 1]; // 入栈
        }
        if ($pivot_index + 1 < $border['end']) {
            $border_stack[] = ['start' => $pivot_index + 1, 'end' => $border['end']]; // 入栈
        }
    }
}

/*function partition(&$arr, $start, $end)
{
    $left_index = $start;
    $right_index = $end;
    $pivot = $arr[$start];

    while ($left_index < $right_index) {
        while ($left_index < $right_index && $arr[$right_index] >= $pivot) {
            $right_index--;
        }
        while ($left_index < $right_index && $arr[$left_index] <= $pivot) {
            $left_index++;
        }
        if ($left_index < $right_index) {
            $temp = $arr[$left_index];
            $arr[$left_index] = $arr[$right_index];
            $arr[$right_index] = $temp;
        }
    }

    // 左右指针重叠停止循环比较,交换元素
    $pivot_index = $left_index;
    $arr[$start] = $arr[$pivot_index];
    $arr[$pivot_index] = $pivot;
    return $pivot_index;
}*/

function partition(&$arr, $start, $end)
{
    $left_index = $start;
    $right_index = $end;
    $index = $start;
    $pivot = $arr[$start];

    while ($left_index <= $right_index) {
        while ($left_index <= $right_index) {
            if ($arr[$right_index] < $pivot) {
                $arr[$index] = $arr[$right_index];
                $index = $right_index;
                $left_index++;
                break;
            }
            $right_index--;
        }
        while ($left_index <= $right_index) {
            if ($arr[$left_index] > $pivot) {
                $arr[$index] = $arr[$left_index];
                $index = $left_index;
                $right_index--;
                break;
            }
            $left_index++;
        }
    }
    $arr[$index] = $pivot;
    return $index;
}

$arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48, 3, 50, 0, 0, -1, -3, 51, -2, -1, -8];
/*$arr = [];
for ($i = 0; $i < 20; $i++) {
    $arr[$i] = mt_rand(0, 100);
}*/
echo join(',', $arr);
echo "\n";
$startTime = microtime(1);
quick_sort($arr, 0, count($arr) - 1);
echo join(',', $arr);
echo "\n";
echo "use time: ", microtime(1) - $startTime, "s\n";


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 typedef struct { int book_id; char book_name[50]; float price; } Book; typedef struct { Book books[MAX_SIZE]; int length; } BookList; void input_books(BookList* list, int n) { for (int i = 0; i < n; i++) { printf("请输入第%d本书的信息:\n", i + 1); printf("图书编号:"); scanf("%d", &list->books[i].book_id); printf("书名:"); scanf("%s", list->books[i].book_name); printf("价格:"); scanf("%f", &list->books[i].price); } list->length = n; } void display_books(BookList* list) { printf("图书表中所有图书的相关信息:\n"); for (int i = 0; i < list->length; i++) { printf("图书编号:%d\n", list->books[i].book_id); printf("书名:%s\n", list->books[i].book_name); printf("价格:%f\n", list->books[i].price); } } void insert_book(BookList* list, int pos, Book book) { if (pos < 1 || pos > list->length + 1) { printf("插入位置不合法!\n"); return; } for (int i = list->length - 1; i >= pos - 1; i--) { list->books[i + 1] = list->books[i]; } list->books[pos - 1] = book; list->length++; } void delete_book(BookList* list, int pos) { if (pos < 1 || pos > list->length) { printf("删除位置不合法!\n"); return; } for (int i = pos - 1; i < list->length - 1; i++) { list->books[i] = list->books[i + 1]; } list->length--; } int count_books(BookList* list) { return list->length; } int partition(BookList* list, int low, int high) { Book pivot = list->books[low]; while (low < high) { while (low < high && list->books[high].book_id >= pivot.book_id) high--; list->books[low] = list->books[high]; while (low < high && list->books[low].book_id <= pivot.book_id) low++; list->books[high] = list->books[low]; } list->books[low] = pivot; return low; } void quick_sort(BookList* list, int
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值